class A : public Parent
{
privte:
A* next;
A* previous;
public:
void setNext(A*);
void setPrevious(A*);
};
..
int main()
{
A* obj1 = new A();
A* obj2 = new A();
A* obj3 = new A();
obj1->setNext(obj2);
obj2->setNext(obj3);
obj3->setNext(nullptr);
obj1->setPrevious(nullptr);
obj2->setPrevious(obj1);
obj3->setPrevious(obj2);
return 0;
}
if I delete obj1.. obj2 and obj3 get deleted auotmatically. Why? Is this a memory leak? Are they actually deleted? I don't understand such behaviour. Shouldn't there be a delete for every new?
Can you give the actual class definition? What you have there wouldn't compile because an object A cannot contain itself or else there would be an infinite amount of A's.
Unless you are also calling delete somewhere else we can't see in class's member functions, obj2 and obj3 are not being deleted. You are right in saying that there should be a delete for every new.
Is doing delete obj1; delete obj2; delete obj3; giving you an error or something?
I would use a program like valgrind, or if you don't feel like setting that up, put a big for loop around the main code and see how your memory usage goes up as the lifetime of the program increases.
the project is too big to copy paste everything I hope this gives a good idea about the problem. The first time removeEventChainFromActiveStimuli() is called the object is deleted, the next time I get an unhandled exception. When I debug, after delete all the other instances on the vector become NULL :(
I like RAII approach, i.e. some wrappers around naked pointers, I mean so-called smart pointers to be sure memory is not leaked. If are not sure about your code just check it using tools like Deleaker (choose any).