473,386 Members | 1,823 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Dispose and Finalize

Hi, anybody can help me with this.

I've created a singleton class, and now wants to add destructor to it.

I know we can implement the IDisposable and also overrides the Finalize
method (from the MSDN example). But is it possible that we just implement
the Dispose method, which we will call at the end of the application?

Will there be any memory leak or side effects?

Oh ya, I'm using VB.net.
Jul 21 '05 #1
8 21064
Hi,
I know we can implement the IDisposable and also overrides the Finalize
method (from the MSDN example). But is it possible that we just implement
the Dispose method, which we will call at the end of the application?

Will there be any memory leak or side effects?
You should perform manual Dispose (and implement IDisposable as well) if you
have any unmanaged resources that have to be explicitly freed (or, you
instantiate managed classes that in turn use such resources and should be
disposed). If this is the case, Dispose is the right way.

On the other hand, it is even not guaranteed that Finalize will ever be
called on an instance. Therefore, one should not rely on this method for
freeing any resources.

Memory leaks are possible only if your class or any instantiated classes
allocate memory from an unmanaged heap and then are not properly disposed.
Any memory from the managed heap will be automatically freed by the garbage
collector.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

"celeong" <bl******@tm.net.my> wrote in message
news:3f********@news.tm.net.my... Hi, anybody can help me with this.

I've created a singleton class, and now wants to add destructor to it.

I know we can implement the IDisposable and also overrides the Finalize
method (from the MSDN example). But is it possible that we just implement
the Dispose method, which we will call at the end of the application?

Will there be any memory leak or side effects?

Oh ya, I'm using VB.net.


Jul 21 '05 #2
You should put your clean-up code in the Finalize method. This method gets
called automatically when the object is garbage collected where you must
remember to call Dispose explicitly.

One note, you should technicaly always implement IDisposable when you add a
finalizer to a class but becuase this class is a Singleton, which is
designed to have the same lifespan as the application, implementing
IDisposable would not add any benefit.

--
Rob Windsor
G6 Consulting
Toronto, Canada
"celeong" <bl******@tm.net.my> wrote in message
news:3f********@news.tm.net.my...
Hi, anybody can help me with this.

I've created a singleton class, and now wants to add destructor to it.

I know we can implement the IDisposable and also overrides the Finalize
method (from the MSDN example). But is it possible that we just implement
the Dispose method, which we will call at the end of the application?

Will there be any memory leak or side effects?

Oh ya, I'm using VB.net.

Jul 21 '05 #3
How should I dispose of a Singleton class anyway? Any ideas?
Should I set me = nothing ??? or will the gc take care of everything.

This class also will hold a reference to an ActiveX COM object during
runtime.
Because it is an unmanaged object, I should release the reference to it
during finalization.
Or shouldn't I?

--------
celeong
Esteem Innovation
"Rob Windsor" <rw******@NO.MORE.SPAM.bigfoot.com> wrote in message
news:ep*************@tk2msftngp13.phx.gbl...
You should put your clean-up code in the Finalize method. This method gets
called automatically when the object is garbage collected where you must
remember to call Dispose explicitly.

One note, you should technicaly always implement IDisposable when you add a finalizer to a class but becuase this class is a Singleton, which is
designed to have the same lifespan as the application, implementing
IDisposable would not add any benefit.

--
Rob Windsor
G6 Consulting
Toronto, Canada

Jul 21 '05 #4
If you don't need to use a finalizer, then you should not as this also
causes more ( maybe un-needed) work for the GC.

--
William Stacey, DNS MVP

"celeong" <bl******@tm.net.my> wrote in message
news:3f********@news.tm.net.my...
Hi, anybody can help me with this.

I've created a singleton class, and now wants to add destructor to it.

I know we can implement the IDisposable and also overrides the Finalize
method (from the MSDN example). But is it possible that we just implement
the Dispose method, which we will call at the end of the application?

Will there be any memory leak or side effects?

Oh ya, I'm using VB.net.

Jul 21 '05 #5
When the application terminates a final garbage collection is done and all
remaining objects are destroyed. If the objects have a finalizer it will be
called. If you have an internal reference to a COM object you should clean
it up in the finalizer which can be done using the template code below.

Dim objCOM As New ComComponent
' do work
System.Runtime.InteropServices.Marshal.ReleaseComO bject(objCOM)
objCOM = Nothing

----
Rob

"celeong" <bl******@tm.net.my> wrote in message
news:3f**********@news.tm.net.my...
How should I dispose of a Singleton class anyway? Any ideas?
Should I set me = nothing ??? or will the gc take care of everything.

This class also will hold a reference to an ActiveX COM object during
runtime.
Because it is an unmanaged object, I should release the reference to it
during finalization.
Or shouldn't I?

--------
celeong
Esteem Innovation
"Rob Windsor" <rw******@NO.MORE.SPAM.bigfoot.com> wrote in message
news:ep*************@tk2msftngp13.phx.gbl...
You should put your clean-up code in the Finalize method. This method gets called automatically when the object is garbage collected where you must
remember to call Dispose explicitly.

One note, you should technicaly always implement IDisposable when you
add a
finalizer to a class but becuase this class is a Singleton, which is
designed to have the same lifespan as the application, implementing
IDisposable would not add any benefit.

--
Rob Windsor
G6 Consulting
Toronto, Canada


Jul 21 '05 #6
Thanks for the reply. It clears up the confusion a lot.
:-)

--------
celeong
Jul 21 '05 #7
Rob, are you sure about putting clean up code in the finalize method? Since
it is possible that finalize may not run in a timely manner (and for some
apps, not at all [theoretically]), why would you rely on it to clear up
memory recourses? I had always been under the assumption that clean up code
should be placed in the Dispose method because you can predict when dispose
will happen as well as cause it to happen.

If I put clean up code in finalize, I may have objects sitting around much
longer than they need to.

-Scott
"Rob Windsor" <rw******@NO.MORE.SPAM.bigfoot.com> wrote in message
news:ep*************@tk2msftngp13.phx.gbl...
You should put your clean-up code in the Finalize method. This method gets
called automatically when the object is garbage collected where you must
remember to call Dispose explicitly.

One note, you should technicaly always implement IDisposable when you add a finalizer to a class but becuase this class is a Singleton, which is
designed to have the same lifespan as the application, implementing
IDisposable would not add any benefit.

--
Rob Windsor
G6 Consulting
Toronto, Canada
"celeong" <bl******@tm.net.my> wrote in message
news:3f********@news.tm.net.my...
Hi, anybody can help me with this.

I've created a singleton class, and now wants to add destructor to it.

I know we can implement the IDisposable and also overrides the Finalize
method (from the MSDN example). But is it possible that we just implement the Dispose method, which we will call at the end of the application?

Will there be any memory leak or side effects?

Oh ya, I'm using VB.net.


Jul 21 '05 #8
Scott,

Actually, it is recommended to implement both Dispose and Finalize,
especially if you use unmanaged resources. With this pattern, you have much
more guarantee that the resources will be freed if the calling code haven't
called Dispose.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

"Scott M." <s-***@badspamsnet.net> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Rob, are you sure about putting clean up code in the finalize method? Since it is possible that finalize may not run in a timely manner (and for some
apps, not at all [theoretically]), why would you rely on it to clear up
memory recourses? I had always been under the assumption that clean up code should be placed in the Dispose method because you can predict when dispose will happen as well as cause it to happen.

If I put clean up code in finalize, I may have objects sitting around much
longer than they need to.

-Scott
"Rob Windsor" <rw******@NO.MORE.SPAM.bigfoot.com> wrote in message
news:ep*************@tk2msftngp13.phx.gbl...
You should put your clean-up code in the Finalize method. This method gets
called automatically when the object is garbage collected where you must
remember to call Dispose explicitly.

One note, you should technicaly always implement IDisposable when you
add a
finalizer to a class but becuase this class is a Singleton, which is
designed to have the same lifespan as the application, implementing
IDisposable would not add any benefit.

--
Rob Windsor
G6 Consulting
Toronto, Canada
"celeong" <bl******@tm.net.my> wrote in message
news:3f********@news.tm.net.my...
Hi, anybody can help me with this.

I've created a singleton class, and now wants to add destructor to it.

I know we can implement the IDisposable and also overrides the Finalize method (from the MSDN example). But is it possible that we just

implement the Dispose method, which we will call at the end of the application?

Will there be any memory leak or side effects?

Oh ya, I'm using VB.net.




Jul 21 '05 #9

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

Similar topics

4
by: aaj | last post by:
Hi can anyone point to a good link explaining (simply) when I should use the dispose method. Sort of things I'd like to know are how does it differ from finalize in VB.net (or is it nothing...
14
by: Jonas | last post by:
Hi! I'm developing the middletiers of an ASP.NET application in VB.NET. I've got a business logic layer in which I would like to perform auditing to a database. Instead of making an auditing...
156
by: Dennis | last post by:
Ok, I'm trying to dispose of every object that I create that has a dispose method based on advice from this newsgroup. However, I'm not sure how to dispose of the following object that was created...
12
by: Joe Abou Jaoude | last post by:
hi, I have a component that uses a database connection. In the finalizer I dispose the connection because I read in msdn the following: "A type must implement Finalize when it uses...
6
by: RG | last post by:
Hello – I’m relatively new to VB and I’m not getting how to do Dispose correctly from my readings. I have an application with 3 Forms (with a lot of logic going on within each): Form1,...
4
by: Larry Lard | last post by:
I still don't really 'get' this. Why can't an object just release its resources in its Finalize method? Why when I create (eg) a Graphics object does it become *my* responsibility to say when I've...
2
by: eBob.com | last post by:
I have a user control which creates an Excel spread sheet and badly needs Dispose/Finalize. I've read up on the subject in Balena and I think I understand what is going on. But the VS generated...
4
by: BLUE | last post by:
I've read many articles including the one from Duff's blog but I've many doubts. public static myClass Instance { get { if (myClass.instance == null) myClass.instance = new myClass();
11
by: BLUE | last post by:
I've a class A that implements IDisposable interface and has a method that start a thread. In Dispose(bool) can I use the lock keyword to wait for thread exit before disposing resources it use or...
2
by: BLUE | last post by:
Since singleton classes conceptually are like static classes, the are supposed to last for the entire lifetime of the application. Starting from this point tell me if I'm wrong saying: - it make...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...

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.