[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Finalizing an object

Posted on 2004-04-06
6
Medium Priority
?
261 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
[X]
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
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

650 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