Click here to Skip to main content
15,900,676 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

i have query regarding the insert item in tree control as mentioned below:


m_cTreeCtrl.InsertItem(TVIF_TEXT | TVIF_IMAGE |
TVIF_SELECTEDIMAGE
| TVIF_PARAM, strTemp, 0, 0, 0, 0, (LPARAM)(LPCTSTR)(LPCTSTR)strParamval, m_hMatNode, TVI_LAST);

the strparamval is inserted here

when iam retrieving the data iam not able to get the data as mentioned below:

TVITEMEX item;
item.mask = TVIF_PARAM;
item.hItem = hChildItem;
TreeView_GetItem(m_cTreeCtrl, &item);
CString strParam = (LPCTSTR)item.lParam;


this is a for loop so lot of times the function will be called. when i kept below code it worked as mentioned below:

m_cTreeCtrl.InsertItem(TVIF_TEXT | TVIF_IMAGE |
TVIF_SELECTEDIMAGE| TVIF_PARAM, strTemp, 0, 0, 0, 0,
(LPARAM)strParamval.AllocSysString(),
m_hMatNode, TVI_LAST);


but as this logic is loop when i allocate and sysfreestring its giving wrong value for last time.

my doubt is wether we have to do sysfreestring here as it is in for loop ?

What I have tried:

m_cTreeCtrl.InsertItem(TVIF_TEXT | TVIF_IMAGE |
TVIF_SELECTEDIMAGE| TVIF_PARAM, strTemp, 0, 0, 0, 0,
(LPARAM)strParamval.AllocSysString(),
m_hMatNode, TVI_LAST);
Posted
Updated 30-Oct-17 4:30am
Comments
jeron1 30-Oct-17 10:26am    
Any reason for the 2 (LPCTSTR)'s?
(LPARAM)(LPCTSTR)(LPCTSTR)strParamval
Richard MacCutchan 31-Oct-17 5:38am    
See the comments in my solution below. If you free the allocated memory then the tree will not be able to display anything.

If you store the pointer to a data item in a list or control and then free it, then it no longer exists. So when you later try to access that pointer you will get garbage or null reference exception. You must save the string in an allocated piece of memory and not free it until it is no longer required.
 
Share this answer
 
It depends on what you want to store in the tree control. If you want to set the item text, use the lpszItem parameter of the CTreeCtrl::InsertItem[^] method. That will allocate memory to store a copy of the passed text and handles freeing when the item is deleted (or the complete tree control). To get that text later use the GetItemText method which returns a CString.

The lParam parameter is provided for storing additional information about an item. If that that does not fit in an integer it must be a pointer to allocated memory (or fixed memory like a const string). With allocated memory, you are responsible for deleting when removing the item and deleting the tree control.

If you want for example to pass a CString as application-specific parameter, allocate that using new:
C++
CString *pStrParam = new CString(strParamval);
m_cTreeCtrl.InsertItem(TVIF_TEXT | TVIF_IMAGE |
TVIF_SELECTEDIMAGE| TVIF_PARAM, strItemText, 0, 0, 0, 0,
    (LPARAM)pStrParam, m_hMatNode, TVI_LAST);
To delete the item later:
C++
TVITEMEX item;
item.mask = TVIF_PARAM;
item.hItem = hChildItem;
TreeView_GetItem(m_cTreeCtrl, &item);
CString *pStrParam = (CString*)item.lParam;
delete pStrParam;
When the control is deleted, the above has to be done for each item.
 
Share this answer
 
v2
Comments
Member 12677926 31-Oct-17 2:09am    
it is giving error

CString pStrParam = new CString(strParamval);
Jochen Arndt 31-Oct-17 4:22am    
Uups.
Sorry, it must be
CString *pStrParam = new CString(strParamval);
Member 12677926 31-Oct-17 2:10am    
its liking every time we are allocating memory and deleteing at last i think it will be leak
Jochen Arndt 31-Oct-17 4:31am    
It will not leak if you delete it.
A common option to avoid leaks like in your case is deriving a class from CTreeView and adding the code to delete all allocated memory in the destructor. The class should also contain other functions that makes handling easier like an overwritten InsertItem() version that does the allocation.
Member 12677926 1-Nov-17 1:17am    
actaully we are doing new in for loop so it will call lot of times after that we are deleting so i think it will leak ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900