Tree Control and Memory Leak!!!!!

I am writing an application that contains a tree view control. I have set item data which is a user-defined class(call UserList, see below) to each of the node.

Now, i know that i need to delete the item data by myself when the node is deleted (TVN_DELETEITEM) (See below). But there is still memory leak here. Can any help???? It is very urgent!!!

class UserList
{
public:
CString username; //LoginName
int status;
CString NickName;
CString UserID;
int IsVIP;
public:
UserList(){};

UserList(CString name, int Status)
{
username = name;
status = Status;
}

UserList(CString name, CString nickname)
{
username = name;
NickName = nickname;
}

UserList(CString name, CString nickname, int Status, CString userid, int IsVIP_Flag)
{
username = name;
NickName = nickname;
status = Status;
UserID = userid;
IsVIP = IsVIP_Flag;
}

void setContent(CString name, CString nickname, int Status, CString userid, int IsVIP_Flag)
{
username = name;
NickName = nickname;
status = Status;
UserID = userid;
IsVIP = IsVIP_Flag;
}

void setContent(CString name, CString nickname)
{
setContent(name, nickname, 0, "", 0);
}

void setUserName(CString name)
{
username = name;
}

void setNickName(CString nickname)
{
NickName = nickname;
}
...................
};

//Being Called when TVN_DELETEITEM is notified
void HostDialog::OnDeleteItem(NM_TREEVIEW* pNMTreeView)
{
// Item data is pNMTreeView->itemOld.hItem
// Free data allocated with deleted item

//Don't work with this approach
CTreeCtrl* pCtrl;
UserList* ul;
HTREEITEM item ;

item = pNMTreeView->itemOld.hItem;

ul = (UserList*)pNMTreeView->itemOld.lParam;
if(ul)
{
delete ul;
ul = NULL;
}

/* Don't work with this approach
pCtrl = (CTreeCtrl*) GetDlgItem(IDC_GEN_LIST);

ul = (UserList*)pCtrl->GetItemData(item);

if(ul)
{
delete ul;
ul = NULL;
}
*/

/* Don't work with this approach
UserList* lptvid=NULL;
HRESULT hr;
LPMALLOC lpMalloc;

//Let's free the memory for the TreeView item data...
hr=SHGetMalloc(&lpMalloc);
if (FAILED(hr))
return;

lptvid=(UserList*)pNMTreeView->itemOld.lParam;
if(lptvid){
lpMalloc->Free(lptvid);
lpMalloc->Release();
}
*/
}

Thanks in advance!!!
hoandrewAsked:
Who is Participating?
 
ZoppoConnect With a Mentor Commented:
Hi hoandrew,

code seems to be ok, so I only see these possibilities:
- the tree is destroyed without the items being deleted
- some UserList objects are created but not set as tree's item data or some treeitem's data is
set more than once
- the leaks has nothing to do with the tree

could you post code where you add the items and set it's data?

ZOPPO
0
 
proskigCommented:
When using MSVC, you can see ID of the block which was not deallocated, set a breakpoint to break when you allocate this block. Thus you will be able to find out which exactly piece of code allocates memory and elaborate why it was not deallocated.
0
 
hoandrewAuthor Commented:
I have fixed it!
Many thanks!
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
ZoppoCommented:
what was the problem?
0
 
hoandrewAuthor Commented:
First of all, many thanks for your replies.

The cause of the memory leak is that: there are some cases that i havn't set the object to become item data of the node. (I think i am too silly. )

But there is another point: Each time when i create a new object and associate it to the node, i will program like that:

/* ul is a class variable of type UserList* */
ul = new UserList("1", "andrew");

node->SetItemData((DWORD)ul);

And when the application is about to close, i will delete the class variable ul:

delete ul;
ul = NULL;

However, i will also free the data when the node is deleted, so there will be assertion error, and it should be avoid....

0
 
ZoppoCommented:
only delete the allocated data when the tree node is destroyed ... when app closes make
sure all treenodes are deleted...
0
 
Roshan DavisCommented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Answered by : Zoppo

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Roshan Davis
EE Cleanup Volunteer
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.