简体   繁体   中英

Memory leaks from 2d array on heap

I'm having an issue with a lot of memory leaks from a class I've created. The assignment is requires creating a word search puzzle on the heap. I've created my destructor, copy constructor and overload the assignment operator.

I think there must be something wrong with one of these functions, because the final check to ensure it is working is to create objects in a loop, to see if it fails and my function is crashing. I've tried different forms of the destructor and I've tried changing around the copy and assignment operator with no luck. Kind of at a loss, and the lack of warnings is really making it difficult to debug without a proper understanding of the heap.

Any help would be really appreciated!

Here are some functions that are working with the heap.

JumblePuzzle::~JumblePuzzle(){
    for (int i = 0; i < size; ++i){
        delete jumble[i];
    }
    delete jumble;

}

JumblePuzzle::JumblePuzzle(string word, string diff){
    int i = 0;
    toHide = word;
    difficulty = diff;
    jumble = buildArray();
    fillArray();
    hideWord();
}

JumblePuzzle::JumblePuzzle(JumblePuzzle& temp){
    size = temp.size;
    rowPos = temp.rowPos;
    colPos = temp.colPos;
    direction = temp.direction;
    toHide = temp.toHide;
    difficulty = temp.difficulty;
    jumble = temp.getJumble();
}

JumblePuzzle& JumblePuzzle::operator=(const JumblePuzzle& right){
    if (this != &right){
        for (int i = 0; i < size; ++i){
            delete jumble[i];
        }
        delete[] jumble;
        size = right.size;
        rowPos = right.rowPos;
        colPos = right.colPos;
        direction = right.direction;
        toHide = right.toHide;
        difficulty = right.difficulty;
        jumble = right.getJumble();
    }
    return *this;
}

charArrayPtr* JumblePuzzle::buildArray() const{
    charArrayPtr* array = new char*[size];
    for (int i = 0; i < size; ++i){
        array[i] = new char[size];
    }
    return array;
}

Here's the line its failing on.

int loopLimit =20;
for (int i = 0; i < loopLimit; i++)
    JumblePuzzle jp("HIDDENWORD", "hard");

Thanks for any possible help!

EDIT:

Here is my .h file as well.

#ifndef JUMBLE_H_
#define JUMBLE_H_

#include <time.h>
#include <cstdlib>
#include <string>
using namespace std;
typedef char* charArrayPtr;


class BadJumbleException {
public:
    BadJumbleException(const string&);
    string& what();
private:
    string message;
};

class JumblePuzzle{
public:
    JumblePuzzle(string, string); //simple constructor
    JumblePuzzle(JumblePuzzle&); //copy constructor
    ~JumblePuzzle();            //deconstructor
    charArrayPtr* getJumble() const;
    JumblePuzzle& operator=(const JumblePuzzle&);

    //accessors
    int getSize();
    int getRowPos();
    int getColPos();
    char getDirection();

private:
    //attributes
    int size;
    int rowPos;
    int colPos;
    char direction;
    charArrayPtr* jumble;
    string toHide;
    string difficulty;

    void fillArray();
    void hideWord();
    char randomDirection();
    int randomNum(int);
    charArrayPtr* buildArray() const;
};
#endif

and my getJumble. It's used to get the actual word search created. Returned a copy rather than the pointer so it cant be modified.

charArrayPtr* JumblePuzzle::getJumble() const{

    charArrayPtr* tempJumble = new char*[size];
    for (int i = 0; i < size; ++i){
        tempJumble[i] = new char[size];
    }

    for (int i = 0; i < size; i++){
        for (int j = 0; j < size; j++){
            tempJumble[i][j] = jumble[i][j];
        }
    }

    return tempJumble;
}

There is one major thing wrong with your code, and that is you failed to initialize the "size" member in the JumblePuzzle(string, string) constructor.

There are other things you should do:

1) Create a separate function to destroy the 2d array within the JumblePuzzle class. You seem to be copying the same loops to do this in multiple places. No need for that if you just call a function to do this work.

2) Your assignment and copy constructor are not exception safe. If new[] throws an exception during the creation of the copy, then the original object has invalidated data. In other words, you've destroyed the data, and when you want to create another 2d array, when new[] says "oops", you've destroyed your original data and can't get it back.

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