0

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?

Community
  • 1
  • 1
Aaron
  • 23
  • 1
  • 3
  • 3
    If that is the full code then the problem is that `m_pStr` is uninitialized but is referenced in the `IPrintable` destructor. – john Aug 31 '12 at 14:02
  • 1
    Textbook case of how `char*` strings and manual memory management are for very ugly people who want buggy code. – Puppy Aug 31 '12 at 14:06
  • IPrintable needs a constructor, copy constructor and assignment operator to properly init and copy its m_pStr member. – Hans Passant Aug 31 '12 at 14:15
  • Still can't reply properly, too bad... Thank u all for answering, the cause was indeed that pointer. I've added a constructor, copy constructor and assignment operator and it all worked out. hopefully I haven't introduced any more problems in the implementation. And DeadMG: I don't like your answer, very impolite. – Aaron Aug 31 '12 at 21:43

1 Answers1

2

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?).

Community
  • 1
  • 1
hmjd
  • 120,187
  • 20
  • 207
  • 252