473,326 Members | 2,438 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,326 software developers and data experts.

Object lifetime

Just a quick question regarding object scope and lifetime.

Given the following:

{
DataRow myRow = new DataRow();
...... do some stuff
{
myRow = new DataRow;
..... do some stuff
}
myRow = new DataRow;
..... do some stuff
}

Have I just created three seperate DataRow objects? Or does the assignment
just reassign the original?

dmy
Nov 16 '05 #1
7 4009
You create a new object each time.

"dm_dal" <RE******************@yahoo.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
Just a quick question regarding object scope and lifetime.

Given the following:

{
DataRow myRow = new DataRow();
...... do some stuff
{
myRow = new DataRow;
..... do some stuff
}
myRow = new DataRow;
..... do some stuff
}

Have I just created three seperate DataRow objects? Or does the assignment just reassign the original?

dmy

Nov 16 '05 #2
That's what I thought. And so the first two instances created are now
marked for deletion and are waiting for GC to process, right?

dmy
"Marina" <so*****@nospam.com> wrote in message
news:ux**************@tk2msftngp13.phx.gbl...
You create a new object each time.

"dm_dal" <RE******************@yahoo.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
Just a quick question regarding object scope and lifetime.

Given the following:

{
DataRow myRow = new DataRow();
...... do some stuff
{
myRow = new DataRow;
..... do some stuff
}
myRow = new DataRow;
..... do some stuff
}

Have I just created three seperate DataRow objects? Or does the

assignment
just reassign the original?

dmy


Nov 16 '05 #3
dmy,

You have created three new DataRow objects. The objects that were
pointed to by myRow then become eligible for GC once the assignment is made
(unless you have compiled for debug mode).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"dm_dal" <RE******************@yahoo.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
Just a quick question regarding object scope and lifetime.

Given the following:

{
DataRow myRow = new DataRow();
...... do some stuff
{
myRow = new DataRow;
..... do some stuff
}
myRow = new DataRow;
..... do some stuff
}

Have I just created three seperate DataRow objects? Or does the assignment just reassign the original?

dmy

Nov 16 '05 #4
dm_dal <RE******************@yahoo.com> wrote:
Just a quick question regarding object scope and lifetime.

Given the following:

{
DataRow myRow = new DataRow();
...... do some stuff
{
myRow = new DataRow;
..... do some stuff
}
myRow = new DataRow;
..... do some stuff
}

Have I just created three seperate DataRow objects? Or does the assignment
just reassign the original?


You've created three separate DataRow objects. If you don't pass them
around in some way which keeps a reference elsewhere, each will become
eligible for garbage collection at the time when the next one's
reference is assigned to myRow. (That doesn't mean they'll be collected
immediately, however.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5
dm_dal <RE******************@yahoo.com> wrote:
That's what I thought. And so the first two instances created are now
marked for deletion and are waiting for GC to process, right?


They're not marked for deletion - they're just *eligible* for
collection. Nothing marks objects as ready to be deleted - the garbage
collector marks things which *shouldn't* be deleted (when it runs) and
then collects everything else (in the generation). (That's a simplistic
view which ignores finalization, admittedly.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6
Jon Skeet [C# MVP] wrote:
dm_dal <RE******************@yahoo.com> wrote:
Just a quick question regarding object scope and lifetime.

Given the following:

{
DataRow myRow = new DataRow();
...... do some stuff
{
myRow = new DataRow;
..... do some stuff
}
myRow = new DataRow;
..... do some stuff
}

Have I just created three seperate DataRow objects? Or does the assignment
just reassign the original?

You've created three separate DataRow objects. If you don't pass them
around in some way which keeps a reference elsewhere, each will become
eligible for garbage collection at the time when the next one's
reference is assigned to myRow. (That doesn't mean they'll be collected
immediately, however.)


To expand on this a bit...

Strictly speaking, an object referenced by myRow might become eligible
for collection *before* it gets assigned a new object reference. That
can happen if the JIT/Garbage Collector determines that the current
myRow reference is not used at some point before the re-assignment.

For example:

DataRow myRow = new DataRow();

// a) do some stuff

object o = myRow[0]; // get column 0's value

// b) do some other stuff, but not using myRow

myRow = new DataRow;

At any point after execution of the "object = = myRow[0];" statement
(section 'b') the object referenced by myRow can be collected - the GC
doesn't need to wait until the second assignment to myRow.

Normally, this isn't a problem (and I imagine it would not be with the
DataRow object), but I've seen some problems described in the news
groups that were a result of this kind of thing (one was a mutex
intended to signal new instances of the application that never seemed to
function, and the other was a Registry value write that blew up because
the underlying handle got closed by a finalizer called too early).

These problems can be real head scratchers if you don't realize that
object lifetime can be shorter than what you'd guess. I believe that
this issue would only be a true potential problem for objects that have
a finalizer.

Anyway, this is discussed by Chris Brumme in gory detail here:

http://blogs.msdn.com/cbrumme/archiv.../19/51365.aspx
--
mikeb
Nov 16 '05 #7
mikeb <ma************@nospam.mailnull.com> wrote:

<snip>
To expand on this a bit...

Strictly speaking, an object referenced by myRow might become eligible
for collection *before* it gets assigned a new object reference. That
can happen if the JIT/Garbage Collector determines that the current
myRow reference is not used at some point before the re-assignment.


Oh yes, good catch. Here's a sample program to show this happening:

using System;

class Test
{
~Test()
{
Console.WriteLine ("Finalizing");
}

static void Main()
{
Test t = new Test();

Console.WriteLine ("Created first");
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();

Console.WriteLine ("Creating second");
t = new Test();
Console.WriteLine ("Created second");

GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();

Console.WriteLine ("Last reference to t: {0}", t);

GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();

Console.WriteLine ("End of method");
}
}

The output on my box is:

Created first
Finalizing
Creating second
Created second
Last reference to t: Test
Finalizing
End of method

I had expected that the JIT wouldn't be clever enough to spot that -
that it would just have a "first use to last use" policy, but nope -
it's really smart :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #8

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

Similar topics

2
by: Jeff Grundy | last post by:
I have a function that instanciates an object like this... Private Sub MySub() Dim myObject as new myClass End Sub My new object has a timer that fires every min waiting for some criteria to...
14
by: MuZZy | last post by:
Hi, Lately i've been (and still am) fixing some memory leaks problems in the project i just took over when i got this new job. Among the other issues i've noticed that for localy created objects...
3
by: Bob Altman | last post by:
Hi all, I have a basic question regarding holding references to objects in unmanaged C++. Suppose my unmanaged C++ class has a method that accepts a reference to an object as an argument, and...
2
by: Tim | last post by:
I have a class called Manager. In the 'Initialize' method of the Manager class, I create an instance of a COM class (say oComObject). What is the lifetime of oComObject. Will it be available as...
16
by: anonymous.user0 | last post by:
The way I understand it, if I have an object Listener that has registered as a listener for some event Event that's produced by an object Emitter, as long as Emitter is still allocated Listener...
15
by: cedgington | last post by:
I wanted to take advantage of the large set of functionality offered by the framework, so for my latest project I'm using managed C++ with .NET v2. I'm using the gcnew operator in two different...
12
by: Belebele | last post by:
Suppose that a class A depends on class B because an object of class B is passed into A's constructor. See below: class B { ... }; class A { public: A(B const& b); ... };
4
by: REH | last post by:
I've been trying to better understand the subtle rules for object lifetime. The standard says that pointers to the memory of a dynamically allocated object may be used in limited ways after the...
6
by: better_cs_now | last post by:
Hello all, class Foo {/* Details don't matter */}; class Bar { public: Bar(): m_Foo(/* Construct a Foo however it wants to be constructed */); const Foo &GetFoo() const { return m_Foo; }...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.