I've run into a situation were I cannot free my dynamically allocated memory eventhough my pointer in the 'free' call is definitely pointing to the right location. I've written a piece of test code that works when compiled with MSVC, but not with CVI.
Below is a test small, reduced program that demostrates the problem.
This code should be continuously mallocing arrays of ints and then freeing them, but when I compile it with CVI my memory footprint grows continuously. When compiled with MSVC, my memory footprint stays the same, as it should.
When I step through my code with the CVI debugger, the pointer passed to AnyListDeleteItem is pointing to the correct memory location when 'free' is called, but the memory does is not freed.
Is there something I don't understand about dynamic memory allocation or is this a bug?
Regards,
Greg VanSlyke
#include
#include
#include
static int panelHandle;
void* AnyListAddItem(void** list, int size);
int AnyListCreate(void** list);
int AnyListDelete(void** list) ;
int AnyListDeleteItem(void** list, void* item);
AnyListCreate(void** list)
{
*list = NULL;
return 0;
}
void* AnyListAddItem(void** list, int size)
{
*list =(void*)malloc(size);
return *list;
}
int AnyListDeleteItem(void** list, void* item)
{
free(item);
return 0;
}
int main (int argc, char *argv[])
{
int *any_list;
int *list_item1;
if (InitCVIRTE (0, argv, 0) == 0)
return -1; /* out of memory */
if ((panelHandle = LoadPanel (0, "test.uir", PANEL)) 0)
return -1;
DisplayPanel (panelHandle);
AnyListCreate((void**)&any_list);
while(1)
{
list_item1 = (int*)AnyListAddItem((void**)&any_list,100*sizeof(int));
AnyListDeleteItem((void**)&any_list,list_item1);
}
RunUserInterface ();
DiscardPanel (panelHandle);
return 0;
}