views:

201

answers:

3

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 :'(

+1  A: 

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.

bmargulies
thank you. but no I am not using user defined "new"
Gollum
Nope, I have not overloaded allocators.
Gollum
+1  A: 

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?

Dean Harding
thanks : ) helpful but this is not the case.why is it not reliable, what is the other method to make sure if heap pointer is in local heap? I tried doing the same thing using windbg and checking if the pointer is in range of the local heap but do not have enough skills and Windbg is anything but intuitive.
Gollum
Link both your DLL and your application against the C runtime library DLL - that way they'll both use the same heap.
Larry Osterman
+2  A: 

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.

Ben Voigt
Good advice. If you're *really* careful and know what you're doing, it's possible to export entire classes, but it's generally not a great idea...
Dean Harding
Ben, The allocation is actually taking place in DLL not allocating the DLL class in exe , let me edit it (I made it so confusing)
Gollum
Is `_CrtIsValidHeapPointer` also being called from inside the DLL?
Ben Voigt
Yes, right after I do a new, and no the pointer is to the class it self(no parente, no slicing).
Gollum