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 21095
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.ne t.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.ne t.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.MO RE.SPAM.bigfoot .com> wrote in message
news:ep******** *****@tk2msftng p13.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.ne t.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.Releas eComObject(objC OM)
objCOM = Nothing
----
Rob
"celeong" <bl******@tm.ne t.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.MO RE.SPAM.bigfoot .com> wrote in message news:ep******** *****@tk2msftng p13.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.MO RE.SPAM.bigfoot .com> wrote in message
news:ep******** *****@tk2msftng p13.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.ne t.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******** ********@tk2msf tngp13.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.MO RE.SPAM.bigfoot .com> wrote in message news:ep******** *****@tk2msftng p13.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.ne t.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 like finalize)
Does the garbage collector automatically call the dispose method when an
object goes out of scope.
|
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 call in every method of my classes,
would it be a workable way to implement IDisposable in the base class to all
the BLL-classes and then in the Dispose method to do the audit call? Do I
then have to make sure that all uses of the BLL-classes end...
|
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 inside a method call.
dim myvar as new object1
object1.dosomethingmethod(new object2)
Note that object 2 has a dispose method but how do I dispose of it unless I
do the following:
|
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 unmanaged resources such as
file handles or database connections that must be released when the
managed object that uses them is reclaimed."
|
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, Form2, and a 3rd Form which is a dialog.
- Form 1 at the top has:
Dim frmTwo As New Form2
| |
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 finished
with it, and explcitly say so? I thought one of the points of managed
memory / garbage collection was that users of objects didn't have to
worry about tracking object lifetime?
What's the point of me saying g.Dispose at the end of my...
|
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 coded confuses me a bit.
VS generates a Dispose subroutine, but it's not obvious if it is ok to
modify it. In the New subroutine VS makes clear that you can add code to it
and tell you where to add it ("Add any initialization after the...
|
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 it's enough to pay attention when disposing
objects of type A from "consumer" classes?
Thanks,
Luigi.
|
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 no sense to implement IDisposable
- it make sense to implement Finalize if there are unmanaged resources, else
the GC will not be able to free them
- this is perhaps the only case in which a finalizer is implemented, but a
Dispose is not...
|
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...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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
|
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...
| |