Re: Object destruction
- From: "Carl Daniel [VC++ MVP]" <cpdaniel_remove_this_and_nospam@xxxxxxxxxxxxxxx>
- Date: Thu, 23 Mar 2006 06:47:31 -0800
Igor Tandetnik wrote:
"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.
....in particular, I'd hunt for code in the destructor of a static object
elsewhere in the program that accesses myObj, since it'll be accessing
deleted memory when it runs.
-cd
.
- References:
- Object destruction
- From: Arnie
- Re: Object destruction
- From: Igor Tandetnik
- Object destruction
- Prev by Date: Re: GetCommandLine() and parsing input from a file (<)
- Next by Date: Re: GetCommandLine() and parsing input from a file (<)
- Previous by thread: Re: Object destruction
- Next by thread: Strange debugger problem
- Index(es):
Relevant Pages
|