Solved

Tree Control and Memory Leak!!!!!

Posted on 2001-06-06
7
1,079 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Strange date stored 22 150
Pressing cancel button with C# UI Automation 3 80
Macro to save highlighted rows as a separate file 5 45
fizzArray  challenge 1 94
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

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