Re: Object destruction

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



"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


.



Relevant Pages

  • Re: Object destruction
    ... MyObject * myObj; ... myObject is destroyed when it goes out of scope in main. ... destructor (or, in a sense, it has a trivial destructor that does ...
    (microsoft.public.vc.language)
  • Re: Overcoming C# limitations
    ... You might as well have defined a CString and have a ... "hidden" destructor as you call it run when the function leaves. ... This would have been useful since I could use this macro in any scope ... knowing that the destructor of MyObj will be called at ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Overcoming C# limitations
    ... This would have been useful since I could use this macro in any scope ... knowing that the destructor of MyObj will be called at ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Objects -- Instances vs. References
    ... Now, allowing for myObj of class MyObject being element 0 in testVec, in ... Source code is only what you type in. ... MyObject myObj = new MyObject; ... creates a new object with a unique reference floating in memory and puts ...
    (comp.lang.java.programmer)
  • Re: Objects -- Instances vs. References
    ... that the compiler would just keep track of the reference and not need to ... MyObject myObj; ... it just tells the compiler to reserve a 32-bit ... That tells the JVM to create a new instance of class MyObject. ...
    (comp.lang.java.programmer)