Question

Language : C++

Platform : Windows Server 2003

I have an exe calling a DLL.

EDIT : (exe is not doing anything, it calls few global function which does everything related to DLL within DLL. It does not explicitly new any of DLL classes)

I allocate (new) the memory for class A within the DLL, it returns me a non-local heap pointer.

I tried to new other classes within the DLL (which are in DLL), "new" returns a valid heap pointer for them, its only Class A which is not being allocated properly.

I am on windows and validating the heap by this function call :

EDIT:

 ClassA* pA = new ClassA(); 

 _CrtIsValidHeapPointer ( (const void *) pA )

I am seriously confused why this only happens with new-ing Class A and no other class ?

(All Native Code)

FINAL EDIT :
it turned out to be a bad build. can't get it to reproduce in the new one... wasted 5 days on this :'(

Was it helpful?

Solution

Export only global functions from DLLs and call only exported functions and through v-tables. Your problem is only one of many caused by trying to export entire classes. DLLs aren't the same as .so libraries.

EDIT: Since the question now reveals that the class isn't exported, but all of this is observed inside the single DLL, going to suggest other possible causes.

Is the same pointer returned from new being tested, or could it be a pointer to a base sub-object? Did the allocation use new or new []? Either of these could cause the pointer to point into the middle of a heap block instead of the start.

OTHER TIPS

If Class A overrides operator new then the memory for that class could be allocated in the DLL's copy of the C runtime. This would cause _CrtIsValidHeapPointer to return false - because your version of the C runtime library is using a different heap than the one in the DLL.

The value returned from _CrtIsValidHeapPointer is generally not reliable anyway (certainly, even though it may return TRUE it doesn't necessarily mean you can use the pointer). Why do you do this anyway?

Perhaps the class declares its own operator new that get storage from some mystery location?

e.g. The author of the class may have written:

class MyClass {
   public:
     void * operator new (size_t amt);
     void operator delete(void *);
};

Then their operator new beats your operator new.

Either that or the DLL is built with /MTd while the executable uses /MD.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top