473,606 Members | 2,381 Online
Bytes | Software Development & Data Engineering Community
+ 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 1764
>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(i nt* 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.org wrote in message
news:ey******** ******@TK2MSFTN GP06.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
11757
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 performs the query is contained in a delegate function that I execute via a second thread. On 1 or 2 of the 600+ servers the query hangs. I've tried to use Thread.Join() coupled with a Thread.Abort() but this does not kill the thread. Based on...
5
2088
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 problems I am employing the aforementioned software in trying to track down the problems. After an hour or so of program execution, a snapshot of the managed heap shows 27,025 BinaryReader objects as having been garbage collected but never...
4
39976
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 deals with managed in one place, and code that deals with unmanaged in another place, but I can't seem to find anything that clearly explains what that means. I think if I used a Windows API function to optain a handle, that handle would be an...
3
3497
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
1638
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 code which works well. I understand the basic concept: our code is unmanaged, Windows Forms is Managed and Unmanaged may not call Managed code. I read about Wrappers, PInvoke, Runtime Callable Wrappers for COM and about It just
3
2642
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 usage of resources in managed classes. For example I want to display warning message and want to load that warning message from the resource file. The problem is that in this mixed managed/unmanaged project I don't have pure managed resources...
4
2079
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 what is the "best practice" for disposing of pointers to unmanaged classes that you have newed in your constructor in MC++ For a better description of what is the standard affair I have tried looking online at:
7
1558
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); is a call to an unmanaged resource
6
4111
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 memory management and connection pooling constructs) as static variables which were intended to scoped to the process. 2. Managed C++ assemblies (.NET 1.1) that wrap the unmanaged DLLs as nice neat classes with managed interfaces.
0
8016
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7955
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8096
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8306
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6773
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5966
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5466
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2448
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 we have to send another system
1
1557
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.