Problem with RtlValidateHeap

I have a Windows DLL, which at some point returns a pointer to a class that was new'ed in the DLL code. The class itself is a very thin shell around another class private to the DLL.

The calling executable works without problems with this class, everything works fine, except that when the calling executable tries to delete this class, I get an RtlValidateHeap error.

The causes of the error make sense; exe is trying to free memory on the DLL heap, which is usually bad.

I came up with several possible solutions:

  • redefine the operator of the new class to allocate its memory from the executable heap (provided that I can even get into this heap space). The wrapper is very thin, so I would extract a few bytes from the exe heap.
  • Provide a special destruction function for this class (yuck).
  • Tell the user not to destroy the class and live with leaks (in no way!)

Is there a “normal” way to do this?

+3
source share
7 answers

I saw this behavior when one of the binaries was created with debugging options (which uses the debug heap, which, among other things, uses RtlValidateHeap), and the other binary was created as a release binary. It might be worth checking that you only use what you like ...

+3
source

, , C ++, - . DLL, .

+3

( Visual Studio) /MD /MDd . CRT DLL, . DLL .

+2

DLL ( LoadLibrary) .lib exe? / DLL, DLL ? , - - , .

+1

2 , , Release(), " ".

, DLL - DLL (. GetProcessHeap), - , dll , .

, .

, , , HeapAlloc , . GlobalAlloc/GlobalFree . , SysAllocString, . , , DLL- , , .

+1

DLL - :

dll.h:  /* all exported, included by dll user */
class dll_class {
  public:
    virtual void foo() = 0;
}
dll_class* create_class();
void delete_class(dll_class*);

dll.cpp: /* implement the creator and destroyer */
dll_class* create_class() { return new dll_class_imp(); }
void delete_class(dll_class* pClass) { delete pClass; }

dll_imp.h: /* implement the actual dll_class logic */
class dll_class_imp : public dll_class {
  public:
    virtual void foo() {...}
}

, DLL, dll.h :

dll_class* pClass = create_class();
pClass->foo();
delete_class(pClass);

, DLL . , dll_class, ; , , .. .

+1

, , . , 3 :

  • EXE, EXE delete
  • DLL, DLL
  • DLL, EXE

, DLL . , , DLL.

0

Source: https://habr.com/ru/post/1710684/


All Articles