Re: Passing pointer between DLL boundary question
Why would creating global memory in a dll and freeing it in the application
be a problem?
AliR.
<indrawati.yahya@gmail.com> wrote in message
news:1153224364.650528.89470@i42g2000cwa.googlegroups.com...
Hi
This may be a stupid question, but I want to make sure my understanding
is correct. I understand that allocating a memory inside a DLL and
freeing the memory in the program that uses the DLL (or vice versa) is
not a good idea, but if a pointer passed between a DLL boundary is
allocated and freed at the correct place, is the code guaranteed to be
safe? For example:
1. In dll implementation code:
static const char someString[] = "a string";
void GetString(const char** ps)
{
if(ps)
*ps = &(someString[0]);
}
And in application code:
const char* aString;
GetString(&aString);
cout << aString;
2. Another example: in dll implementation code:
static void* data = 0;
static int dataSize = 0;
///... functions that manipulate data
int GetData(void* buff, int buffSize)
{
if(buff && buffSize >= dataSize)
{
memcpy(buff, data, dataSize);
return dataSize;
}
return 0;
}
(buff is a buffer allocated and freed in the application).
I tried on my VC++ 6, both examples run fine, but are they always
guaranteed to be safe? Are there any side effects that I should be
aware of? Thanks!