• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 448
  • Last Modified:

delete a char *ptr array inside a struct cause memory leak error

Hi,
I'm trying to implement a destructor but I'm getting an error of memory leakage of some kind

this isn't the actual code but it is how I'm having a problem with, mainly the part of delete the dynamic array of char inside the struct.

am I doing it right?

thanks,
class
{
 public:
func();
 private:     
         struct Node
        {
            char* text;
           Node* link;
        };

Node *head;
}
//////////////////////
class::func(const string& mytext)
{
head = new Node[sizeof(Node)];
head->text = new char[strlen(mytext.c_str())];
}

~class()
{
 delete[] head->ptr; //here is the error happening
 delete head;
}

Open in new window

0
logicallayer
Asked:
logicallayer
  • 3
  • 2
1 Solution
 
evilrixSenior Software Engineer (Avast)Commented:
I can't really make sense of your code because it donesn't see to anything logical.

Rather than posting code it would help to explain what you expect the code to specifically do.
0
 
farzanjCommented:
It appears to me a link list kind of structure.  It would certainly cause a memory leak.  Before you delete the head, you have to delete all the element in the link list in a sequential fashion.  You have to use your head and get the pointer to the next links.  When all the links are deleted, then delete the head last of all.  If it is a tree, you have to do the depth first search.
0
 
logicallayerAuthor Commented:
sorry the destructor looks like this in the code bellow...

is this the right way to delete an array of char inside struct?
delete[] head->text; //here is the error happening
~class()
{
 delete[] head->text; //here is the error happening
delete head->link; //here is error too
 delete head;
}

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
evilrixSenior Software Engineer (Avast)Commented:
Ok, there are a number of issues here. To keep is simple I am going to add annotation to your code. If anything is unclear please as for clarification.

Unfortunately, I fear you are trying to mix two concepts, a dynamic array and a linked list. They are two different data structures and cannot be combined in any sensible way.

Again, can you clarify exactly what it is you are trying to do... not in a technical sense but layman's terms.
class // RX: You class needs to have a name.
{
 public:
func(); // RX: This needs a return type defined.
 private:     
         struct Node
        {
            char* text;
           Node* link;
        };

Node *head;
} // RX: You need a comma here

// RX: this prototype doesn't match the class declaration
class::func(const string& mytext) /
{

// RX: What do you expect this to do? You are creating an array of nodes the
//     number of elements will be the sizeof(Node) -- this make no real sense
head = new Node[sizeof(Node)]; 

// RX: Since mytest is clearly a std::string why not just store a std::string in node?
head->text = new char[strlen(mytext.c_str())];
}

// RX: You need the name of a destructor not the keyword class
~class()
{
 // RX: THere is no such member as ptr in Node!
 delete[] head->ptr; //here is the error happening
 delete head; // RX: given the way you currently new head this should be delete [] head (becuase head is an array of Nodes)
}

// RX: Is this a rework of the destructor? Well, you have problems way before you here
~class()
{
// RX: this should work but is not necessary if you use a std::string
 delete[] head->text; //here is the error happening

// RX: At which point did you allocate memory to ling that needs deleting?
delete head->link; //here is error too

// RX: Again, this should be delete [] head since it's an array of nodes.
 delete head;
}

Open in new window

0
 
logicallayerAuthor Commented:
thanks evilrix to bring to my attention that I'm creating an array of nodes...I missed that for some reason

I used a class as a demo example but actually I have a class name in my program...I have changed in the demo to avoid the confusion.

also I was stupidly creating a new instance of link instead of just using it as a pointer that is connecting two nodes...

please take a look at this code and let me know why my destructor is failing with
delete[] head->text;

I included more of my actual code that it might make more sense...let's say I called func few times in my main then I called the destructor...here where the error happens and when I comment out the
delete[] head->text;
then it works

thanks
class LinkedList
{
 public:
LinkedList();
~LinkedList();
void func();
 private:     
         struct Node
        {
            char* text;
           Node* link;
        };

Node *head;
Node *tail;
}
//////////////////////
void LinkedList::func(const string& mytext)
{
if(head == nullptr)
{
head = new Node;
head->text = new char[strlen(mytext.c_str())];
strcpy(head->text, mytext.c_str());
tail = head;
tail->link = nullptr;
}
else //if the first node been already created
{
Node *temp;
temp = tail;
tail = new Node;
tail->text = new char[strlen(mytext.c_str())];
strcpy(tail->text, mytext.c_str());
temp->link = tail;
tail->link = nullptr;
}
}
LinkedList::LinkedList()
{
head = nullptr;
tail = nullptr;
}
LinkedList::~LinkedList()
{
 delete[] head->text; //here is the error happening
 delete head;
}

Open in new window

0
 
logicallayerAuthor Commented:
I solved the problem,it was with
head->text = new char[strlen(mytext.c_str())];
tail->text = new char[strlen(mytext.c_str())];

I changed them to
head->text = new char[strlen(mytext.c_str()) + 1];
tail->text = new char[strlen(mytext.c_str()) + 1];

and the memory leakage was fixed...

I will give the full credit to evilrix as his answer was very productive and it did hit the spot

have a good night everyone
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now