Solved

Tree Control and Memory Leak!!!!!

Posted on 2001-06-06
7
1,077 Views
Last Modified: 2013-11-20
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!!!
0
Comment
Question by:hoandrew
7 Comments
 
LVL 31

Accepted Solution

by:
Zoppo earned 80 total points
ID: 6163354
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
 
LVL 5

Expert Comment

by:proskig
ID: 6163698
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
 

Author Comment

by:hoandrew
ID: 6166939
I have fixed it!
Many thanks!
0
ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

 
LVL 31

Expert Comment

by:Zoppo
ID: 6167092
what was the problem?
0
 

Author Comment

by:hoandrew
ID: 6175685
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
 
LVL 31

Expert Comment

by:Zoppo
ID: 6176221
only delete the allocated data when the tree node is destroyed ... when app closes make
sure all treenodes are deleted...
0
 
LVL 23

Expert Comment

by:Roshan Davis
ID: 9484580
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

Featured Post

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question