In a class, there is a reference or pointer of an object of
another class embedded, and the memory is either allocated
or assigned to the new object created for this class. In
the constructor, parameters are passed to initialize the
data members and the embedded object reference to get
inialized. What measures or design change should be advised
for proper destruction and avioding memory leaks, getting
pointers dangling for the embedded object memory
allocation? Please suggest.
Answers were Sorted based on User's Feedback
Answer / ravindranath m
Embedded object is of reference type. That is, its possible
that the constructor of the container class would receive an
argument that is a reference to the embedded class object.
So, there should be a copy-constructor and/or
copy-assignment operator implemented for the embedded
object's class.
As stated in the problem statement, the embedded object ptr
is "either allocated memory" or "assigned memory".
Since the embedded object is a *reference* to an object, its
quite possible that actual object of embedded type is
constructed elsewhere and passed as an arg to the container
class. This also means that someone outside the container
class is also having a reference to the embedded class. It
implies that this embedded object can be deleted outside the
class, in which case the embedded object ptr may become a
dangling ptr.
The best approach seems to be the implementation of smart
pointer aka shared pointer, which can do reference counting
and destroy itself only when no more references are present.
see www.boost.org (or google search) for information on
shared pointers.
Is This Answer Correct ? | 2 Yes | 0 No |
Answer / mms zubeir
The most common design is to delete the object reference in
the destructor of the contained class. This will not be
helpful in cases where an exception is thrown.
There are two objects here viz, the container object and
the embedded object.
If the embedded object construction fails, no problem, it
will not cause any memory leaks since there will be no
memory allocated for it.
If the container object construction fails after
constructing the embedded object, there is a chance of
memory leak. We can use exception handling mechanism to
deallocate the object's space.
1. If the embedded object reference is not inside the try
block, then we can use that reference in the catch block to
delete in case of exceptions.
2. Suppose the embedded object reference/pointer is local
to the try block. This time, the reference to it won't be
available in catch block too. So we will not get a chance
to destroy it even in catch block also. So we need to keep
such kind of references in a global variable and will be
available anywhere in the application. So we can delete the
object reference safely.
Is This Answer Correct ? | 1 Yes | 0 No |
Answer / mms zubeir
Sorry, there is an error in my above answer. In the first
statement, it is container class, not contained class.
Is This Answer Correct ? | 0 Yes | 0 No |
Answer / som shekhar
If you are using the reference orpointer of other class as
an Aggregation (class own every thing, tight coupling) then
the destructor should take care of it, but if the reference
or pointer is taken as composition (class doesnt owwn the
pointer, loose coupling) then the class destructor should
not worry about that.
Is This Answer Correct ? | 0 Yes | 0 No |
What data encapsulation is in c++?
What is OOPs
What do you mean by funtion prototype?
List down the guideline that should be followed while using friend function.
In a class only declaration of the function is there but defintion is not there then what is that function?
Write a struct time where integer m, h, s are its members?
What do you understand by zombie objects in c++?
What is Pure Virtual Function? Why and when it is used ?
What are virtual functions and what is its use?
What is ios :: in in c++?
When is the destructor called?
Why c++ is faster than java?