简体   繁体   中英

Pass by pointer, memory leak

NOTE: Initially I though that a section of code was causing a leak but that turned out to be incorrect. Hence the comments below. I have updated the question

Possible Solution: I managed to solve this. What I did was move all initializations to the head of the function. Listed them out on a piece of paper and scratched em off as I came across the corresponding delete code. This way 1. I did not miss any deletions 2. Made sure I was not initializing anything inside the loop Managed to eliminate all but a few bytes of leak. Updated code: http://sharetext.org/gPJf The "before" code you can find below:


The function in which I am facing errors is this one: http://sharetext.org/3gq0

From the comments I gather that the initialization and deletion may not be proper. This is the code responsible for the tast

float** CMemAlloc::init_2Dfloat(int rows,int cols)
{
    float **a;
    a=new float*[rows];
    for(int j=0;j<rows;j++)
        a[j]=new float[cols];

    for(int i=0;i<rows;i++)
        for(int j=0;j<cols;j++)
            a[i][j]=0.0;

    return a;

}



void CMemAlloc::del_float(float **a,int rows)
{
    if(a!=NULL)
    {
        for (int i = 0; i <rows; i++) {
            delete[] a[i];
            a[i] = NULL;

        }
        delete[] a;
        a=NULL;
    }
    else
    {
        return;
    }

}

I suspect this function pair is malfunctioning:

CCoarseFun::window** CCoarseFun::init_2Dwin(int rows,int cols)
{
    window** a;

    a=new window*[rows];
    for(int j=0;j<rows;j++)
        a[j]=new window[cols];

    for(int i=0;i<rows;i++)
    {
        for(int j=0;j<cols;j++)
        {
            a[i][j].line_high=0;
            a[i][j].line_low=0;
            a[i][j].pixel_high=0;
            a[i][j].pixel_low=0;
        }
    }
    return a;
}

void CCoarseFun::del_win(window ** a, int rows)
{
    for(int i=0;i<rows;i++)
    {
        delete [] a[i];
        a[i] = NULL;
    }
    delete[] a;
    a=NULL;
}

Could there be an error here?

NOTE: I have put trace statements at various points and am printing the address of "block". Here is the output: http://sharetext.org/ODSv

What I am currently trying is to remove all initializations from inside loops.

I think your issue is not with pointers but with references, you are using double pointer to allocate 2D array, and in your code:

void CCoarseFun::del_win(window ** a, int rows)

window is not passed by pointer, it's a pointer that's passed by value

To solve this issue, you just have to pass it by reference,

void CCoarseFun::del_win(window ** &a, int rows)

And here also ..

void CMemAlloc::del_float(float ** &a,int rows)

In your code everything goes fine, but when you do CCoarseFun::del_win(a,rows); it will de-allocate memory but a won't be nullified, because it's passed in your code as a copy

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM