Double pointer,memory leak?

Hi all,

In my class I have a member int** m_yValues, witch I use
to create an dynamic allocated array/matrix:

1
2
3
4
5
6
 m_yValues = new int*[h];
    for (int i = 0; i < h; i++) {
        m_yValues[i] = new int[w];
        for (int j = 0; j < w; j++)
            m_yValues[i][j] = yValues[i][j];
    }


I create a lot of these classes, so this is my destructor:


1
2
3
4
5
6
 for (int i = 0; i < m_h; i++) {
       delete[] m_yValues[i];
        m_yValues[i] = NULL;
    }
    delete[] m_yValues;
    m_yValues = NULL;


But only this crashes ( The constructor is called once, but the destructor twice). I searched the web and found I needed a copy constructor and assigment operator. I implemented them like this:

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
IFrame::IFrame(const IFrame& obj) {
    //m_yValues = obj.m_yValues;
    m_w = obj.m_w;
    m_h = obj.m_h;

    m_yValues = new int*[m_h];
    for (int i = 0; i < m_h; i++) {
        m_yValues[i] = new int[m_w];
        for (int j = 0; j < m_w; j++)
            m_yValues[i][j] = obj.m_yValues[i][j];
    }

    m_gopSize = obj.m_gopSize;
    m_PFrames = obj.m_PFrames;
    m_blokken = obj.m_blokken;

}

IFrame& IFrame::operator=(const IFrame& obj) {
    
    m_w = obj.m_w;
    m_h = obj.m_h;
    m_yValues = new int*[m_h];
    for (int i = 0; i < m_h; i++) {
        m_yValues[i] = new int[m_w];
        for (int j = 0; j < m_w; j++)
            m_yValues[i][j] = obj.m_yValues[i][j];
    }

    m_gopSize = obj.m_gopSize;
    m_PFrames = obj.m_PFrames;
    m_blokken = obj.m_blokken;

    return *this;
}


But now I use the same amount of memory without a destructor( and copy constructor/assigment operator).

Am I doing this wrong, or is the memory leak somewhere else?

Thanks in advance!
I think the assignment operator as shown will create a memory leak (or leaks).
You should check whether the object being overwritten already has valid
data, and if so, then delete the existing arrays before creating
new ones. Something like this ?

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
IFrame& IFrame::operator=(const IFrame& obj) {
    
    //First delete  any existing arrays
    if (m_yValues != NULL)
  {
       for (int i = 0; i < m_h; i++) 
      {
           delete[] m_yValues[i];
       }

        delete[] m_yValues;
  }
    
    //Now create  some new arrays and do the copy
    m_w = obj.m_w;
    m_h = obj.m_h;
    m_yValues = new int*[m_h];
    for (int i = 0; i < m_h; i++) {
        m_yValues[i] = new int[m_w];
        for (int j = 0; j < m_w; j++)
            m_yValues[i][j] = obj.m_yValues[i][j];
    }

    m_gopSize = obj.m_gopSize;
    m_PFrames = obj.m_PFrames;
    m_blokken = obj.m_blokken;

    return *this;
}
Another possible solution would be, since you're already assigning NULL to the pointers, to make sure the pointers are valid before deleting them.
I'm not sure if this would work for you since I don't know why you're calling the destructor for the same object twice. Perhaps if you posted some more code we could give you better advice.
The code for allocation and deallocation seems to be alright and it may not be the culprit for memory leak (if any).
But I don't understand your destructor is called twice though the object is constructed once. (Even if your class is a derived and base class's destructor is virtual, the number of calls to the constructor and destructor would match).

Only thing I suspect that there could be a dangling pointer problem in your code, ie, there are a couple of pointers pointing to same IFrame object and one deletes/deallocates the object without latter's knowledge of it. When the other pointer attempts to delete the same already-dead object it could lead the program to crash with a memory error.
Since you are nullifying the m_yvalues pointer after its deallocation, any additional deletes on the same nullified pointer would not harm either, but other part of the object destruction (second time) could be a problem.

With the above posted seemingly error-free code, the problem could not be identified however I would suggest you to check the code again with a probing look.

I would run the code with only a few object allocation of IFrame and trace the construction and destruction of the object in a debugging mode, so that it could lead to the segment where it is crashing.
Check it out and post your findings here. That could give us some hints to suggest/help you.

Good luck :)
Yeah, as guestgulkan suggested, it is a good idea to check if the pointer is already pointing to some valid object and deallocate it before it points to new one.
Yes, that is a good approach though.

Good luck :)
Topic archived. No new replies allowed.