delete operator

I have a question regarding delete operator. In case there are 3 instances each holding the next/previous instance how to delete it?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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?
Last edited on
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.
Last edited on
So I have a class TimingEventChainInstance somthing like this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class TimingEventChainInstance {
		private:
			const TimingEventOccurrence* stimulus;
			const TimingEventOccurrence* response;
			TimingEventChain* eventChain;

			TimingEventChainInstance* predecessorOrParent;

			TimingEventChainInstance* parent;
			TimingEventChainInstance* predecessor;
			TimingEventChainInstance* successor;
			TimingEventChainInstance* previousRoot;

...
...
};


and from another class called TimingEventChain i try to delete the instances

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
void TimingEventChain::deleteAndCleanPreviousInstance(TimingEventChainInstance* newInstance)
	{
		TimingEventChainInstance* previousInstance = newInstance->getPreviousRoot();
		if (previousInstance == nullptr)
		{
			return;
		}

		int64_t prevInstanceNo = previousInstance->getInstanceNo();
		newInstance->setInstanceNo(prevInstanceNo);
		std::vector<TimingEventChain*> segments = this->getSegments();
		TimingEventChainInstance* parentInstance = previousInstance;

		for (size_t i = 0; i < segments.size(); i++)
		{
			previousInstance = previousInstance->getSuccessor();
			if (previousInstance != nullptr)
			{
				segments.at(i)->removeEventChainFromActiveStimuli(previousInstance, true);
			}
		}
		this->removeEventChainFromActiveStimuli(parentInstance, true);
	}

	void TimingEventChain::removeEventChainFromActiveStimuli(TimingEventChainInstance* instance, bool deleteInstance)
	{
		if (responseConsideration != "last" && responseConsideration != "complex")
		{
			return;
		}

		uint64_t hashKey = instance->getStimulus()->calcHash();
		auto activeStimuliInstancesIt = activeStimuliInstances.find(hashKey);
		std::deque<TimingEventChainInstance*>* instances = activeStimuliInstancesIt->second;
		uint64_t size = activeStimuliInstancesIt->second->size();

		if (size > 1)
		{
			if (deleteInstance)
			{
				TimingEventChainInstance* inst = instances->front();
				delete inst;
			}
			instances->pop_front();
		}
	}


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 :(
You can do the delete stuff in the class' destructor. Or better still, use a unique pointer.

Aceix.
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).
Topic archived. No new replies allowed.