473,480 Members | 3,017 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

free unmanaged resource from managed code

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
Jan 9 '08 #1
8 1751
>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
Jan 9 '08 #2
On 9 jan, 13:26, David Lowndes <Dav...@example.invalidwrote:
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[]
Jan 9 '08 #3
LeXave wrote:
On 9 jan, 13:26, David Lowndes <Dav...@example.invalidwrote:
>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?

--
David Wilkinson
Visual C++ MVP
Jan 9 '08 #4
On 9 jan, 15:07, David Wilkinson <no-re...@effisols.comwrote:

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)
Jan 9 '08 #5
>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
Jan 9 '08 #6
>
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.

Jan 9 '08 #7

"Brian Muth" <bm***@mvps.orgwrote in message
news:ey**************@TK2MSFTNGP06.phx.gbl...

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.
Jan 9 '08 #8
>
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.

Jan 9 '08 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
11723
by: Bryan | last post by:
I have a multi-threaded C# console application that uses WMI (System.Management namespace) to make RPC calls to several servers (600+ ) and returns ScheduledJobs. The section of my code that...
5
2076
by: Barry Anderberg | last post by:
I'm using a tool by Sci-Tech called the .NET Memory Profiler. We have a massive .NET/C# application here and it has been exhibiting memory leak behavior for some time. Attempting to remedy the...
4
39928
by: Rachel Suddeth | last post by:
What is the difference between a managed/unmanaged resource, and how do you tell which is which? I'm trying to understand how to write some Dispose() methods, and we are supposed to put code that...
3
3473
by: zhphust | last post by:
I want to convert a object of a managed class to a unmanaged structure that has the same member with that managed class. Can anybody tell me how i can do it? Thanks in advance. -- zhphust...
1
1606
by: Sparhawk | last post by:
Hi, my company is going to migrate a large VC++ application to .NET to make use of Windows Forms (the old class library is not updated any more). We are not planning to migrate the rest of the...
3
2624
by: Steve | last post by:
I have former VC++ 6.0 application which was ported to .NET. Now we continue to develop this app under VS.NET. The app now contains both managed and unmanaged classes. Now I have a problem with...
4
2046
by: Maxwell | last post by:
Hello, Newbie question here for disposing of unmanaged resources in MC++.NET. I have a managed VS.NET 2003 MC++ wrapper class that wraps a unmanaged C++ dll. What I am trying to figure out is...
7
1550
by: thomson | last post by:
Hi All, Connecting to SQL Server we say its an Un Managed connection Can any one tell me whether this statement objConnection = new System.Data.OleDb.OleDbConnection(strConnectionString); ...
6
4082
by: Stephen Walch | last post by:
Our application environment consists of three basic layers: 1. Third-party unmanaged DLLs that were written before the CLR was invented and maintain a significant amount of information (including...
0
7060
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6760
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7022
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4501
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3013
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3004
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1311
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
572
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
206
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.