473,725 Members | 2,017 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dispose not getting called

Hello All
I have an abstract class C1 with this:

public abstract class C1
{
protected bool m_Dirty;
protected override void Dispose(bool disposing)
{
if(m_Dirty)
WriteOuput();
base.Dispose (disposing);
}

protected void finalize()
{
Dispose(false);
GC.SuppressFina lize(this);
}
public abstract void WriteOuput();
}

Then I have a derived class C2 with this:

public class C2: C1
{
public override void WriteOuput()
{
if(m_Dirty)
{
try
{
m_Log.WriteXml( m_InitialData);
}
catch
{
//Ignore error if file is not valid
}
m_Dirty = false;
}
}
}

I use C2 in my code, but I don't see the parent dispose being called
since the output is never written...Am'I missing something here..??

thanks
Sunit
su********@netz ero.net
Nov 15 '05 #1
4 6729
Your finalize method should be called "~C1", not "finalize".
"Sunit Joshi" <sj****@ingr.co m> wrote in message
news:8f******** *************** *@posting.googl e.com...
Hello All
I have an abstract class C1 with this:

public abstract class C1
{
protected bool m_Dirty;
protected override void Dispose(bool disposing)
{
if(m_Dirty)
WriteOuput();
base.Dispose (disposing);
}

protected void finalize()
{
Dispose(false);
GC.SuppressFina lize(this);
}

Nov 15 '05 #2
As previously noted, you should use C++ destructor syntax for your finalize
method in C#:
protected ~C1() // note: I don't think that it matters that it's protected
vs. public.

Also, I think that your call to GC.SuppressFina lize() may be in the wrong
place. You don't want it in the finalize method -- if the finalizer is
running, it means that the GC has already called suppress finalize anyway.

Instead, put the call to suppress finalize in the public version of the
Dispose method, so that if the calling code calls dispose manually, the C1
class will not have its destructor called (no need).

For a more detailed discussion, read this:
http://msdn.microsoft.com/library/de...poseMethod.asp

"Sunit Joshi" <sj****@ingr.co m> wrote in message
news:8f******** *************** *@posting.googl e.com...
Hello All
I have an abstract class C1 with this:

public abstract class C1
{
protected bool m_Dirty;
protected override void Dispose(bool disposing)
{
if(m_Dirty)
WriteOuput();
base.Dispose (disposing);
}

protected void finalize()
{
Dispose(false);
GC.SuppressFina lize(this);
}
public abstract void WriteOuput();
}

Then I have a derived class C2 with this:

public class C2: C1
{
public override void WriteOuput()
{
if(m_Dirty)
{
try
{
m_Log.WriteXml( m_InitialData);
}
catch
{
//Ignore error if file is not valid
}
m_Dirty = false;
}
}
}

I use C2 in my code, but I don't see the parent dispose being called
since the output is never written...Am'I missing something here..??

thanks
Sunit
su********@netz ero.net

Nov 15 '05 #3
Hi,

In addition to the other comments in this thread your
abstract class must descend from the IDisposable
interface. The "Dispose Pattern" is documented in MSDN.

In the Dispose pattern your abstract {or base} class
should take a form like the code below. Descenders of
your abstract class can override the protected virtual
form of the Dispose method to add custom cleanup code.

class MyClass: IDisposable
{
public MyClass()
{
}

~MyClass()
{
Dispose(false);
}

public void Dispose()
{
Dispose(true);
GC.SuppressFina lize(this);
}

protected virtual void Dispose(bool disposing)
{
<<implement cleanup logic here>>
}
}
--Richard
-----Original Message-----
Hello All
I have an abstract class C1 with this:

public abstract class C1
{
protected bool m_Dirty;
protected override void Dispose(bool disposing)
{
if(m_Dirty)
WriteOuput();
base.Dispose (disposing);
}

protected void finalize()
{
Dispose(false);
GC.SuppressFina lize(this);
}
public abstract void WriteOuput();
}

Then I have a derived class C2 with this:

public class C2: C1
{
public override void WriteOuput()
{
if(m_Dirty)
{
try
{
m_Log.WriteXml( m_InitialData);
}
catch
{
//Ignore error if file is not valid
}
m_Dirty = false;
}
}
}

I use C2 in my code, but I don't see the parent dispose being calledsince the output is never written...Am'I missing something here..??
thanks
Sunit
su********@net zero.net
.

Nov 15 '05 #4
Typo, sorry:
it means that the GC has already called suppress finalize anyway.

I meant to say that the GC has already called finalize anyway.

"J.Marsch" <je****@ctcdeve loper.com> wrote in message
news:e4******** ******@TK2MSFTN GP12.phx.gbl... As previously noted, you should use C++ destructor syntax for your finalize method in C#:
protected ~C1() // note: I don't think that it matters that it's protected vs. public.

Also, I think that your call to GC.SuppressFina lize() may be in the wrong
place. You don't want it in the finalize method -- if the finalizer is
running, it means that the GC has already called suppress finalize anyway.

Instead, put the call to suppress finalize in the public version of the
Dispose method, so that if the calling code calls dispose manually, the C1
class will not have its destructor called (no need).

For a more detailed discussion, read this:
http://msdn.microsoft.com/library/de...poseMethod.asp
"Sunit Joshi" <sj****@ingr.co m> wrote in message
news:8f******** *************** *@posting.googl e.com...
Hello All
I have an abstract class C1 with this:

public abstract class C1
{
protected bool m_Dirty;
protected override void Dispose(bool disposing)
{
if(m_Dirty)
WriteOuput();
base.Dispose (disposing);
}

protected void finalize()
{
Dispose(false);
GC.SuppressFina lize(this);
}
public abstract void WriteOuput();
}

Then I have a derived class C2 with this:

public class C2: C1
{
public override void WriteOuput()
{
if(m_Dirty)
{
try
{
m_Log.WriteXml( m_InitialData);
}
catch
{
//Ignore error if file is not valid
}
m_Dirty = false;
}
}
}

I use C2 in my code, but I don't see the parent dispose being called
since the output is never written...Am'I missing something here..??

thanks
Sunit
su********@netz ero.net


Nov 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
3693
by: Robert | last post by:
Hello all... In my code below, the Notify Constructor and Destructor is getting called twice and it appears that a new Notify object is created on the 2nd call. The 2nd call is caused by this line below: pNotify = new EMAILnotify; that lives in the Notification Constructor. One theory is that the Notify base class is not completely constructed prior to using it in the Notification Constructor code: pNotify = new EMAILnotify;
5
7338
by: Adam Clauss | last post by:
I have a webapp which consists of several different webforms. Basically: (1) view the contents of a database (2) one is to add a new entry (3) confirms the new entry. The third form, upon confirming the entry (in a button_clicked handler) adds the entry into the database and then redirects back to (2) using Response.Redirect(..)
2
1372
by: samir dsf | last post by:
hi I had been using this datagrid for long. all well...but suddenly the itemdatabound event is not at all getting called...i dont see my links..initally it worked well here is the page_load and itemdatabound event. Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'Put user code to initialize the page here
0
1754
by: Erik | last post by:
Why isn't my update method getting called? Pasted below is an aspx from a 1.1 application I'm working on. It has two textboxes and a button for inserting data into the database, and a datagrid for editing and deleting data. When a user clicks on the "Edit" button in the datagrid, Edit() method is called and the appropriate row is changed into textboxes. And if user clicks on the "Update" button, the Update() method is fired. But if...
1
1304
by: jm.suresh | last post by:
In the following code, I could not find out why the set and get methods are not called once I set the property. .... def __init__(self): .... self._color = 12 .... def _setcolor(self,value): .... print 'setting' .... self._color = value .... def _getcolor(self):
1
3610
by: sean | last post by:
I'm trying to create "rubber-band" rectangles by overriding the OnPaint method to place rectangles on top of all graphic controls, but when I call Me.Invalidate() (when the user moves the mouse), OnPaint is not getting called... Here is the relevent code: I'm trying to create a "rubber band" rectangle effect on my form. Private Sub HighlightSectionOfTrack(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles...
3
1378
by: Abubakar | last post by:
Hi, I have a dll that gets called by an exe, I just placed a little code in its dlmain method and put a breakpoint on the code only to discover that its getting called nearly hundreds of times. I suspect each time some exported function is getting called the dllmain also gets called. Why is it happening and why should it happen? Regards, ...ab
4
3729
by: SAL | last post by:
Hello, I'm working, basically my first, AJAX page and am having a few problems. One is that the Click event for a button I have in UpdatePanel1 is not getting called. I've tried with the button both inside and outside of the updatepanel and the event doesn't get called either way. What might I be missing here? Incidently, something else, kind of weird, is happening when the button is clicked, a required field validator control in a...
2
2052
by: hgarg | last post by:
The onpaint() is getting called before calculating the required parameters by listBox1_SelectedIndexChanged function. I tried calling it at the end of this function. But of no use. Due to this issue only half of the diagram is getting painted,some lines are missing in the diagram. private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e) { string str=listBox1.SelectedItem.ToString(); string str1=null;...
0
8888
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8752
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9257
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9111
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6702
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4782
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3221
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2634
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2157
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.