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. 8 21068
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.
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.
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
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.
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
Thanks for the reply. It clears up the confusion a lot.
:-)
--------
celeong
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.
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.
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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();
|
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...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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,...
|
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,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
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...
| |