473,848 Members | 1,679 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4028
You create a new object each time.

"dm_dal" <RE************ ******@yahoo.co m> wrote in message
news:%2******** ********@TK2MSF TNGP11.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******** ******@tk2msftn gp13.phx.gbl...
You create a new object each time.

"dm_dal" <RE************ ******@yahoo.co m> wrote in message
news:%2******** ********@TK2MSF TNGP11.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.co m

"dm_dal" <RE************ ******@yahoo.co m> wrote in message
news:%2******** ********@TK2MSF TNGP11.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.co m> 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.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5
dm_dal <RE************ ******@yahoo.co m> 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.co m>
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.co m> 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.mailnul l.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.WriteLi ne ("Finalizing ");
}

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

Console.WriteLi ne ("Created first");
GC.Collect();
GC.WaitForPendi ngFinalizers();
GC.Collect();

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

GC.Collect();
GC.WaitForPendi ngFinalizers();
GC.Collect();

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

GC.Collect();
GC.WaitForPendi ngFinalizers();
GC.Collect();

Console.WriteLi ne ("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.co m>
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
1362
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 be satified. Once the criteria is satisfied how can myObject destroy itself? --
14
3174
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 it makes difference to explicitly put them to null after working with them is done - it somehow makes the GC collect them sooner, like here: void SomeFunc() { MyClass c = new MyClass();
3
1149
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 stores the object reference in a stack object, and another method that uses the stored objects in the stack object, like this: void MyClass::AcceptString(const string& myArg) { m_myStack.push(myArg); // declared as stack<string> m_myStack
2
1531
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 long as the 'Manager' object is in memory? Is there any difference in object lifetime with and without Interop?
16
2910
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 will stay alive. Is this correct? If this is correct, I've got a problem. Let's say I've got an object Customer that has an PurchaseList (Collection) of Purchase objects. Now, these Purchase objects were pulled from a datasource Datasource. The...
15
8334
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 ways, and I'm confused about the lifetime of objects and whether or not I should be calling delete. Here are two examples: ref class SYMBOL : public IComparable { public: // Constructor / destructor
12
2034
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
1730
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 object's destructor has executed (but the memory not deallocated). Specifically, the pointer must be a void*. Does that mean, the following is well defined? #include <new> class T {};
6
2704
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; } private:
0
9738
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11001
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, 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...
1
10722
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,...
0
10348
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 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...
1
7894
isladogs
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7069
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5735
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5919
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3175
bsmnconsultancy
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...

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.