ShaunO,
That's not going to tell you anything. Because PChar is an unmanaged
type, the memory is unmanaged. If it was managed, then you wouldn't have
this issue to begin with.
You need to know how the PChar is allocated. Does he use "new" (or the
Delphi equivalent, I don't know what they use) or a a call to an API to
allocate the memory for the string? Or is it copied from somewhere else?
As a side note, this is why you see most Window's APIs take a pointer to
an allocated buffer. Because there are so many ways you can allocate
memory, the API functions leave it to the caller to do this, which also
leaves it to the caller to de-allocate the memory. This way, it doesn't
have to make assumptions.
The only place where you won't see this is where there are explicit API
functions to de-allocate memory, such as in COM (where there is an
allocator, usually called through CoTaskMemAlloc, which gets the IMalloc
implementation and then allocates/deallocates memory) or in the WNET API's
(which have a separate API function for deallocating memory), to name a few.
--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)
ShaunO said:
Nicholas,
He just created it by declaring a variable and then using it in the same
way
that if it was a string within a c# function it would look like:
string st = "test";
st=st+" hello world";
return st;
Does that give you enough to go on - or has it flagged a wild flaw in the
approach?
S.
Nicholas Paldino said:
ShuanO,
Yes, it's a local variable, but the original author must have used a
method to allocate the memory (malloc, for example, or new). If that is
the
case, then you need to know what else the author did.
If the author actually used memory that was allocated on the stack,
then
the original code is broken, because once the stack exits, there is no
guarantee that the memory pointed to by the return value is still
correct.
--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)
ShaunO said:
Nicholas,
Having spoken to the dll author (not in-house) the dll just creates a
local
variable and then returns that variable - I am presuming that because
the
dll
definition states a return of a pointer to a null terminated char array
the
dll will take that variable and return to me it's address in memory.
There was however no specific malloc style memory allocation.
Does this help?
S
:
ShaunO,
Generally, yes, if the method in the unmanaged DLL allocated the
memory,
then you need to do something to free it. However, how you free it
depends
on how it was allocated in the DLL. How did the unmanaged DLL
allocate
the
memory?
--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)
Hi,
I am calling a delphi .dll that returns a pchar (pointer to char
array).
I use this as a string - however when done I believe that i need to
free
the
memory that the pchar resided in.
1) Do I need to do this
2) How do i go about freeing the memory that my application did not
allocate?
Thanks,
S