CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jul 2002
    Posts
    2

    Memory leak with lParam

    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.

  2. #2
    Join Date
    Sep 2003
    Location
    M...N
    Posts
    220

    Re: Memory leak with lParam

    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).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured