0

As stated, I'm getting "Invalid address specified to RtlValidateHeap" when deleting a pointer allocated with new. I've never seen this error before, and it occurs in main.

Main.ccp:

#include <iostream>

#include "ConsolePrinter.h"

int main(int argc, char* argv[]) {
    std::cout << "Test" << std::endl;

    ObjectPrinter* op1 = new ConsolePrinter('V');
    op1->printObject();

    delete op1;
}

ConsolePrinter is derived from ObjectPrinter. Both are custom classes

ObjectPrinter.h:

#ifndef OBJECTPRINTER_H
#define OBJECTPRINTER_H

#include <typeinfo>

class ObjectPrinter {
public:
    virtual bool compatible(const ObjectPrinter* objprtr) const = 0;
    virtual void printObject() = 0;
    virtual const std::type_info& getType() const = 0;
};

#endif

ConsolePrinter.h:

#ifndef CONSOLEPRINTER_H
#define CONSOLEPRINTER_H

#include "ObjectPrinter.h"

class ConsolePrinter : public virtual ObjectPrinter {
public:
    ConsolePrinter();
    ConsolePrinter(unsigned char c);

    bool compatible(const ObjectPrinter* objprtr) const;
    virtual void printObject();
protected:
    const bool compatibilityMode;

    virtual const std::type_info& getType() const;
private:
    const unsigned char repr;
};

#endif

Before delete op1;:

  • The address of op1 is 0x00E1BF7C.
  • Within the heap, op1 is instance <0x00E1BF50> (according to Visual Studio's HEAP snapshot).

When delete op1; is executed, the error given is Invalid address specified to RtlValidateHeap( 00E10000, 00E1BF5C ).

If someone can explain why this is happening, I would be very appreciated.

The Eye
  • 59
  • 12
  • 3
    [No `virtual` destructor could be the problem](https://stackoverflow.com/questions/461203/when-to-use-virtual-destructors). Hard to say for sure without a complete example. – user4581301 Nov 22 '21 at 23:59
  • That's just crazy enough to work. I'll get back to you once I'm back at my workstation and can test it. – The Eye Nov 23 '21 at 00:47
  • Let us know if it is and we'll close as a duplicate. If it isn't we'll probably need more in the example code. – user4581301 Nov 23 '21 at 00:53
  • It worked. Should I put that as the answer then, or will marking as duplicate suffice? – The Eye Nov 23 '21 at 20:45

0 Answers0