My memory leak in C++ program

Go To StackoverFlow.com

1

I'm faced a new problem something with memory allocation and leak here is my error log:

Dr. Memory version 1.4.6 build 2 built on Mar  7 2012 10:14:04
Application cmdline: ""D:\c++\Begin\Lab3-5_OOP\Debug\Lab3-5_OOP.exe""
Recorded 62 suppression(s) from default C:\Program Files (x86)\Dr. Memory/bin/suppress-default.txt

Error #1: UNINITIALIZED READ: reading register eax
# 0 _fu89___ZSt4cout               [D:\c++\Begin\Lab3-5_OOP\Debug/../Controller.cpp:156]
# 1 main                           [D:\c++\Begin\Lab3-5_OOP\Debug/../M.cpp:25]
Note: @0:00:00.924 in thread 4584
Note: instruction: test   %eax %eax

Error #2: LEAK 12 direct bytes 0x00531420-0x0053142c + 1024 indirect bytes
# 0 libstdc++-6.dll!Znwj           
# 1 constr()               [D:\c++\Begin\Lab3-5_OOP\Debug/../ListStruc.cpp:24]
# 2 main                   [D:\c++\Begin\Lab3-5_OOP\Debug/../M.cpp:18]

Error #3: LEAK 12 direct bytes 0x009bec48-0x009bec54 + 1024 indirect bytes
# 0 libstdc++-6.dll!Znwj  +0x23     (0x6fcbb523 <libstdc++-6.dll+0x7b523>)
# 1 constr()               [D:\c++\Begin\Lab3-5_OOP\Debug/../ListStruc.cpp:24]
# 2 main                   [D:\c++\Begin\Lab3-5_OOP\Debug/../M.cpp:20]

DUPLICATE ERROR COUNTS:

SUPPRESSIONS USED:

ERRORS FOUND:
      0 unique,     0 total unaddressable access(es)
      1 unique,     1 total uninitialized access(es)
      0 unique,     0 total invalid heap argument(s)
      0 unique,     0 total warning(s)
      2 unique,     2 total,   2072 byte(s) of leak(s)
      0 unique,     0 total,      0 byte(s) of possible leak(s)
ERRORS IGNORED:
     78 still-reachable allocation(s)
         (re-run with "-show_reachable" for details)
Details: C:\Users\Warzaru\AppData\Roaming/Dr. Memory/DrMemory-Lab3-5_OOP.exe.10024.000/results.txt

Struct:

const int days=31;
const int exp=6;

struct Arr{
    int days;
    int exp;
    int **M;
};
typedef Arr* Array;

Constructor:

void constr(Array &loc){
    //Construct of 31*6 Matrix, were 31 nr. of days and 6 specific types:
    //0-HouseKeeping, 1-Food, 2-Transport, 3-Clothing, 4-TelNet, 5-others
    loc=new Arr;
    loc->days = days;
    loc->exp = exp;
    loc->M = new int*[loc->days];
    for(int i=0; i<loc->days;i++ ){
       loc->M[i] = new int[loc->exp];
       for (int j = 0; j < loc->exp; j++){
           loc->M[i][j] = 0;
       }
    }
}

The program errors me only for some function of ti for example function:

void maxDay(Array &M){
    //Output the day with highest value
    cout<<"test";
    int hD = 0;
    int s1 = 0;
    int s2 = 0;
    cout<<"test";
    for(int i = 0; i<30;i++){
        s1=0;
        for (int j=0; i<5; j++){
            s1 = s1 + M->M[i][j];
            if(s2 <= s1){
                s2 = s1;
                hD = i;
                cout<<"test";
            }
        }
    }

}

So short, I have a structure Arr ( Matrix of 31*6) were I store ints (different types of expenses) but when I use some of my functions I get Segmentation fault. I have no experience with this kind of errors, so any advices are useful.

EDIT:

void destruc(Array &loc){
    for(int i=0; i<loc->days;i++ ){
       delete[] loc->M[i];
       for (int j = 0; j < loc->exp; j++){
           delete[] loc->M[i][j];
   }
}
}
2012-04-04 04:07
by Bogdan Maier
I see new but not delete - Pubby 2012-04-04 04:12
I suggest you replace int **M with std::vector< std::vector<int> >. Actually, there are better approaches, but this would have the least impact on your existing solution - Marcelo Cantos 2012-04-04 04:12
the thing is I have to use my own structure : - Bogdan Maier 2012-04-04 04:21
@BogdanMaier: If this is homework, you need to tag it as homework, otherwise you will get proper/suitable answers that you can't use because of the restrictions imposed by your homework guidelines - dreamlax 2012-04-04 04:29
ok, thank you i`ll keep in mind for the futur - Bogdan Maier 2012-04-04 04:32


0

The destructor in seems to be strange

void destruc(Array &loc){
    for(int i=0; i<loc->days;i++ ){
       delete[] loc->M[i]; <-------------- deleting array of pointers to array
       for (int j = 0; j < loc->exp; j++){
           delete[] loc->M[i][j]; <------- deleting pointer to array that
                                           is already deallocated
   }
}

Destructor should look the following way (in accordance to constructor):

void destruc(Array &loc){
    for(int i=0; i<loc->days;i++ ){
       delete[] loc->M[i];
    }
    delete[] M;
}
2012-04-04 07:38
by Alex


1

Follow the rule "Deallocate every block of memory that you have allocated dynamically"

Deallocate using delete the memory you have allocated using new

This may throw some light for you http://www.cplusplus.com/doc/tutorial/dynamic/

If you allocated an array using new[] then delete it using delete[]

In this case i would suggest you to write a constructor and destructor for the struct Arr instead of writing normal functions.

2012-04-04 04:22
by Rohit Vipin Mathews
is it correct like this? i edited the psot and added deconstrctor - Bogdan Maier 2012-04-04 04:29
You are mixing delete[] and delete with new and new[]Rohit Vipin Mathews 2012-04-04 04:39


1

// I hope you intended to write j<5
for (int j=0; i<5; j++){  //infinite Loop... as j is still 0

So what happens with the statement i<5 in your program is, your inner for loop becomes an indefinite loop and tries to access unallocated memory.

2012-04-04 04:46
by Sam Daniel
good Spot!! it must be a typ - Rohit Vipin Mathews 2012-04-04 04:49


1

1    void destruc(Array &loc) {
2        for(int i=0; i<loc->days;i++ ) {
3            delete[] loc->M[i];
4            for (int j = 0; j < loc->exp; j++) {
5                delete[] loc->M[i][j];
6            }
7        }
8    }

i see you're delete[]'ing loc->M[i] (line 3) and yet you still references it's contents at line 5.

I suggest this is a bug as you've handed the memory back to the heap and any other part of your application can now re-use it. So by the time your application gets to line 5 it may not have the contents you expect.

I'd suggest rewriting it as...

1    void destruc(Array &loc) {
2        for(int i=0; i<loc->days;i++ ) {
3            for (int j = 0; j < loc->exp; j++) {
4                delete[] loc->M[i][j];
5            }
6            delete[] loc->M[i];
7        }
8    }
2012-04-04 07:35
by ScaryAardvark
Ads