Adam
Adam

Reputation: 11

Memoryleak with pointer to pointer, C++

I'm trying to do a Phonehandler system for a class I'm taking, The task specifies that we use a pointer to pointer for the Phone objects in the Phonehandler-class, and that the array to store Phones has a size of two phones from the start and that it can be expanded later. My relevant(?) code is as follow:

PhoneHandler.h

Phone **phones;

PhoneHandler.cpp (constructor)

PhoneHandler::PhoneHandler()
{
this->phones = new Phone*;
*phones = new Phone[2];
}

My code includes some more things than this, but I have not written "new" at any other place so the memoryleaks are allocated in the constructor. I've made a destructor as followed:

PhoneHandler.cpp (destructor)

PhoneHandler::~PhoneHandler()
{
delete[] phones;
delete phones
}

But it's crashing at the first line in the destructor. So I need help with ether the constructor or the destructor, maybe both. Feel free to ask me for more code if it's necessary, but I think that the problem is somewhere in this code. Thanks

Upvotes: 1

Views: 54

Answers (1)

Santhosh Kumar
Santhosh Kumar

Reputation: 167

delete [] phomes wont lead to any crash.

delete phomes is causing the crash.

Upvotes: 0

Related Questions