473,473 Members | 1,513 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

re-initialize an object in same variable

Hi,

When I use same variable for a new created object, then whill the old one
destroyed by garbage collection, or do I first have to set it to null ?

eg:

SomeObject o = new SomeObject();
// and a while later:
o = new SomeObject();

or should I do:
o = null; // or call destructor? how?
o = new SomeObject();

--
rgds, Wilfried
http://www.mestdagh.biz
Nov 16 '05 #1
6 4368
Hi Wilfried,

You should call Dispose, if that class supports it, before replacing the
instance.

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
SLODUG - Slovene Developer Users Group
www.rthand.com

"Wilfried Mestdagh" <Wi**************@discussions.microsoft.com> wrote in
message news:0C**********************************@microsof t.com...
Hi,

When I use same variable for a new created object, then whill the old one
destroyed by garbage collection, or do I first have to set it to null ?

eg:

SomeObject o = new SomeObject();
// and a while later:
o = new SomeObject();

or should I do:
o = null; // or call destructor? how?
o = new SomeObject();

--
rgds, Wilfried
http://www.mestdagh.biz

Nov 16 '05 #2
There is no need to set the variable to null prior to changing it's
value. The only thing that matters to the garbage collector in this
scenario is that there no longer are any references to the instance that
was created first.

As to when the garbage collector actually collects the now unused
object, you don't know. It depends on how much more memory your
application is allocating.

As Miha said, if your object are referencing any resources that you need
to explicitly destroy at the time you stop referencing your object, you
should have the object implement IDispose and call that object's Dispose
method.

Regards,
Joakim

Miha Markic [MVP C#] wrote:
Hi Wilfried,

You should call Dispose, if that class supports it, before replacing the
instance.

Nov 16 '05 #3
Hi Miha,
You should call Dispose, if that class supports it, before replacing the
instance.


It dont supports it :( It is little class from my own. If I write a
destructor then it is not called. It is called only when I do a new into a
stack variable wich seems logical.

It does not allocate memory, but it holds some public variables calculated
at creation time. This means I have a memory leak when calling new 2 time?

How do I implement a Dispose method ? I read that a destructor cannot be
called just like that?

--
rgds, Wilfried
http://www.mestdagh.biz
Nov 16 '05 #4
Sorry, the interface to implement is named IDisposable, not IDispose as
I said.

The following article describes how to use it:

http://www.codeproject.com/csharp/IDispose.asp

Regards,
Joakim

Joakim Karlsson wrote:
There is no need to set the variable to null prior to changing it's
value. The only thing that matters to the garbage collector in this
scenario is that there no longer are any references to the instance that
was created first.

As to when the garbage collector actually collects the now unused
object, you don't know. It depends on how much more memory your
application is allocating.

As Miha said, if your object are referencing any resources that you need
to explicitly destroy at the time you stop referencing your object, you
should have the object implement IDispose and call that object's Dispose
method.

Regards,
Joakim

Miha Markic [MVP C#] wrote:
Hi Wilfried,

You should call Dispose, if that class supports it, before replacing
the instance.

Nov 16 '05 #5
What do you mean with " it holds public variables"? If these hold values,
those will be gone with the object when it gets collected. If they hold
references, those will get collected when the GC comes along too. Thers is
no leak, and there's no need to Dispose.

Willy.

"Wilfried Mestdagh" <Wi**************@discussions.microsoft.com> wrote in
message news:C4**********************************@microsof t.com...
Hi Miha,
You should call Dispose, if that class supports it, before replacing the
instance.


It dont supports it :( It is little class from my own. If I write a
destructor then it is not called. It is called only when I do a new into a
stack variable wich seems logical.

It does not allocate memory, but it holds some public variables calculated
at creation time. This means I have a memory leak when calling new 2 time?

How do I implement a Dispose method ? I read that a destructor cannot be
called just like that?

--
rgds, Wilfried
http://www.mestdagh.biz

Nov 16 '05 #6
Hi Wilfried,

I was generaly speaking. If the class implements IDisposable (Dispose
method) you should call it before loosing the instance. If it doesn't then
you don't need to call anything (well, actually depends on the class
implementation - well behaved classes would have IDisposable).
You need to implement IDisposable (and thus Dispose) only if you want to do
some clean up or close valuable resources.
In your case, you don't need to implement it because you don't hold any such
resource - that variable of yours will be collected by garbage collector
when all references to holding class are lost (when you assing new instance
to your variable o).

So, in your case, there is no need to do anything (call Dispose or set
variable to null) - just assign new instance.

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
SLODUG - Slovene Developer Users Group
www.rthand.com

"Wilfried Mestdagh" <Wi**************@discussions.microsoft.com> wrote in
message news:C4**********************************@microsof t.com...
Hi Miha,
You should call Dispose, if that class supports it, before replacing the
instance.


It dont supports it :( It is little class from my own. If I write a
destructor then it is not called. It is called only when I do a new into a
stack variable wich seems logical.

It does not allocate memory, but it holds some public variables calculated
at creation time. This means I have a memory leak when calling new 2 time?

How do I implement a Dispose method ? I read that a destructor cannot be
called just like that?

--
rgds, Wilfried
http://www.mestdagh.biz

Nov 16 '05 #7

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

Similar topics

4
by: Craig Bailey | last post by:
Anyone recommend a good script editor for Mac OS X? Just finished a 4-day PHP class in front of a Windows machine, and liked the editor we used. Don't recall the name, but it gave line numbers as...
1
by: Chris | last post by:
Sorry to post so much code all at once but I'm banging my head against the wall trying to get this to work! Does anyone have any idea where I'm going wrong? Thanks in advance and sorry again...
11
by: James | last post by:
My form and results are on one page. If I use : if ($Company) { $query = "Select Company, Contact From tblworking Where ID = $Company Order By Company ASC"; }
4
by: Alan Walkington | last post by:
Folks: How can I get an /exec'ed/ process to run in the background on an XP box? I have a monitor-like process which I am starting as 'exec("something.exe");' and, of course the exec function...
1
by: John Ryan | last post by:
What PHP code would I use to check if submitted sites to my directory actually exist?? I want to use something that can return the server code to me, ie HTTP 300 OK, or whatever. Can I do this with...
10
by: James | last post by:
What is the best method for creating a Web Page that uses both PHP and HTML ? <HTML> BLA BLA BLA BLA BLA
8
by: Beowulf | last post by:
Hi Guru's, I have a query regarding using PHP to maintain a user profiles list. I want to be able to have a form where users can fill in their profile info (Name, hobbies etc) and attach an...
1
by: joost | last post by:
Hello, I'm kind of new to mySQL but more used to Sybase/PHP What is illegal about this query or can i not use combined query's in mySQL? DELETE FROM manufacturers WHERE manufacturers_id ...
2
by: sky2070 | last post by:
i have two file with jobapp.html calling jobapp_action.php <HTML> <!-- jobapp.html --> <BODY> <H1>Phop's Bicycles Job Application</H1> <P>Are you looking for an exciting career in the world of...
1
by: Brian | last post by:
I have an array like this: $events = array( array( '2003-07-01', 'Event Title 1', '1' //ID Number (not unique) ), array( '2003-07-02',
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
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...
1
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...
0
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...
0
agi2029
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,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.