Warning: ListView may leak memory!

G

Guest

Hi guys,

I just found that ListView leaks memory on a specific ocasion. This is when removing (already inserted) items from the ListView while its window handle is not created yet (or has been destroyed already).

To reproduce this condition it is enough to put a ListView on a tab page which is not initialy visible. Adding items to the ListView is not a problem. Removing thems (again - before the ListView has its handle created!!!) finnishes with no obvious problems too but it leaves the (being removed) items stuck forever in an internal Hashtable collection (listItemsTable).

The source of the problem is in the ListViewItemCollection.RemoveAt(int index) method. The first thing it seems to do is unhosting the removed item from the ListView and later it (unsuccesufuly) removes the item from the internal Hashtable. But unhosted item does not have na ID value any more and therefore can not be located by the Hashtable for removal. So it will sit there forever.

A simple workaround, which I'm using right now, is to force the creation of the handle of all ListView controls before you start adding and removig items to them. When the handle is there the ListView is using the native LVI's ID (which is still in place) as a key when removing the intem from the internal Hastable, so it works.

I did not have the time to search on the web and see if this bug was already reported. My excuses if that's the case, but the issue seems to be present in .NET 2.0 Beta as well.

I'll appreciate appreciate a from somebody from MS since I posted this issue to the GotDotNet group but got no reponse and thus I don know yet if MS aknowledged the issue

Best,

Stefa
 
C

Claes Bergefall

Yes, it looks like you're right
This simple snippet demonstrates the problem

Dim lv As New ListView
Dim item0 As ListViewItem = lv.Items.Add("0")
Dim item1 As ListViewItem = lv.Items.Add("1")
Dim item2 As ListViewItem = lv.Items.Add("2")
lv.Items.Remove(item0)
lv.Items.Remove(item1)
lv.Items.Remove(item2)

lv.listItemsTable still contains 3 items after the last remove
I didn't find any info on the net. Anyone from MS like to comment?

The fix should be really simple. Just switch place of the following two
rows in ListViewItemCollection.RemoveAt:

this[index].UnHost();
int num1 = this.DisplayIndexToID(index);

/claes


Stefan_A said:
Hi guys,

I just found that ListView leaks memory on a specific ocasion. This is
when removing (already inserted) items from the ListView while its window
handle is not created yet (or has been destroyed already).
To reproduce this condition it is enough to put a ListView on a tab page
which is not initialy visible. Adding items to the ListView is not a
problem. Removing thems (again - before the ListView has its handle
created!!!) finnishes with no obvious problems too but it leaves the (being
removed) items stuck forever in an internal Hashtable collection
(listItemsTable).
The source of the problem is in the ListViewItemCollection.RemoveAt(int
index) method. The first thing it seems to do is unhosting the removed item
from the ListView and later it (unsuccesufuly) removes the item from the
internal Hashtable. But unhosted item does not have na ID value any more and
therefore can not be located by the Hashtable for removal. So it will sit
there forever.
A simple workaround, which I'm using right now, is to force the creation
of the handle of all ListView controls before you start adding and removig
items to them. When the handle is there the ListView is using the native
LVI's ID (which is still in place) as a key when removing the intem from the
internal Hastable, so it works.
I did not have the time to search on the web and see if this bug was
already reported. My excuses if that's the case, but the issue seems to be
present in .NET 2.0 Beta as well.
I'll appreciate appreciate a from somebody from MS since I posted this
issue to the GotDotNet group but got no reponse and thus I don know yet if
MS aknowledged the issue.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top