Re: Object destruction
- From: "Igor Tandetnik" <itandetnik@xxxxxxxx>
- Date: Thu, 23 Mar 2006 08:24:51 -0500
"Arnie" <none> wrote in message
news:OCqZmvnTGHA.4600@xxxxxxxxxxxxxxxxxxxx
Yeah, I know the code below is kind of dumb but it's not my code.
Please consider:
// This is at file scope
MyObject * myObj;
main( ...
{
boost::scoped_ptr<MyObject> myObject( new MyObject );
myObj = myObject.get();
}
myObject is destroyed when it goes out of scope in main(). Is
the destructor ever called on myObj?
myObj is a pointer, not an object of a class. It does not have a
destructor (or, in a sense, it has a trivial destructor that does
nothing).
This program creates one object of type MyObject with dynamic duration,
and one object of type scoped_ptr<MyObject> with automatic duration. At
the end of main, scoped_ptr object goes out of scope and its destructor
is automatically run. This destructor happens to deallocate the instance
of MyObject, so MyObject's destructor is run, too. myObj is a regular
dumb pointer: it used to point to MyObject instance on the heap, but now
it becomes a dangling pointer.
We're seeing some
occasional A/V's at program exit.
The program as shown is valid and should not crash. The problem lies in
the code you haven't shown.
--
With best wishes,
Igor Tandetnik
With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925
.
- Follow-Ups:
- Re: Object destruction
- From: Carl Daniel [VC++ MVP]
- Re: Object destruction
- From: Arnie
- Re: Object destruction
- References:
- Object destruction
- From: Arnie
- Object destruction
- Prev by Date: Re: GetCommandLine() and parsing input from a file (<)
- Next by Date: Re: Object destruction
- Previous by thread: Object destruction
- Next by thread: Re: Object destruction
- Index(es):
Relevant Pages
|