473,387 Members | 1,621 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,387 software developers and data experts.

proper destruction techniques

This should be an easy question.

With all of my dataadapters, when I am done with them i do the
following:

DataAdapter da = new DataAdapter();
// do something with da
da.Dispose();

Is that the proper way to destroy an object when I am done with it? Or
should I be doing:
da = Nothing;

or a combonation of both? I want to make I am getting rid of my objects
correctly to free up memory on the server.

thanks

DKode

Nov 16 '05 #1
4 1432
Hi...

Certain classes require explicit cleanup. These classes express this need
to the world by implementing IDisposable. If a class exposes a public
Dispose() method, you should call it to clean up resources. To "help" the
garbage collector, you can explicitly set the object reference to null, as
well. Note, however, that calling Dispose() is far more important than
setting the reference to null in terms of cleaning up resources.

A common pattern that has emerged is:

using (MyClassThatNeedsDisposing obj = new MyClassThatNeedsDisposing()) {
// use object
}

This will ensure that Dispose is called on the "obj" instance and the object
will be cleaned up properly to boot! Another pattern is as follows:

Foo f;
try {
f = new Foo();
// use f
} finally {
f.Dispose();
f = null;
}

Hope this helps.

John Puopolo

"DKode" <dk****@gmail.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
This should be an easy question.

With all of my dataadapters, when I am done with them i do the
following:

DataAdapter da = new DataAdapter();
// do something with da
da.Dispose();

Is that the proper way to destroy an object when I am done with it? Or
should I be doing:
da = Nothing;

or a combonation of both? I want to make I am getting rid of my objects
correctly to free up memory on the server.

thanks

DKode

Nov 16 '05 #2
In the .NET world, the garbage collector cleans the objects for you, except
from objects in unmanaged code or GDI objects (Maybe there are more). Anyway,
if you don't need this dataset anymore, leave it alone. It will be collected
if you don't have a reference to it, e.g. you don't have a way to access it.

"DKode" wrote:
This should be an easy question.

With all of my dataadapters, when I am done with them i do the
following:

DataAdapter da = new DataAdapter();
// do something with da
da.Dispose();

Is that the proper way to destroy an object when I am done with it? Or
should I be doing:
da = Nothing;

or a combonation of both? I want to make I am getting rid of my objects
correctly to free up memory on the server.

thanks

DKode

Nov 16 '05 #3
DataAdapter da = new DataAdapter();
// do something with da
da.Dispose();

Is that the proper way to destroy an object when I am done with it? Or
should I be doing:
da = Nothing;

or a combonation of both? I want to make I am getting rid of my objects
correctly to free up memory on the server.


DataAdapter inherits from Component, which in turn implements
IDisposable and the disposable pattern (just like forms do). Component
surfaces a protected, virtual Dispose(bool) method that is called from
both IDisposable.Dispose and Finalize. DataAdapter and SqlDataAdapter
override the protected Dispose method and do some internal cleanup.

In general, as soon as you are done using a DataAdapter, or any other
class derived from Component, you should call Dispose. If a class
implements IDisposable, it is an indication by the class' designer that
you should call Dispose when you are done using instances of the class.
If you don't, its not the end of the world. The GC will call Finalize
eventually on the object. However, for optimum memory usage, IDisposable
objects should be disposed as soon as possible.

If you use interfaces frequently, often times you may not know whether
an object implements IDisposable. For example, I like to write generic
code that deals with the data interfaces (IDbConnection, IDbDataAdapter,
etc), and these interfaces don't descend from IDisposable. In this case,
you can mix a using statement with a cast.

IDbConnection connection = ConnectionFactory.Create()
using(connection as IDisposable)
{
connection.Open();
...
}

The IL generated by a using statement checks for null before calling
Dispose. If the object implements IDisposable, Dispose is called.
Otherwise, the using statement acts as a NOP.

One last gotcha: dropping a component on a form does not guarantee that
Dispose will get called for that object. Components that implement a
magical constructor (public ImageList(IContainer container)) are added
to the components container of the form, and are automatically disposed
by the boilerplate Dispose method of your derived form. However, the
data classes don't have this constructor, so you have to add a Dispose
call manually.

PS: This seems like a flaw to me. Any component with a non-trivial
dispose method should have this special constructor. Most of the
components don't.

H^2


Nov 16 '05 #4

Harold Howe wrote:
DataAdapter da = new DataAdapter();
// do something with da
da.Dispose();

Is that the proper way to destroy an object when I am done with it? Or should I be doing:
da = Nothing;

or a combonation of both? I want to make I am getting rid of my objects correctly to free up memory on the server.
DataAdapter inherits from Component, which in turn implements
IDisposable and the disposable pattern (just like forms do).

Component surfaces a protected, virtual Dispose(bool) method that is called from both IDisposable.Dispose and Finalize. DataAdapter and SqlDataAdapter override the protected Dispose method and do some internal cleanup.

In general, as soon as you are done using a DataAdapter, or any other class derived from Component, you should call Dispose. If a class
implements IDisposable, it is an indication by the class' designer that you should call Dispose when you are done using instances of the class. If you don't, its not the end of the world. The GC will call Finalize eventually on the object. However, for optimum memory usage, IDisposable objects should be disposed as soon as possible.

If you use interfaces frequently, often times you may not know whether an object implements IDisposable. For example, I like to write generic code that deals with the data interfaces (IDbConnection, IDbDataAdapter, etc), and these interfaces don't descend from IDisposable. In this case, you can mix a using statement with a cast.

IDbConnection connection = ConnectionFactory.Create()
using(connection as IDisposable)
{
connection.Open();
...
}

The IL generated by a using statement checks for null before calling
Dispose. If the object implements IDisposable, Dispose is called.
Otherwise, the using statement acts as a NOP.

One last gotcha: dropping a component on a form does not guarantee that Dispose will get called for that object. Components that implement a
magical constructor (public ImageList(IContainer container)) are added to the components container of the form, and are automatically disposed by the boilerplate Dispose method of your derived form. However, the
data classes don't have this constructor, so you have to add a Dispose call manually.

PS: This seems like a flaw to me. Any component with a non-trivial
dispose method should have this special constructor. Most of the
components don't.

H^2


Nov 16 '05 #5

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

Similar topics

5
by: Gandalf | last post by:
Hello (it's the newbie again). If I have a class class Foo{ public: Foo(){cout<<"Making"<<endl;} ~Foo(){cout<<"Destroying"<<endl;} }; void func(Foo x){}
0
by: Stephan Keil | last post by:
Hi all, consider a class class X { static X& OneX() { static X sgl1; return sgl1; } static X& TheX() { static X sgl2(OneX()); return sgl2; } }; and two source files with global variables
9
by: plahey | last post by:
I have been dabbling in Python for a while now. One of the things that really appeals to me is that I can seem to be able to use C++-style RAII idioms to deal with resource management issues. ...
14
by: ToddLMorgan | last post by:
Summary: How should multiple (related) projects be arranged (structured) and configured so that the following is possible: o Sharing common code (one of the projects would be a "common" project...
6
by: Pablo | last post by:
Hello, I am writing a windows application using C++ and BorlandBuilder 6 compiler. It is an event driven program and I need to create objects of some classes written by me. One of the classes...
2
by: Dennis Jones | last post by:
Hello, I have a class that will eventually look something like this: class TTableHolder { private: boost::scoped_ptr<TSessionFSession; boost::shared_ptr<TTableFTable;
7
by: BeautifulMind | last post by:
In case of inheritence the order of execution of constructors is in the order of derivation and order of destructor execution is in reverse order of derivation. Is this case also true in case...
13
by: Chris Thomasson | last post by:
Here is some info on a C++ allocator prototype I am working on: http://groups.google.com/group/comp.lang.c++/browse_frm/thread/beeee1f61fdbb52c Any tried-and-true techniques for calculating the...
1
by: Martijn Mulder | last post by:
I am looking for a C# .NET 2.0 tutorial on animation techniques, especially the difficult subject of Invalidating() the smallest possible area on the screen and the proper way to set things up.
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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...
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,...

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.