How to free memory allocated in C function

S

silvia.fama

Hi!

I wrote VB Net code to create an application.
The application calls a C function (loaded into a dll). This C
function allocates a string:
Blob = malloc (sizeof xxx)
The string Blob is returned to VB Net application that needs to read.
I'm sure that the memory allocated for Blob into the C function is not
freed in VB Net application (aslo if I do Blob = Nothing).
How can I free this memory?

I hope to be clear!!
Thank you!
Silvia
 
R

rowe_newsgroups

Hi!

I wrote VB Net code to create an application.
The application calls a C function (loaded into a dll). This C
function allocates a string:
Blob = malloc (sizeof xxx)
The string Blob is returned to VB Net application that needs to read.
I'm sure that the memory allocated for Blob into the C function is not
freed in VB Net application (aslo if I do Blob = Nothing).
How can I free this memory?

I hope to be clear!!
Thank you!
Silvia

I'm pretty sure you'll need to use the Window's API for this.
Unfortunately, I'm not famaliar on the memory management parts of the
API so I can't help you out too much. Here's a list of the
documentation for the memory functions:

http://msdn2.microsoft.com/en-us/library/aa366781.aspx

Thanks,

Seth Rowe
 
J

Jack Jackson

Hi!

I wrote VB Net code to create an application.
The application calls a C function (loaded into a dll). This C
function allocates a string:
Blob = malloc (sizeof xxx)
The string Blob is returned to VB Net application that needs to read.
I'm sure that the memory allocated for Blob into the C function is not
freed in VB Net application (aslo if I do Blob = Nothing).
How can I free this memory?

Assuming that the C code is unmanaged (i.e. not .NET) I don't think
you can make this work.

In .NET you will not see the memory allocated by the .dll, you will
see a copy of it.

I don't think there is any way to make this work even if you weren't
using .NET - I don't think there is any way in totally unmanaged code
for memory allocated in a .dll to be freed by another entity.

I think you will need to allocate your own memory, pass a reference to
it to the .dll and change the .dll to fill in that memory with its
results. This might require two calls to the .dll, one to find out
how much memory to allocate and the other to get the data.
 
M

Michael Phillips, Jr.

I don't think there is any way to make this work even if you weren't
using .NET - I don't think there is any way in totally unmanaged code
for memory allocated in a .dll to be freed by another entity.

The memory allocators CoTaskMemAlloc and GlobalAlloc were designed for that
purpose.
 
S

silvia.fama

The memory allocators CoTaskMemAlloc and GlobalAlloc were designed for that
purpose.










- Mostra testo tra virgolette -


I read the documentation but every function to free memory suppose
that you allocated it with a defined function, e.g GlobalAlloc ->
GlobalFree.

So, if I pass a pointer to vbnet, allocated in a C function, it seems
that vbnet cannot free it, cause the pointer was not allocated in
vbnet. Is it right?

Thank you!
 
M

Michael Phillips, Jr.

So, if I pass a pointer to vbnet, allocated in a C function, it seems
that vbnet cannot free it, cause the pointer was not allocated in
vbnet. Is it right?

If the "c" function allocates memory with GlobalAlloc or CoTaskMemAlloc,
then the memory can be freed by any language including vb.net.

If the "c" function allocates memory using malloc or new, then vb.net cannot
free the unmanaged memory. You would have to create a "c" or
"c++" wrapper to free the memory.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top