Aaron
Aaron

Reputation: 23

Windows has triggered a breakpoint

I'm experiencing a problem while trying to use functions exported from my DLL.

I'm getting a message which states (sorry, but I couldn't upload an image):

Windows has triggered a breakpoint in LibTester.exe.

This may be due to a corruption of the heap, which indicates a bug in LibTester.exe or any of the DLLs it has loaded.

This may also be due to the user pressing F12 while LibTester.exe has focus.

The output window may have more diagnostic information.

I have a Vector class, with overloaded assignment operator and some constructors:

Vector::Vector() : X(0.0f), Y(0.0f), Z(0.0f) { }
Vector::Vector(const Vector& vector) : X(vector.X), Y(vector.Y), Z(vector.Z) { }
Vector::Vector(float x, float y, float z) : X(x), Y(y), Z(z) { }
.
.
.
Vector& Vector::operator=(const Vector& rhs)
{
    this->X = rhs.X;
    this->Y = rhs.Y;
    this->Z = rhs.Z;

    return *this;
}

The problem only occurs when i'm attempting to assign an existing vector to a new vector generated by the constructor:

Vector v1 = Vector();                  //Works
Vector v2 = Vector(1.0f, 1.0f, 1.0f);  //Works
v1 = v2;                               //Works
v1 = Vector();                         //Fails
v1 = Vector(1.0f, 1.0f, 1.0f);         //Fails

In case this is relevant, the Vector struct is derived from the class IPrintable:

class IPrintable
{
public: 
    ~IPrintable() 
    {
        if (this->m_pStr != NULL)
            delete[] this->m_pStr;
    }

    virtual char* ToString() = 0;   

protected:
    char* m_pStr;
};

Any one has a clue as to what could cause this behavior?

Upvotes: 0

Views: 4337

Answers (1)

hmjd
hmjd

Reputation: 121961

If that is the full definition of IPrintable the problem is that m_pStr is unitialised meaning there will be an incorrect invocation of delete[].

This fails:

v1 = Vector();

because a temporary Vector is created and the faulty destructor is executed immediately. To correct initialise m_pStr or a better solution would be to use std::string. If you must use a char* then you must also implement a copy constructor and assignment operator or prevent copying (see What is The Rule of Three?).

Upvotes: 2

Related Questions