Re: Destructor that implies another destructor call (using new/delete)
- From: chrisisid@xxxxxxxxx
- Date: Thu, 5 Mar 2009 09:16:19 -0800 (PST)
On Mar 5, 9:57 am, "Giovanni Dicanio"
<giovanniDOTdica...@xxxxxxxxxxxxxxxxx> wrote:
[...]Trying to solve a memory allocation/deallocation issue in VC6. Have an
object A which, when initialized, allocates within itself objects of
type X, and X also internally allocates a few simple arrays
dynamically.
[...] the destructors as built are not
handling the 'nested' allocations, it appears. Anyone have ideas, or
good reference material? Much thanks
I mean: in a C++ program I would use modern C++ container classes (like
std::vector, instead of new[]) and smart pointers, as Doug already
suggested.
Just as an addition of previous sample code, I thought it might be useful
for you OP to post a version of that code that uses modern C++ classes like
std::vector and shared_ptr.
Note that thanks to both these classes the code complexity is reduced.
For example: there is no need to explicitly delete the arrays allocated with
new[] using delete[], thanks to std::vector destructor. The deletion is
implicit: the destructor of the class that has std::vector data members will
automatically call the std::vector destructor, so each std::vector instance
will be automatically cleaned up.
Moreover, thanks to "vector of shared_ptr", there is no need to loop through
the vector items, deleting every single pointed object, and then deleting
the whole vector; the synergy of both vector and shared_ptr allows you to
just do nothing. The power and elegance of STL classes will do proper
cleanup of both the pointed objects and the vector storing the (smart)
pointers automatically.
For more information about shared_ptr, you may want to consider this post on
VC++ Team Blog:
http://blogs.msdn.com/vcblog/archive/2008/02/22/tr1-slide-decks.aspx
Note that if you use VS2008 with SP1, you will have shared_ptr ready "out of
the box".
Instead, if you use a previous version like VC6, you should use Boost
implementation.
Giovanni
Test_ModernStyle.cpp
2KViewDownload
While trying to formulate the question, keeping the idea clear was a
challenge. Thanks so much for the detailed help. What you posted in
sample1, Giovanni, is exactly the scenario I'd hoped to employ but
couldn't for some (likely already clarified here) reason. The first
workaround was implented a year ago as a quick mod to get things
ported, but then realized on picking the project back up recently that
there were memory cleanup issues. The compiler was giving issues with
the assignment you have:
// Create the objects
const int xInitParameter = 2;
for (int i = 0; i < n; i++)
{
m_objX[i] = new X(xInitParameter);
}
This didn't work for me directly, instead I tried adding an explicit
assignment to class X (operator = )
and created temp objects in a similar loop and assigned them to m_objX
[i]. The assignment handled allocating the arrays for objX, and I was
happily oblivious as to how they might get deleted..
I see you are employing a copy constructor, as Doug had also
mentioned. Sorry for my rusty skills, but
what does it mean to 'ban class copy' etc by using this explicit
constructor? Also unfamiliar with keyword 'Explicit' used.
// Ban class copy declaring private copy ctor and operator=
//
private:
X(const X &);
X & operator=(const X &);
when I get back to that pc after work, will try variations of what you
presented. Am trying to avoid some overhead with STL here as it is
heavy processing, otherwise the modern approach is fantastic.
Many thanks, this all makes sense.
.
- Follow-Ups:
- Re: Destructor that implies another destructor call (using new/delete)
- From: Giovanni Dicanio
- Re: Destructor that implies another destructor call (using new/delete)
- References:
- Destructor that implies another destructor call (using new/delete)
- From: chrisisid
- Re: Destructor that implies another destructor call (using new/delete)
- From: Giovanni Dicanio
- Re: Destructor that implies another destructor call (using new/delete)
- From: Giovanni Dicanio
- Destructor that implies another destructor call (using new/delete)
- Prev by Date: Re: MsgWaitForMultipleObjects() instead of SleepEx() with WSARecv() overlapped
- Next by Date: Re: Destructor that implies another destructor call (using new/delete)
- Previous by thread: Re: Destructor that implies another destructor call (using new/delete)
- Next by thread: Re: Destructor that implies another destructor call (using new/delete)
- Index(es):
Relevant Pages
|