Solved

Tree Control and Memory Leak!!!!!

Posted on 2001-06-06
7
1,065 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 30

Accepted Solution

by:
Zoppo earned 80 total points
Comment Utility
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
Comment Utility
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
Comment Utility
I have fixed it!
Many thanks!
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 30

Expert Comment

by:Zoppo
Comment Utility
what was the problem?
0
 

Author Comment

by:hoandrew
Comment Utility
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 30

Expert Comment

by:Zoppo
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
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 demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

763 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now