Click to See Complete Forum and Search --> : Memory leak with lParam


zrbite
July 15th, 2002, 01:07 PM
I have a problem with memory leaks using lParam. I am using a Tree control, and when adding an item, I set the lParam to a variable using char*. However, if I free that char*, the lParam value is gone later on. Using strdup works, but causes memory leaks after closing the application.

cdir is a std::string which as an example, will have a value of
"c:\zrbite\programs"

char* temp = strdup(cdir.c_str());

TVITEM item;
TVINSERTSTRUCT itemstr;
item.mask = TVIF_TEXT | TVIF_PARAM | TVIF_IMAGE | TVIF_SELECTEDIMAGE;
item.lParam = (LPARAM) (LPCTSTR) temp;
item.pszText = title;
item.iImage = 1;
item.iSelectedImage = 1;

itemstr.hParent = parent.hItem;
itemstr.item = item;
CtlDir.InsertItem(&itemstr);
free(temp);

The lParam doesn't work because temp is freed. However, having strdup(temp) to set lParam causes a memory leak also.

myron
November 22nd, 2004, 12:21 AM
Of course you cannot access the data, since it's local.
You have to new a memory for that lparam, and then free them when you are going to terminate the program (control).