How do you correctly use wxListView/Ctr user data?

BlitzMax Forums/Brucey's Modules/How do you correctly use wxListView/Ctr user data?

DavidDC(Posted 2008) [#1]
Hi Brucey :-)

I'm currently struggling with a memory leak in my listbox code. I've narrowed it down to the user data element that may be associated with each list item. Obviously I'm misusing this functionality, as the way I'm doing things causes a very decent leak.

The idea is to associate a String with each listbox cell item. In my app, this string stores the database unique id of the contents of this cell. The question is, how do I correctly do this?

Thanks!

Example code:



Brucey(Posted 2008) [#2]
You always manage to dig out those nasty little bugs, eh? :-)

Should be sorted now - at least... your example doesn't leak any more.

So, what was up?

Well, this is where the leak was occurring :
		' 	Please note that client data is associated with the item And Not with subitems.
		item.SetData(_s_context)
		
		'	Which column are we destined for?
		item.SetColumn(_col)

Note that your initial comment mentions the data is associated only with the item.
Well, you were essentially setting the subitems too. The underlying code didn't care about that, so was ref-counting everything you were setting, even for columns > 0.
When it came time to remove the data, some of those references were not available to release. (they had been replaced by subsequent calls to SetData)

What happens now, when you SetData on an item, it checks the current column. If > 0 it skips it.
However, you SetColumn after setting data... so I also check on SetColumn, if > 0 and data is set, we release it - doesn't belong there!

This *should* mean that only data set on a column of 0 should be valid.

Let me know how you get on with it :-)


DavidDC(Posted 2008) [#3]
Thanks so much Brucey. The fix has stopped the leaks in both the test code in XP and in my app. Very happy!

Bonus plus is I now know what a "subitem" is - I had just copied that comment straight from wxDocs :-)