[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Finalizing an object

Posted on 2004-04-06
6
Medium Priority
?
262 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 80 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses
Course of the Month19 days, 17 hours left to enroll

873 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