Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Finalizing an object

Posted on 2004-04-06
6
254 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

 
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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

856 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