473,698 Members | 2,833 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1564
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**********@d iscussions.micr osoft.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
RemoveHandle r 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()
{
WhereTheEventsC omeFrom.MyEvent -= new Event( this.EventHandl erRoutine );
}

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**********@d iscussions.micr osoft.comwrote in message
news:A8******** *************** ***********@mic rosoft.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
3746
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 their dtor is called immediately and not at the end of the function. to be able to use return objects (to avoid copying) i often assign them to a const reference. now, casting a const return object from a function to a non-const reference to this...
7
4023
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
3918
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', 'Scripting', 'Scripting.FileSystemObject', and a few others However, when I try to create the fso object I keep receiving an error. 'ActiveX component can't create object: 'Scripting.FileSystemObject'
2
3192
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. Recently we (my fellow team members & I) observed an InvalidOperationException - 'collection has been modified', however it wasn't immediately obvious why this would be occuring. There are no modifications occuring within the loop and there is...
5
2514
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: System.Runtime.InteropServices.Marshal.ReleaseComObject(Obj);
16
2895
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 will stay alive. Is this correct? If this is correct, I've got a problem. Let's say I've got an object Customer that has an PurchaseList (Collection) of Purchase objects. Now, these Purchase objects were pulled from a datasource Datasource. The...
17
2587
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 something with b //possibly store in a list
6
2251
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 contains a pointer to int values as a filed. In the definition (implementation) of constructor I use this pointer to create table of int values with the new operator. The number of elements of the table is provided by the user during execution of the...
10
8151
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 and excel is closed. But there are some commented lines: //xlSeries.XValues = xlWs.get_Range("B2", "B4"); // makes com objects, but which...
0
8611
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9170
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8904
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6531
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5867
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4624
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2341
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.