Basically i have made a binary class that inherits from a matrix class. and aparantly when i make the binary image the deconstructor doesn't work properly. the program runs, reads in the files and makes the binary matrix. However when the binary deconstructor and matrix deconstructors are called, the program fails and shoot to this line of code.
/* verify block type */_ASSERTE(_BLOCK_TYPE_IS_VALID(pHead->nBlockUse));

I don't know what this means even after looking online, I'm going to guess the pointer isn't being deleted, so maybe somebody could help me.

This is the matrix class

class Matrix
{
private:

    //double* data;
    int value;


public:
    double* data;

    int M;
    int N;
    Matrix();
    Matrix(int MM, int NN);
    //Matrix(const Matrix&); 
    Matrix(const Matrix& rhs);  //deep copy
    Matrix(int MM, int NN, double* input_data);
    virtual ~Matrix();

    double getRead(int i, int j) const; 

    double getReadWrite(int i, int j); 
    void set(int i, int j, double val); 

    void Print();

    Matrix* sum1(Matrix* rhs); 
    double* to1DArray();
    ///operators
    virtual Matrix* operator +(Matrix* rhs) const;
    virtual Matrix* operator -(Matrix* rhs) const;
    virtual Matrix* operator *(Matrix* rhs) const;
    virtual Matrix* operator /(Matrix* rhs) const;
    virtual Matrix& operator =(Matrix* rhs);

    ///overload double operators
    Matrix* operator +(double* rhs) const;
    Matrix* operator -(double* rhs) const;
    Matrix* operator *(double* rhs) const;
    Matrix* operator /(double* rhs) const;
    Matrix* operator =(double* rhs);

    ///min,max,sum,mean
    double min(Matrix* rhs) const;
    double max(Matrix* rhs) const;
    double sum() const;
    double mean() const;

    //double SqaureRoot() const;

};

and this is the binary class which inherits from the matrix class

class BinaryImage: public Matrix
{
private:
    char ThreshData(double thresh); 


public: 
    double* input_data;

    BinaryImage();
    BinaryImage(int MM, int NN, double* input_data){ThreshData(0);};
    //BinaryImage(int MM, int NN){ThreshData(1);};
    ~BinaryImage();

    Matrix* operator +(Matrix* rhs) const;
    Matrix* operator -(Matrix* rhs) const;
    Matrix* operator *(Matrix* rhs) const;
    Matrix* operator /(Matrix* rhs) const;

    Matrix* operator +(double* rhs) const;
    Matrix* operator -(double* rhs) const;
    Matrix* operator *(double* rhs) const;
    Matrix* operator /(double* rhs) const;

};

BinaryImage::BinaryImage()
{
    cout << "BinaryImage Constructor...\n";
}

This next bit of code is what calls the class

void function()
{
    double* data = 0;
    double* shufData = 0;
    int M=0; int N=0; 

    cout << endl;
    cout << "Data from text file -------------------------------------------" << endl;
    M = 512; N = 512; //M and N represent the size of the image, e.g. for task 1, M = 512, N = 512
    char* fileName2 = "...\\logo_bi.txt"; 
    data = readTXT(fileName2, M, N);

    cout << endl;
    cout << "Data from text file -------------------------------------------" << endl;
    M = 512; N = 512; //M and N represent the size of the image, e.g. for task 1, M = 512, N = 512
    char* fileName3 = "...\\logo_shuf.txt"; 
    shufData = readTXT(fileName3, M, N);

    cout << "---------originalImage----------"<< endl;
    BinaryImage originalImage(32,32, data);
}

It was after this is called that it dies.

Thank you in advanced

I don't know what this means even after looking online, I'm going to guess the pointer isn't being deleted, so maybe somebody could help me.

If a pointer wasn't deleted, the program would not crash, only leak memory (which the OS will reclaim anyways). I'm pretty sure that this error is due to deleting a pointer twice, or attempting to delete a pointer that doesn't point to memory that was allocated on the heap (either an uninitialized pointer, or a pointer to a stack-based object or array). This is a classic heap-corruption error (asking the heap to do something that doesn't make sense like deleting memory that is already free or deleting memory that doesn't belong to the heap (or that heap)).

I'd like to be able to help you find the problem, but it's impossible with the code you posted. Clearly, the error is in your implementations of the constructors, copy-constructors, copy-assignment operators and destructors, but you didn't post that code, only the interfaces of your classes. Since all I can look at is the interface, I'll comment on that.

First of all, you are missing the assignment operator for your Matrix class. The standard form for the copy-assignment operator is one of the following:

Matrix& operator=(const Matrix&);
Matrix& operator=(Matrix);

What you have in your code is the following:

Matrix& operator=(Matrix*);

This is wrong, and it won't be recognized by the compiler as a viable alternative for the copy-assignment operator, and thus, the compiler will generate a default version which does a "shallow-copy" (meaning it copies the pointers, not the pointed-to memory). This could be the root cause of your error, if I assume you wrote the other functions correctly (but I'm not inclined to think that, judging by the rest of your interface).

Similarly, your BinaryImage class is also missing a copy-constructor and copy-assignment operator. That is also an error, for obvious reasons.

Second, you make your data pointers public members of your classes. This is bad, real bad. I'm as permissive as one can get when it comes to having public data members in a class (many people are much more severe than I am about this). But in this case, huge UNACCEPTABLE. The first task of a class that holds a resource (memory or otherwise) is to protect that resource (and its integrity), and you can't do that if it is public. If you need the resource in the derived class, make it protected (but even that is not recommended, maybe some protected member functions would be enough?). I also strongly recommend that you read my tutorial on the subject of resource-holding classes.

Third, you should not use so many raw-pointers in your operators. Not only will this make the use of those operators annoyingly awkward, but also terribly unsafe and difficult to manage (avoid leaks). Especially today, with C++11 that has move-semantics, there is very little reason to use pointers so much, just pass-by-value or pass-by-reference, and always return by value (the temporary will be optimized-away anyways).

Finally, most operators should be implemented as non-member functions (i.e. free functions) (and as friend functions if needed). There are only a few operators that need to be implemented as member functions, and they are pretty much the only ones that should be (assignments, conversions, dereference, etc.). All operators like simple math or comparison operators should be implemented as free functions (in the same namespace as your class). If you want virtual dispatch on operators (e.g. you would be tempted to write them as virtual member functions), it is recommended that you implement them as protected virtual member functions (not operator overloads) and then call those virtual functions from the operator overloads (implemented as friend functions).

If you follow the above guidelines, and those of my tutorial, I'm pretty sure that your problem with the heap-corruption will disappear. I've been following these simple guidelines for years, and have never had memory-related problems since, I mean, not, ever!

@mike_2000_17 Thank you very much, i'll give that a go and get this thing working again

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.