Solved

Finalizing an object

Posted on 2004-04-06
6
253 Views
Last Modified: 2006-11-17
Hey people

I have an object that I would like to kill straight away instead of it sitting on the heap. My problem is that if I call dispose then its not guranteed to be killed and I have an event linked to my object from an another class which when called goes to the code for the old object still on the heap which causes some major problems.

Can you help sorry about the low points availiable for this question but I have almost run out.

Thanks

Simion
0
Comment
Question by:simion_tishler
  • 2
  • 2
  • 2
6 Comments
 
LVL 2

Accepted Solution

by:
eric_duncan earned 20 total points
ID: 10771674
It sounds like you need to explicitly remove the event handler referencing the other class as you are disposing the object. Just like you add the handler using the += syntax, you can remove it using -=. For example:

// add an event handler
b.ButtonClick += new ButtonEventHandler(onButtonAction);

// remove an event handler
b.ButtonClick -= new ButtonEventHandler(onButtonAction);

This would keep the event from calling your disposed object.

At any rate, there isn't a way to kill an object straight away in C#; the closest you can get is to call Dispose() on the object, and then call System.GC.Collect() force the garbage collection routines to kick in but even then there isn't really any guarantee that your object will be destroyed because of the generational aspect of the garbage collector.

Eric
0
 
LVL 2

Expert Comment

by:codewiz51
ID: 10771757
You can define an interface (or abstract class) that your classes can use to communicate.  Since you can only inherit from one class, but you can inherit (implement) multiple interfaces, I use interfaces.

Any class that needs to have a callback to another class implements a standard interface.  I implement a Register(object) method to keep a list of objects with callbacks (events) hooked to my instance and an Unregister() method that is called from the instance's Dispose method.  The unregister method loops through all registered objects, notifying them to unhook the event handler.  It's just a minor extenstion of the message above, but it's simple and effective and you don't have to worry about releasing an instance.  When it's Dispose() method is called, it notifies everyone in its list to unhook any events.  What's really cool, after you get used to the idea, is being able to cascade the disposal of registered objects as well.

Here's a reference, if you're interested:
Explicit Interface Implementation Tutorial
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vcwlkExplicitInterfaceImplementationTutorial.asp
0
 
LVL 1

Author Comment

by:simion_tishler
ID: 10772095
Hey guys well thanks for the comments. I will split the points between you. Not that there much to split sorry about that. I'm doing my best to get more points but no one will accept my answers.

Actually although you helped me I realised that I was playing the dangerous game of static events which was why they were not disposing with the object. Hahahah The laugh of a crazy man who spend 5 hours wondering what the hell was going on. Well at least I'm a wiser man.

Thanks

Simion
0
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
LVL 2

Expert Comment

by:eric_duncan
ID: 10773563
I hate it when that happens ;). BTW, you didn't split the points ... you might want to ask an admin to reopen the question so you can.

Eric
0
 
LVL 2

Expert Comment

by:codewiz51
ID: 10773861
It's OK.  I wasn't looking for points anyway.  Just glad he lost his insanity.  Static events?  *grin*
0
 
LVL 1

Author Comment

by:simion_tishler
ID: 10779433
Hey sorry I didn't know how to spilt the point it didn't seem to give me the option. Sorry about that.

Simion
0

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question