free unmanaged resource from managed code

L

LeXave

Hi all

In C++/CLI, is there a way to free an array allocated from an
unmanaged dll ? In my managed code, I call a function from that dll
which returns me an array of bytes. But how can I free it ?

Thanks
 
D

David Lowndes

In C++/CLI, is there a way to free an array allocated from an
unmanaged dll ?

Only if you know (and have the exact same access to) the memory
manager that allocated it.
In my managed code, I call a function from that dll
which returns me an array of bytes. But how can I free it ?

More details and a short example are probably needed before a
definitive answer can be given.

Dave
 
L

LeXave

More details and a short example are probably needed before a
definitive answer can be given.

in my unmnaged dll :

int *function()
{
int *a = new int[16];
// fill array, etc...
return a;
}

in my managed dll (C++/CLI):

tab = function();
// misc. operations with tab...
delete[] tab; // error

so, in my managed dll, once I finish to play with tab, I have to free
it, but my complier don't like my delete[]
 
D

David Wilkinson

LeXave said:
More details and a short example are probably needed before a
definitive answer can be given.

in my unmnaged dll :

int *function()
{
int *a = new int[16];
// fill array, etc...
return a;
}

in my managed dll (C++/CLI):

tab = function();
// misc. operations with tab...
delete[] tab; // error

so, in my managed dll, once I finish to play with tab, I have to free
it, but my complier don't like my delete[]

LeXave:

Are you sure that both DLL's are using the same version of the CRT?

But, personally, I do not like this mixed new/delete across DLL
boundaries. What happens if you export another function from your
unmanaged DLL:

void free_function(int* a)
{
delete [] a;
}

and call it from your managed DLL?
 
L

LeXave

Dave,
What happens if you export another function from your
unmanaged DLL:

It would certainly be the best solution, but the problem is that I
don't own the source of that dll, so I can't modify it :(

Both dll use the same version of the CRT (compiled both with VS 2005)
 
D

David Lowndes

Both dll use the same version of the CRT (compiled both with VS 2005)

You can't mix debug & release components (they have different heaps),
and they both have to be built to use the DLL CRT option.

Dave
 
B

Brian Muth

It would certainly be the best solution, but the problem is that I
don't own the source of that dll, so I can't modify it :(

Then a better solution would be to create your own C++/CLI wrapper around the DLL to put a managed front face to it. For example,
you could copy the int[] array to a managed array, then delete[] the unmanaged array before returned the managed array.
 
B

Ben Voigt [C++ MVP]

Brian Muth said:
It would certainly be the best solution, but the problem is that I
don't own the source of that dll, so I can't modify it :(

Then a better solution would be to create your own C++/CLI wrapper around
the DLL to put a managed front face to it. For example, you could copy the
int[] array to a managed array, then delete[] the unmanaged array before
returned the managed array.

As I understand the original post, that's exactly what's not working.
Perhaps the original DLL has statically linked the CRT and returned pointers
allocated by that static CRT, in which case there is no way to safely free
the memory.
 
B

Brian Muth

As I understand the original post, that's exactly what's not working.
Perhaps the original DLL has statically linked the CRT and returned pointers
allocated by that static CRT, in which case there is no way to safely free
the memory.

Aaggh. That makes sense.
 

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