Silver
April 18th, 2007, 05:23
Hi all,
Been mulling this one over for a bit and I can't quite find a satisfactory answer. C/C++ question.
Assuming I have a pointer to "something", how can I programmatically determine whether that something is created on the stack or the heap.
Consider this code:
nReturn now has either a pointer or a value in it, depending what RandomFunc returns. All fine so far. Now here's the kicker. How do I differentiate between this code in the RandomFunc export returning a heap ptr:
and this code returning a string literal:
The first example would require my code to do this:
But doing this on the second example will die horribly. Programmatically, if the DLL is a black box DLL I don't have docs or info for, I have no simple way to determine this.
I tried using _CrtIsValidHeapPointer() on the pointer but this just breaks and chucks "Invalid address specified to RtlValidateHeap()" at me, which I can't catch with an exception.
Anyone have an idea?
Thanks!
Been mulling this one over for a bit and I can't quite find a satisfactory answer. C/C++ question.
Assuming I have a pointer to "something", how can I programmatically determine whether that something is created on the stack or the heap.
Consider this code:
Code:
LoadLibrary(....blah...);
pfnExport = GetProcAddress("RandomFunc", ....blah...);
int nReturn = pfnExport();
nReturn now has either a pointer or a value in it, depending what RandomFunc returns. All fine so far. Now here's the kicker. How do I differentiate between this code in the RandomFunc export returning a heap ptr:
Code:
void* RandomFunc()
{
char* pszNew = new char[10];
strcpy(blah into pszNew);
return (void*)pszNew;
}
and this code returning a string literal:
Code:
void* RandomFunc()
{
return "blah1234";
}
The first example would require my code to do this:
Code:
int nReturn = pfnExport();
void* ptr = (void*)nReturn;
delete ptr
But doing this on the second example will die horribly. Programmatically, if the DLL is a black box DLL I don't have docs or info for, I have no simple way to determine this.
I tried using _CrtIsValidHeapPointer() on the pointer but this just breaks and chucks "Invalid address specified to RtlValidateHeap()" at me, which I can't catch with an exception.
Anyone have an idea?
Thanks!