I have a situation like below in which I have some problem in freeing the memory. Basically I have a vector that holds pointer to objects from classA. I have another classB that store the pointer of classA from the vector. It would make use of the objB. The problem is I need to operate on the vector to erase the pointer.
So how could I free the memory of objA as in the description below?
pass by value the objA and make a copy in objB?
delete the pointer to objA in the destructor of objB? ( I would not want to do that as well because it is only when the logic is fulfilled, I need to erase the pointer and delete the object. Otherwise, I would like to keep the object.
vectorA< classA*> vt; .... storing multiple pointer to objects to vector
classB* objB = new classB(vt.at(pos));
some logic performed. If it is fulfilled, need to erase a particular object from vector.
vt.erase(pos); ==> how do I free the memory if I deleted the pointer here? I could not call delete here because objB might need to use the objA later.
classB::classB(classA* objA){ this->objA = objA; }
So there any way to free the memory using Normal pointer as of the case above? As some of the replies directed me to using smart pointer, my question is are there any penalty imposed on the performance by using smart pointer? (like C# indeterministic garbage collector is not really ideal for performance critical application).