0

I'm using xcode and instruments to find my memory leak, it is complaining that this following code creates memory leak:

double **cn2;
cn2= new double*[noOfItem];
for(int i=0;i<noOfItem;i++)
{
    cn2[i]=new double[noOfItem];
}
for(int i=0;i<noOfItem;i++)
{
    for(int j=0;j<noOfItem;j++)
    {
        cn2[i][j]=getCN2(noOfItem,i,j,pearson,cn2CutOff);
        //cout<<i<<" "<<j<<" "<< cn[i][j]<<endl;
    }

}
for(int i=0;i<noOfItem;i++)
{
    delete [] cn2[i];
}
delete [] cn2;

This is the function of getCN2, it is used to fill the 2d array:

double getCN2(int _isize,int _i1,int _i2,double **sim,double _cutoff)
{
    //cout<< med<<" "<<med1<<endl;
    int count=0;

    for(int i=0; i<_isize; i++)
    {
        //if(sim[_i1][i]>_cutoff && sim[_i2][i]>_cutoff)
        if(sim[_i1][i]>sim[_i1][_i2] && sim[_i2][i]>sim[_i1][_i2] && sim[_i1][_i2]>0)
        {
            count++;
        }
    }
    //cout << rez/sqrt(rez1*rez2) <<endl;
    return count;
}

And there is no memory leak if I change my code into:

double **cn2= new double*[noOfItem];
for(int i=0;i<noOfItem;i++)
{
    cn2[i]=new double[noOfItem];
}
for(int i=0;i<noOfItem;i++)
{
    for(int j=0;j<noOfItem;j++)
    {
        cn2[i][j]=getCN2(noOfItem,i,j,pearson,cn2CutOff);
        //cout<<i<<" "<<j<<" "<< cn[i][j]<<endl;
    }

}
for(int i=0;i<noOfItem;i++)
{
    delete [] cn2[i];
}
delete [] cn2;

The only possible reason I could think of is that when I call double **cn2; it points to something already and when I call cn2= new double*[noOfItem]; it points to something else and the original **cn2 didn’t get freed? Has anyone else encountered this problem before? It is really weird...Do I have to write them in one line instead of 2 when using new to allocate?

weeo
  • 2,619
  • 5
  • 20
  • 29
  • 1
    What happens if something uexpected happens between the new and delete? A leak. – Ivan Rubinson Jan 11 '17 at 10:44
  • 10001 reasons to use smart pointers... I suggest you look up std::shared_ptr and its family – UKMonkey Jan 11 '17 at 10:45
  • _The only possible reason I could think of is that when I call double **cn2; it points to something already_ Well.. It points to garbage, and not to a valid value, which would need to be freed. – Algirdas Preidžius Jan 11 '17 at 10:46
  • What is hidden behind those `...`? – PaulMcKenzie Jan 11 '17 at 10:46
  • updated the codes for ..., I didn't point to anything else. I just filled it with some numbers by using another function. – weeo Jan 11 '17 at 10:52
  • You obviously didn't just allocate and then immediately deallocate just for kicks. You're doing something in-between the allocation and deallocation with `cn2` that you're not showing us that could cause issues. – PaulMcKenzie Jan 11 '17 at 10:53
  • @PaulMcKenzie updated code – weeo Jan 11 '17 at 10:54
  • @weeo If you remove that code, do you get a memory leak? That code without the "..." and without those functions you're calling does not leak memory. – PaulMcKenzie Jan 11 '17 at 10:58
  • try changing allocation as `cn2= (double**)new double*[noOfItem];` .. Hopefully it will get rid of the error. There is no problem with both the code. Your Code might be failing the instrumental logic of xCode – Daksh Gupta Jan 11 '17 at 11:01
  • 2
    @DKG terrible advice – M.M Jan 11 '17 at 11:02
  • probably there is a bug elsewhere in the program , such as a buffer overflow, that happens to show up during this part of the code – M.M Jan 11 '17 at 11:03
  • What you might want to do is try changing your array of pointers into a single array- ie `double *cn2= new double[noOfItem*noOfItem];` The contiguous memory may improve performance, and it will simplify your code somewhat ... – UKMonkey Jan 11 '17 at 11:04
  • @M.M I've seen many memory profiling tool which reports problem in this kind of code.. That's why its just a advice.... Anyway as someone suggested... use of smart pointers are way better – Daksh Gupta Jan 11 '17 at 11:05
  • @DKG if a profiling tool needs the engineer to cast a double** to a double ** to not report a memory leak, it's a tool with only one destination – UKMonkey Jan 11 '17 at 11:06
  • @UKMonkey But then if my matrix is not symmetric, then 2*3 and 3*2 would point to the same number... – weeo Jan 11 '17 at 11:06
  • @M.M the profiling tool stops complaining when I did that only modification...I guess it's a weird thing for xcode only then. – weeo Jan 11 '17 at 11:09
  • 1
    [This has no memory leak](http://ideone.com/5O84SB). This is where anyone that doesn't have your entire code has to start from. So unless your memory reporting tool is a piece of junk, you're probably corrupting memory somewhere else in your larger program, and it is affecting the code you've posted. – PaulMcKenzie Jan 11 '17 at 11:10
  • @weeo how does [i * rowItems + j] end up referencing the same thing for multiple values of i & j? – UKMonkey Jan 11 '17 at 11:11
  • @UKMonkey my bad... My brain stops working for a sec. Is it possible that I get different output because of a memory leak? – weeo Jan 11 '17 at 11:17
  • @weeo no, but what you could be doing is referencing memory you're not allowed to reference in your pearson variable for example which could cause a problem and cause your memory leak - hence the suggestion of smart pointers, they ensure that you never get memory leaks, no matter how much you mess up! – UKMonkey Jan 11 '17 at 11:27
  • @UKMonkey thank you! std::auto_ptr should work the same as boost ones? – weeo Jan 12 '17 at 04:34
  • 1
    @weeo - std::auto_ptr has been deprecated, I think because it confused a number of people with some of the usages. That said, yes, it should do for your purposes if you don't plan to copy it around. – UKMonkey Jan 12 '17 at 10:50

1 Answers1

0

do preventing coding to avoid such kind of error

Pointer initialization is must

double *ptr = nullptr;

Check is required before allocating memory

if (nullptr==ptr) { ....}

If you writing new code then avoid usage of raw pointer

unique_ptr. Allows exactly one owner of the underlying pointer. 
shared_ptr. Reference-counted smart pointer. ...
weak_ptr. Special-case smart pointer for use in conjunction with shared_ptr

Debugging technique:-

Use of visual studio to find memory related error Buffer Overflow explained

Use diagnostic tool

Vld and parallel studio

Shamkumar Rajput
  • 166
  • 1
  • 10
  • "shared_ptr. Reference-counted smart pointer. ..." Yes, but the ideology behind it implies that it should be used less than you might think. Good discussion here: http://stackoverflow.com/questions/8706192/which-kind-of-pointer-do-i-use-when – user4581301 Jan 12 '17 at 03:41