Inherited collection reference lost after for each loop

  • Thread starter Thread starter Alex
  • Start date Start date
A

Alex

hi,

I have a class that has inheritted a collectionbase class
The class works fine generally, I can add items to the collection however
whenever I execute a For Each loop, the dispose event is called and then
inherited collection disapears. (The same problem occurs regardless of which
collection type I inherit)
I cannot figure this out as I still have a ref to the object that inherits
the collection. I think it is something like the collection object is not
referenced to the class that inherits it however I cannot see how to fix it.

Any ideas on this would be appreciated

alex
 
Alex said:
I have a class that has inheritted a collectionbase class
The class works fine generally, I can add items to the collection however
whenever I execute a For Each loop, the dispose event is called and then
inherited collection disapears. (The same problem occurs regardless of which
collection type I inherit)
I cannot figure this out as I still have a ref to the object that inherits
the collection. I think it is something like the collection object is not
referenced to the class that inherits it however I cannot see how to fix it.

Any ideas on this would be appreciated

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
It is not easy to post a working example however I have constructed some
simple pseudo code to demonstrate. The application
is written in vb however to attempt to write the example in vb would make it
unclear.

class MyList: Inherits Collections.Specialized.ListDictionary
{
bool LoadListFromFile(string _StrFileName as string){}
bool SaveListToFile(string _StrFileName as string){}
bool AddItemToList(string _StrItem as string){}
}


class MyForm
{
// I have declared a list somewhere else and I am simply assigning a
ref to the class here
// the list has already been initialised and loaded
MyList _ListClass= AnotherListClass;

//enumerate object to display items
object _MyObject;

// _ListClass.count=3 There are items in the list
for each _MyObject in _ListClass
// the items are added to the list no problem
list.items.add _MyObject
next

// now after the loop the object appears to go out of scope
// and the dispose method is called by .Net. I could understand this if
the object was declared here
// however the object is declared in somewere else. I do not loose the
ref to the MyList class
// it is only the base class that is disposed of. I have seen the same
behaviour with other similar implementations
// I think the problem is something obvious however just cannot see it.
// _ListClass.count=0 there are now no items
}
 
Since writing I have established that the For Each loop has nothing to do
with the problem. I believe that it is now something to do with the Finalize
method being called

When an object that has inherited a collection goes out of scope the
finalize method appears to be called even if there is another ref to the
object. When the finalize method is called the finalize method on the
underlying base collection is called which automatically kills the
collection. I have found a work around and that is to call the
surpressfinalize method which stops this however I have not actually worked
out why this is happening. I am going to have to learn a lot more about the
way references are behave.

If anyone knows what is going on here then i would love to hear from you

thanks
Alex
 
Alex said:
It is not easy to post a working example

Why, out of interest? It sounds like something which should be fairly
simple to reproduce.
however I have constructed some
simple pseudo code to demonstrate. The application
is written in vb however to attempt to write the example in vb would make it
unclear.

class MyList: Inherits Collections.Specialized.ListDictionary
{
bool LoadListFromFile(string _StrFileName as string){}
bool SaveListToFile(string _StrFileName as string){}
bool AddItemToList(string _StrItem as string){}
}


class MyForm
{
// I have declared a list somewhere else and I am simply assigning a
ref to the class here
// the list has already been initialised and loaded
MyList _ListClass= AnotherListClass;

//enumerate object to display items
object _MyObject;

// _ListClass.count=3 There are items in the list
for each _MyObject in _ListClass
// the items are added to the list no problem
list.items.add _MyObject
next

// now after the loop the object appears to go out of scope
// and the dispose method is called by .Net.

The dispose method is called on what though? It should be called on the
enumerator.
I could understand this if
the object was declared here
// however the object is declared in somewere else. I do not loose the
ref to the MyList class it is only the base class that is disposed of.

What exactly do you mean by that? The class itself is not disposed at
all, and there's only one object here - there isn't an instance of the
base class and separate instance of the child class.
 
Alex said:
Since writing I have established that the For Each loop has nothing to do
with the problem. I believe that it is now something to do with the Finalize
method being called

When an object that has inherited a collection goes out of scope the
finalize method appears to be called even if there is another ref to the
object.

I very much doubt that that's happening. Something else must be going
on. Again, a short but complete program demonstrating the problem will
help us to work out what's going on.
 
Back
Top