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

Confused about gcnew and object lifetime

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
SYMBOL()
{
name = nullptr;
};
~SYMBOL()
{
if (name != nullptr)
{
delete name;
}
}

//Data
String^ name;
UInt32 relativeAddress;

// Methods
virtual Int32 CompareTo(Object^ obj)
{
MODULE^ mod = dynamic_cast<MODULE^>(obj);

return relative.CompareTo(mod->relativeAddress);
}
};

In the example above, the class is used to create a List
(System.Collections.Generic.List) of SYMBOL objects. For each addition
to the list (symbols), I do a gcnew of a SYMBOL object, add pass the
gcnew'ed handle to the List.Add method. I also do a gcnew of a String
object, and assign the gcnew'ed handle to the name member of the SYMBOL
object. As you can see, I am explicitly deleting the String object in
the SYMBOL destructor. Is this correct?

The other case is this:

void PARSER::ReadLogFile(void)
{
FileInfo^ logFileI = gcnew FileInfo(logFilename);
FileStream^ logFileS = logFileI->OpenRead();
logData = gcnew array<Byte>((int)logFileI->Length);
int bytesRead = logFileS->Read(logData, 0, (int)logFileI->Length);

delete logFileI;
// TODO: Add some error checking, mechanism for returning 0-length
array on error
}

As you can see, I'm manually deleting the gcnew'ed FileInfo. Is this
correct?

Sorry if this is a basic question, but the info I've found on the net
is not consistent. In one place I found an "expert" saying that
"objects created with gcnew should never be deleted." However, that
doesn't make sense to me. I understand that after the app exits,
objects on the managed heap will eventually be gc'ed, but to be
completely right about memory management, shouldn't I be deleting
objects that I create?

Thanks,
-Chris

Mar 20 '06 #1
15 8253
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:


Yes,

The way I understood it you have to manually delete the object.
C++/CLI != C#

There was a very lively discussion a month ago about this topic in this very
newsgroup (Destructor: not gauranteed to be called?):
http://groups.google.nl/group/micros...c2007f860c0a46

It had a lot of input from lots of experienced people.
The thread makes a good read if you want to know more about this topic.

--

Kind regards,
Bruno.
br**********************@hotmail.com
Remove only "_nos_pam"
Mar 21 '06 #2
Bruno van Dooren wrote:
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:

Yes,

The way I understood it you have to manually delete the object.
C++/CLI != C#


C# and C++/CLI have equivalent GC AFAIK. The problems with C++/CLI GC
are the exact same ones as those of C# (and Java for that matter).
There was a very lively discussion a month ago about this topic in this very
newsgroup (Destructor: not gauranteed to be called?):
http://groups.google.nl/group/micros...c2007f860c0a46

It had a lot of input from lots of experienced people.
The thread makes a good read if you want to know more about this topic.


I think you missed the point (not that I read the whole thread) - you do
not have to call delete if you don't care whether your destructor runs
or not in a timely manner (or even ever). This is true for Java (my area
of GC familiarity), C# and C++/CLI. It is only if the object in question
has a finalizer/destructor that frees up a non-memory resource, such as
a DB connection, socket or file, that you should manually "delete" it in
order to guarantee timely clean up.

Basically, the GC is adept at recycling memory when it gets low, but it
obviously doesn't know which objects to finalise in order to recycle
other resources, such as db connections. For example, you could end up
running out of available connections even though you only have 1
"reachable" db connection object, since the GC hasn't bothered to
finalize your old connections (presumably because it isn't low on memory).

Essentially, GC frees you from manual memory management but not from
manual resource management.

Tom
Mar 21 '06 #3
> I think you missed the point (not that I read the whole thread) - you do
not have to call delete if you don't care whether your destructor runs
or not in a timely manner (or even ever). This is true for Java (my area
of GC familiarity), C# and C++/CLI. It is only if the object in question
has a finalizer/destructor that frees up a non-memory resource, such as
a DB connection, socket or file, that you should manually "delete" it in
order to guarantee timely clean up.


Hi Tom,

I did not miss that point. In the thread I linked to someone already
mentioned that the
GC will eventually (whenever it is in the mood - if ever - as you say)
perform the garbage collection.
However, since lots of classes involve physical resources you have to manually
release them if you want to have some determinism in it to prevent resource
problems.

Since everything in my programming background revolves around some resources
or other,
The GC cannot solve any of my problems. I care as much about resources as
about memory.

Your remark reminded me that this is also true for C#. I just don't use C#
for anything
other than some test programs and private applications, which is why I
didn't think of it.

I agree that I should have been more specific.
Saying 'You have to do it manually If you want to be sure that it is released'
would have been much more precise.

--

Kind regards,
Bruno.
br**********************@hotmail.com
Remove only "_nos_pam"
Mar 21 '06 #4
So maybe I didn't make it clear why I wanted the questions answered.
I'm not worried about running out of resources, and therefore wanting
to make sure that I'm explicility releasing them, I'm wanting to make
sure that resources aren't going to be GC'ed before I'm done with them.
I guess a better general question would be - does the GC ever
automatically release (delete) an object while the process owning the
object is still active?

Thanks,
-Chris

Mar 21 '06 #5
Here's some info I found on MSDN that provides some insight into this:

Code authored in Visual C++ and compiled with /clr will run a type's
destructor for the following reasons:

* If an object created using stack semantics goes out of scope. For
more information, see C++ Stack Semantics for Reference Types.
* If an exception is thrown during the object's construction.
* If the object is a member in an object whose destructor is
running.
* If you call the delete Operator (C++) on a handle (^ (Handle to
Object on Managed Heap)).
* If you explicitly call the destructor.

Mar 21 '06 #6
> So maybe I didn't make it clear why I wanted the questions answered.
I'm not worried about running out of resources, and therefore wanting
to make sure that I'm explicility releasing them, I'm wanting to make
sure that resources aren't going to be GC'ed before I'm done with them.
I guess a better general question would be - does the GC ever
automatically release (delete) an object while the process owning the
object is still active?


That should never happen.

Garbage collection only happens if all gc handles to an object on the
managed heap are out of scope. From that time on the GC is free to Dispose of
them at its own leisure.

Until that time your objects are safe.

--

Kind regards,
Bruno.
br**********************@hotmail.com
Remove only "_nos_pam"
Mar 21 '06 #7
ce********@qualnetics.com wrote:
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:
There's no need to invoke delete in either of the cases that you cited.
However, you touched on a case where you should call delete or use a
stack-allocated object:
void PARSER::ReadLogFile(void)
{
FileInfo^ logFileI = gcnew FileInfo(logFilename);
FileStream^ logFileS = logFileI->OpenRead();
logData = gcnew array<Byte>((int)logFileI->Length);
int bytesRead = logFileS->Read(logData, 0, (int)logFileI->Length);

delete logFileI;
// TODO: Add some error checking, mechanism for returning 0-length
array on error
}


Here, you should be calling delete on logFileS, or at least executing
logFileS->Close() to make sure that the Win32 file handle is closed
deterministicly. Otherwise, that file will remain open (and locked) until
the FileStream object is collected. Unfortunately, the System.IO classes
don't implement copy constructors, so it's not possible to create them as
stack-based objects, so you're left with something like this:

void ReadLogFile(void)
{
FileInfo^ logFileI = gcnew FileInfo(logFilename);
FileStream^ logFileS = logFileI->OpenRead();
try
{
logData = gcnew array<Byte>((int)logFileI->Length);
int bytesRead = logFileS->Read(logData, 0, (int)logFileI->Length);
}
finally
{
delete logFileS;
}
}
-cd
Mar 21 '06 #8
I'm confused by your example:

* You do not delete the FileInfo object gcnew'ed
* You do delete the FileStream object returned from the FileInfo object

If any .NET API returns an object handle, if you want deterministic
resource cleanup should the caller of the API eventually call delete on
the object's handle?

For example, if I do { String^ newText = oldText->Remove(0, someChars)
}, is the API actually doing a gcnew of a String object, and therefore
is it now my responsibility to delete that object (or leave it up to
the GC to delete the object at some point).

Thanks,
-Chris

Mar 21 '06 #9
> * You do not delete the FileInfo object gcnew'ed
FileInfo only contains data. It does not hold resources. Therefore it does
not really matter how long it takes before it is GC'ed. At least from a
resource point of view.
* You do delete the FileStream object returned from the FileInfo object The filestream holds a resource (the file handle) so if it is not deleted
explicitly, it will be left open until the GC decides to delete it.
If any .NET API returns an object handle, if you want deterministic
resource cleanup should the caller of the API eventually call delete on
the object's handle? Yes.
For example, if I do { String^ newText = oldText->Remove(0, someChars)
}, is the API actually doing a gcnew of a String object, and therefore
is it now my responsibility to delete that object (or leave it up to
the GC to delete the object at some point).

Indeed.

--

Kind regards,
Bruno.
br**********************@hotmail.com
Remove only "_nos_pam"
Mar 21 '06 #10
Another example ... if I have a function like I have listed below, do I
need to delete each object returned from StreamReader.ReadLine? If I do
not delete each object, and I end up parsing a very large text file,
how will that affect the GC heap? Is the compiler / runtime smart
enough to see that I'm no longer storing a handle to the object (since
I've stored the handle of a new object in the original container), so
it eventually free's the objects even though the program is still
running (if it needs to do a gc)? Same line of question as previous
post ... if I want deterministic usage of the GC heap, is it safe (or
even proper) to go ahead and call delete on each object returned from
ReadLine?

Thanks,
-Chris

void MODULE::LoadSymbolTable(void)
{
StreamReader^ mapFile = gcnew StreamReader(filename);
String^ line;

while (line = mapFile->ReadLine())
{
// process the line
}

mapFile->Close();
delete mapFile;
}

Mar 21 '06 #11
Chris Edgington wrote:
For example, if I do { String^ newText = oldText->Remove(0,
someChars) }, is the API actually doing a gcnew of a String object,
and therefore is it now my responsibility to delete that object (or
leave it up to the GC to delete the object at some point).


Keep in mind - delete applied to a managed object does nothing unless the
object is IDisposable, in which case it calls Dispose() on it. For objects
like System::String, System::IO::FileInfo, etc., calling delete has no
effect whatsoever. The memory will be reclaimed only when the GC gets
around to it.

For an object such as System::IO::FileStream, which is IDisposable, calling
delete on it results in the unmanaged resources in controls being freed,
which is what you wanted.

-cd
Mar 21 '06 #12
Chris Edgington wrote:
Another example ... if I have a function like I have listed below, do
I need to delete each object returned from StreamReader.ReadLine?
No.
If
I do not delete each object, and I end up parsing a very large text
file, how will that affect the GC heap?
It will grow larger and eventually trigger a collect. This will be a Gen0
collect though, which is very fast.
Is the compiler / runtime
smart enough to see that I'm no longer storing a handle to the object
(since I've stored the handle of a new object in the original
container), so it eventually free's the objects even though the
program is still running (if it needs to do a gc)?
Yes. The CLR implements 100% accurate tracking of the reachability of
memory. The instant you assign a new value to line, the old value is
available for collection. Reachability is based on the concept of roots -
static variables are roots, as are all of the object references on the
stacks of all threads that have entered the CLR, plus any roots that have
been explicitly created (e.g. through the gcroot<T> template). If it's not
possible to reach an object by tracing references starting at a root then
the object is available for collection.
Same line of
question as previous post ... if I want deterministic usage of the GC
heap, is it safe (or even proper) to go ahead and call delete on each
object returned from ReadLine?
Safe and pointless. Since System::String isn't IDisposable, calling delete
on it will have no effect at all.

Thanks,
-Chris

void MODULE::LoadSymbolTable(void)
{
StreamReader^ mapFile = gcnew StreamReader(filename);
String^ line;

while (line = mapFile->ReadLine())
{
// process the line
}

mapFile->Close();
delete mapFile;
}


-cd
Mar 21 '06 #13
Wow. I'm from the device driver / embedded world, where you need to be
diligent about freeing even the smallest allocation. Your explanations
have answered all my questions, but also created many more. Are there
any books written on how this whole GC stuff works in the CLR?

As a side note - this almost seems like it would have the tendency to
create "sloppy" programmers - allocating objects willy nilly, depending
on the system to clean up. Or, maybe some would say that many
programmers are sloppy in the first place, and this type of system
exists to protect the platform from those programmers ;)

Thanks for the quick responses.

-Chris

Mar 21 '06 #14
Chris Edgington wrote:
As a side note - this almost seems like it would have the tendency to
create "sloppy" programmers - allocating objects willy nilly, depending
on the system to clean up.


In a system that was designed to handle that it's OK. It's a different
story when those programmers later go to an unmanaged environment,
whether they can adapt.

But even in the managed world, there are two issues with this "the
system will clean up for me" attitude. First, resources still need to be
manually disposed. Being sloppy there means scarce system resources may
remain locked indefinitely. Not closing a file is often not such a big
problem, but not closing a mutex is disastrous. IMO, it's not
immediately clear which object is a resource, and it puts an extra
pressure on the programmer. There's no quick way that I know of telling
whether an object needs deterministic destruction or not. GC makes
programmers sloppy in a way that they no longer care and may not even
deallocate resource type objects. This is a much bigger problem, as far
as I can tell, than being sloppy with memory allocations. In fact, in
..NET you can't deallocate memory, only the system can, but you still
MUST deallocate resources. In native C++ there is stack syntax that
always works, and boost::shared_ptr that always guards everything
properly. Ironically, .NET makes you think even harder than a C
programmer, because for every object allocated you have to look up the
help to decide whether it needs to be deleted or not. It makes me
paranoid -- did I miss something that really needs destruction? What
happens if a class doesn't store any resources today, but it may in the
future? That class will be used in 100s of places, without the proper
deallocation. Later a resource will be added for that class, and all of
a sudden every existing code becomes invalid. So classes that MAY behave
as a resoruce should be designed to be a resource from day 1. These
issues make you think just as hard as if you didn't have GC.

What's even worse is that .NET doens't have all the tools to make
guaranteed proper deletion of resources. The unmanaged world has slowly
evolved during the past several decades, and we finally have reference
counted smart pointers and deterministic destruction with very solid
guarantees and strong, stable libraries that really makes you forget
about manual deletion of everything. .NET was designed with the goal
that you no longer have to care about any of this stuff, which is not
entirely true. Maybe 99% of your objects never need to be deleted, but
that remaining 1% is as critical as ever was. So programmers still have
to be alert.

The second (completely different) aspect is that just because all memory
is guaranteed to be deleted it doesn't mean you can't have leaks
anymore. You can still have serious memory leaks even if all your
objects are always deterministically destructed. It's as simple as
forgetting about your items stored in containers. You no longer need
them, but they may still reside in some collection in the memory. If you
forget about removing unused objects from lists, maps, sets, hashed
storages, they occupy precious memory, and it's virtually the same
effect as a memory leak. Even worse, no memory leak detector ever finds
that. These kinds of problems are not automatically solved by any type
of garbage collection or reference counting. There's no magic solution
that enitrely protects the system from sloppy programmers.

Once I had a huge lookup table to speed up calculations, and I forgot to
make it a static member. So every instance of my classes had its own
copy of the table, and systems with 256MB RAM ran out of memory quickly.
No leak detector found anything, and yet my application's memory
consumption was growing well beyond the expected limits. Once again,
there was nothing that didn't get properly freed, and yet I had memory
issues due to a human error.

Tom
Mar 21 '06 #15
Chris Edgington wrote:
So maybe I didn't make it clear why I wanted the questions answered.
I'm not worried about running out of resources, and therefore wanting
to make sure that I'm explicility releasing them, I'm wanting to make
sure that resources aren't going to be GC'ed before I'm done with
them. I guess a better general question would be - does the GC ever
automatically release (delete) an object while the process owning the
object is still active?


You've unconvered a very interesting problem. Short answer is that the CLR
will never collect a managed object while before it has finished use. The
problem is when a managed object represents something else (like an OS file
handle, pointer to a buffer on the native heap, etc.) If the managed object
no longer has references to it, but the resource it manages is still being
used (because it didn't properly abstract the resource), the GC will collect
the manage object which will then clean up the resource.

For more info on this, check out:
http://blogs.msdn.com/cbrumme/archiv.../19/51365.aspx

To solve this in C++, just call delete on the handle to the managed object.
The call to destructor will keep the object alive until it is no longer
needed.

Cheerio!
Brandon
Mar 22 '06 #16

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

Similar topics

5
by: Haoyu Zhang | last post by:
Dear Friends, Python assignment is a reference assignment. However, I really can't explain the difference in the following example. When the object is a list, the assignment seems to be a...
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...
5
by: Bit byte | last post by:
I have the following methods: static void Foo::setBar(const Bar*) ; //store a copy of Bar static const Bar* Foo::getBar(void) const ; //return an UNMODIFIABLE ptr to our internal copy In...
3
by: hedbonker | last post by:
OK - I am new to .net C++. Trying to write a simple app that creates an XML output file based on some values that a user puts in a form. After looking in the help, the sample code provided was...
0
by: pkolinko | last post by:
Hi everyone, I am writing a small low level embedded USB application using C++/CLI windows Forms. I am sort of new to the C++/CLI and having trouble understanding what happens in this very...
0
by: whm | last post by:
The page http://msdn2.microsoft.com/en-us/708fb7c4(VS.80).aspx has this code: ServiceController^ sc = gcnew ServiceController; if ( sc ) { ... Section 15.4.6 in ECMA 372 (the C++/CLI spec)...
3
by: nagashre | last post by:
class A { public: A():a(0), b(0){} handleMyMsg( char* aa, char*bb); private: processMessage();
1
by: dogbert1793 | last post by:
Hello, Does one need to delete pointers allocated with gcnew? Or does the garbage collector do it? I always though the garbage collector did it, but I saw this example today on MSDN: ...
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; }...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.