Using List Management APIs in an ADL program

Hi.

I have an applications that uses ADL library. There is a remark in ADL user guide that prohibites mixing lower-level APIs with ADL, but I still decied to try to use List Management API functions, and eventualy got an “Access Violation” exception in rte_kernel.exe process.
Is there a way to use list apis safely, or I have to implement lists myself?

And one more question. Here is a program that crashes rte_kernel.exe process when executed in RTE mode. Can someone tell what could cause this crash?

The program crashes when an item is removed from the list.

#include "adl_global.h"

u32 wm_apmCustomStack [ 256 *3];
const u16 wm_apmCustomStackSize = sizeof ( wm_apmCustomStack );

wm_lst_t	g_list;

s16	Storage_CompareItem(void* item1, void* item2)
{
	return OK;
}

void Storage_FreeItem(void* item)
{
	adl_memRelease(item);
}

void Storage_AddToList(wm_lst_t add_list, void* mem)
{
	wm_lstAddItem(add_list, mem);
	if(wm_lstGetCount(add_list) > 10)
	{
		//	Crash occurs here, after wm_lstDeleteItem is called
		wm_lstDeleteItem(add_list, 0);
	}
}

void Storage_Initialize()
{
	wm_lstTable_t archive_f;
	archive_f.CompareItem = Storage_CompareItem;
	archive_f.FreeItem = Storage_FreeItem;
	g_list = wm_lstCreate(WM_LIST_NONE, &archive_f);
}

void Storage_Add()
{
	void* mem = adl_memGet(sizeof(int));
	Storage_AddToList(g_list, mem);
}

void adl_main ( adl_InitType_e  InitType )
{
	Storage_Initialize();
	int i = 0;
	for(i = 0; i < 20; ++i)
	{
		Storage_Add();
	}
}

It also lists the exceptions to that rule - which include the List Management!

Note that the so-called Basic interface is now obsolete

Then why do I get an Access Violation? I suppose, there is nothing wrong about the code.

Why do you suppose that?
There must be something wrong with the code - otherwise it wouldn’t crash, would it?! 8)

Are you being careful to check all return values from all API calls…?

I found the reason of the crash. As it turns, wm_lstCreate stores a pointer to wm_lstTable_t structure, instead of copying it’s fields. This behaviour isn’t described in documentation (it could be just a little bit more obvious=) ), so I passed a pointer to a local structrue copy by mistake.

That is a classic ‘C’ programming error - nothing to do with Wavecom, ADL or the Open-AT list management!

The whole point of local variables in ‘C’ is precisely that they exist only inside the function, and only while the function is “active” - therefore any reference to any local variable from outside its scope is always an error!

How obvious is this:

It is also one of those errors that might not immediately cause any obvious problems - you might be “lucky” and your code might appear to “work”.

In fact, it should be considered very unlucky if such code does just happen to “work” - because it means that there is a latent bug just waiting to bite as a result of some (apparently) totally unrelated cause…! :astonished:

Pointers in ‘C’ are extremely powerful - but this also makes them extremely dangerous!! :astonished: :open_mouth:

and a primary cause for my headaches :stuck_out_tongue:

and probably most other ‘C’ programmers, too! :wink:

Please, don’t blame me for this mistake=)
First of all, the fragment of documentation that awneil cited concerns wm_lstAddItem function, not wm_lstCreate. There is nothing said about that funcTable must not be a local pointer.
And second, I used C only to write programs using WinAPI, where passing a local pointer to a structure is a common case. I think, some people may do this error, so this topic could be quite useful anyway.