Solved

Finalizing an object

Posted on 2004-04-06
6
252 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 2

Expert Comment

by:eric_duncan
Comment Utility
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
Comment Utility
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
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applicationsā€¦
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the reā€¦
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now