Clearing a Collection

J

Jeff Gaines

If there is a more appropriate newsgroup for this please tell me :)

I am writing a "GridView" control, it looks like a List View but the Cells
are selectable individually.

I have had horrendous problems with the VS IDE (devenv.exe) crashing -
sometimes it closes so quickly it's made me jump. I have pinned it down to
the OnClear override which I have been using to clear Lists in the various
collections.

The class is declared:
public class JColumnCollection : System.Collections.CollectionBase

And the OnClear function:

protected override void OnClear()
{
JColumn column;

for (int col = 0; col < List.Count; col++)
{
column = (JColumn)List[col];
column.OnColumnChanged -= new
JColumn.ColumnChangedEventHandler(colHeader_OnColumnChanged);
}

List.Clear();

ColChangedEventArgs e = new
ColChangedEventArgs(ColChangedEventArgs.ChangeTypes.ColumnsCleared, null);
RaiseColumnChangedEventHandler(null, e);
}

I *thought* it was good practice to remove the ColumnChangedEventHandler
before clearing the List but VS doesn't like it at all.

It seems I am doing something wrong (commenting out this function allows
everything to work as designed). I would appreciate it if anybody can
throw any light on this for me :)
 
M

Morten Wennevik [C# MVP]

Hi Jeff,

If you don't unsubscribe from the event the garbage collector won't be able
to collect the JCollection since a number of columns still refer to it via
the event. It will eventually get collected once all the columns are
collected but it is good practice to unsubscribe before deleting the
reference.

As for what happens, I can only guess the JColumn.ColumnChangedEventHandler
is somehow involved, or some code being triggered OnColumnChanged does not
handle the removal of columns very well. I can't say why Visual Studio
breaks when this happen though, but you may want to try closing the all
unecessary windows in Visual Studio when debugging, and see if Visual Studio
breaks when you run the project on other machines as well.

As for more appropriate newsgroups. You might have better luck in
microsoft.public.vstudio.* altough this group isn't very inappropriate either.

If you can make a complete sample project demonstrating this problem I can
take a closer look at it.
 
J

Jeff Gaines

Hi Jeff,

If you don't unsubscribe from the event the garbage collector won't be able
to collect the JCollection since a number of columns still refer to it via
the event. It will eventually get collected once all the columns are
collected but it is good practice to unsubscribe before deleting the
reference.

As for what happens, I can only guess the JColumn.ColumnChangedEventHandler
is somehow involved, or some code being triggered OnColumnChanged does not
handle the removal of columns very well. I can't say why Visual Studio
breaks when this happen though, but you may want to try closing the all
unecessary windows in Visual Studio when debugging, and see if Visual
Studio
breaks when you run the project on other machines as well.

Hi Morten, thanks for the reply :)

I persevered in view of your response and it was the List.Clear() that was
the problem - with that removed it works fine, obviously this was not a
good place to clear the list :)

Thanks again!
 

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

Top