https://bugs.kde.org/show_bug.cgi?id=459047

            Bug ID: 459047
           Summary: Memory leak in valgrind memcheck code
           Product: valgrind
           Version: 3.15 SVN
          Platform: Ubuntu Packages
                OS: Linux
            Status: REPORTED
          Severity: normal
          Priority: NOR
         Component: memcheck
          Assignee: jsew...@acm.org
          Reporter: vanelbur...@hetnet.nl
  Target Milestone: ---

Created attachment 152018
  --> https://bugs.kde.org/attachment.cgi?id=152018&action=edit
Code from the minimal example as a file.

In code only using pointers to doubles and pointers to pointers to doubles, we
see a memory leak related to 'operator new[](unsigned long) (in
/usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)'. 

Minimal example:

```C++

void divide2DDoubleArray(double * &block, double ** &subblockdividers, int
noofsubblocks, int subblocksize){
    /* The starting address of a block of doubles is used to generate
     * pointers to subblocks.
     *
     *      block: memory containing the original block of data
     *      subblockdividers: array of subblock addresses
     *      noofsubblocks: specify the number of subblocks produced
     *      subblocksize: specify the size of the subblocks produced
     *
     * Design by contract: application should make sure the memory
     * in block is allocated and initialized properly.
     */
    // Build 2D matrix for cols
    subblockdividers=new double *[noofsubblocks];
    subblockdividers[0]= block;
    for (int i=1; i<noofsubblocks; ++i) {
        subblockdividers[i] = &subblockdividers[i-1][subblocksize];
    }
}

int main(){

    double * testarray = new double[16];
    double ** pTestarray; 

    divide2DDoubleArray(testarray, pTestarray, 4, 4);

    delete[] pTestarray;
}
```

Compiled with:
```
g++ -g valgrind_error.cpp -o ve.out
```

and ran with 

```
valgrind --leak-check=yes  ./ve.out 
```

yields:
```
==5775== Memcheck, a memory error detector
==5775== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==5775== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==5775== Command: ./ve.out
==5775== 
==5775== 
==5775== HEAP SUMMARY:
==5775==     in use at exit: 128 bytes in 1 blocks
==5775==   total heap usage: 3 allocs, 2 frees, 72,864 bytes allocated
==5775== 
==5775== 128 bytes in 1 blocks are definitely lost in loss record 1 of 1
==5775==    at 0x483C583: operator new[](unsigned long) (in
/usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==5775==    by 0x109283: main (valgrind_error.cpp:25)
==5775== 
==5775== LEAK SUMMARY:
==5775==    definitely lost: 128 bytes in 1 blocks
==5775==    indirectly lost: 0 bytes in 0 blocks
==5775==      possibly lost: 0 bytes in 0 blocks
==5775==    still reachable: 0 bytes in 0 blocks
==5775==         suppressed: 0 bytes in 0 blocks
==5775== 
==5775== For lists of detected and suppressed errors, rerun with: -s
==5775== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
```

-- 
You are receiving this mail because:
You are watching all bug changes.

Reply via email to