473,396 Members | 2,009 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Object lifetime/event problem

I have a class that raises events that downstream objects subscribe to. In
one case, after destroying the object, the event seems to still get handled
in a subscriber object. So I instantiate an object and the event fires
correctly. Then I destroy the object and instantiate another instance and the
subscriber event handler get hit twice. I've tried using IDisposible and also
using GC.Collect, but it didn't seem to help. I've also tried using
RemoveHandler before destroying the object.

It's not clear to me whether the handler is firing because the objects are
still in memory or for some other reason.

Any help would be appreciated.
Jun 27 '08 #1
3 1542
Barry Gilbert wrote:
I have a class that raises events that downstream objects subscribe to. In
one case, after destroying the object, the event seems to still get handled
in a subscriber object. So I instantiate an object and the event fires
correctly. Then I destroy the object and instantiate another instance and the
subscriber event handler get hit twice. I've tried using IDisposible and also
using GC.Collect, but it didn't seem to help. I've also tried using
RemoveHandler before destroying the object.

It's not clear to me whether the handler is firing because the objects are
still in memory or for some other reason.

Any help would be appreciated.
First of all, you can't explicitly "destroy" objects. You can call
..Dispose() on them, but that's it. That does not in itself do anything; the
object itself has to release resources. An object will stick around for as
long as there are references to it; when the last reference disappears, the
garbage collector will *eventually* reclaim the memory. Threads do not
belong to objects and will stick around until they either exit themselves or
(if flagged as background threads) the process exits.

When does your class raise events? In response to what? My guess is that
whatever you're using for that isn't stopped or signaled properly. The only
way to prevent the events from being raised is simply to not raise them. You
should know when not to raise them anymore because you should know when your
object is done doing whatever it's supposed to do (because someone called
..Dispose() or .Stop() or whatever, or because an external condition occurs).

--
J.
http://symbolsprose.blogspot.com
Jun 27 '08 #2
On Wed, 21 May 2008 08:31:01 -0700, Barry Gilbert
<Ba**********@discussions.microsoft.comwrote:
>I have a class that raises events that downstream objects subscribe to. In
one case, after destroying the object, the event seems to still get handled
in a subscriber object. So I instantiate an object and the event fires
correctly. Then I destroy the object and instantiate another instance and the
subscriber event handler get hit twice. I've tried using IDisposible and also
using GC.Collect, but it didn't seem to help. I've also tried using
RemoveHandler before destroying the object.

It's not clear to me whether the handler is firing because the objects are
still in memory or for some other reason.

Any help would be appreciated.
Setting a reference to null does not make an object go away. It just
makes it eligible for garbage collection.

Suppose for example I create an instance of class X. In the
constructor the class creates an instance of a timer and sets the
timer to fire every second.

Immediately after creating the instance I set the reference to it to
null. Until the garbage collector runs and collects this instance,
the timer will continue to fire. The instance is still there, and
until the garbage collector runs and checks things no one knows that
there are no longer any references to the instance.

You say "the subscriber event handler get hit twice". Is that twice
for the same instance, or once for two different instances?

If twice for the same instance, then you have subscribed to the event
twice.

If for two different instances, then you need to find out why the
class that you no longer have references to is still generating
events.

Perhaps you need to implement IDispose in your classes, and have the
Dispose method 'turn off' the class - make it stop doing whatever
generates events. Then if you dispose of the class when you are
finished with it, it will stop generating events.
Jun 27 '08 #3
When you destroy your subscriber object do you remove the event handler?

i.e.

// Shut down my subscriber object
Close()
{
WhereTheEventsComeFrom.MyEvent -= new Event( this.EventHandlerRoutine );
}

If you don't do that the object raising the event still has a reference to
the subscriber. The result of this is that the subscriber will remain in
memory. Creating another one adds a new instance of the event handler - 2
objects now exist in memory, each one subscribing to the event.

I know you say you call RemoveHandler but the fact that your event is firing
twice suggests to me that your original object is still around, and is still
subscribing to the event.

HTH,

Adam.

"Barry Gilbert" <Ba**********@discussions.microsoft.comwrote in message
news:A8**********************************@microsof t.com...
>I have a class that raises events that downstream objects subscribe to. In
one case, after destroying the object, the event seems to still get
handled
in a subscriber object. So I instantiate an object and the event fires
correctly. Then I destroy the object and instantiate another instance and
the
subscriber event handler get hit twice. I've tried using IDisposible and
also
using GC.Collect, but it didn't seem to help. I've also tried using
RemoveHandler before destroying the object.

It's not clear to me whether the handler is firing because the objects are
still in memory or for some other reason.

Any help would be appreciated.

Jun 27 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: klaus triendl | last post by:
hi, recently i discovered a memory leak in our code; after some investigation i could reduce it to the following problem: return objects of functions are handled as temporary objects, hence...
7
by: dm_dal | last post by:
Just a quick question regarding object scope and lifetime. Given the following: { DataRow myRow = new DataRow(); ...... do some stuff { myRow = new DataRow; ..... do some stuff
9
by: kermit | last post by:
I keep seeing that you can use the FileSystemObject in either VB script, or Javascript on an aspx page. I added a refrence to the scrrun.dll I added importing namespaces for 'System.Object',...
2
by: bughunter | last post by:
This is partly 'for the record' and partly a query about whether the following is a bug somewhere in .Net (whether it be the CLR, JITter, C# compiler). This is all in the context of .Net 1.1 SP1. ...
5
by: Michael Moreno | last post by:
Hello, In a class I have this code: public object Obj; If Obj is a COM object I would like to call in the Dispose() method the following code: ...
16
by: anonymous.user0 | last post by:
The way I understand it, if I have an object Listener that has registered as a listener for some event Event that's produced by an object Emitter, as long as Emitter is still allocated Listener...
17
by: Divick | last post by:
Hi, I am designing an API and the problem that I have is more of a design issue. In my API say I have a class A and B, as shown below class A{ public: void doSomethingWithB( B * b) { //do...
6
by: Pablo | last post by:
Hello, I am writing a windows application using C++ and BorlandBuilder 6 compiler. It is an event driven program and I need to create objects of some classes written by me. One of the classes...
10
by: Hendri Adriaens | last post by:
Hi, I'm trying to automate the creation of an excel file via COM. I copied my code below. I read many articles about how to release the COM objects that I create. The code below runs just fine...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.