473,395 Members | 1,457 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

calling C++ from C#

1
Hello,

I have unmanaged C++ dll, that I use in a managed code (C++). I call this managed code from C#. It seems to work fine, but I am not sure about the memory management, should I free the data? Here is sample of my code:

#pragma unmanaged

void unmanaged_free (void *p_)
{
free (p_);
}
void *unmanaged_malloc (int size_)
{
return malloc (size_);
}

#pragma managed

void Dnzmq::send (int eid_, void * data_, size_t size_)
{
// Copy data to the unmanaged buffer.
void *data = unmanaged_malloc (size_);
assert (data);
memcpy (data, data_, size_);

// Forward the call to native 0MQ library.
zmq::message_t msg (data, size_, unmanaged_free);
context->api_thread->send (eid_, msg);
}
I call send() function from C#. My question is: who deallocates the function argument data_ ? Is it correct to free *data, that was allocated in unmanaged part of the code?
Jan 13 '09 #1
2 2232
Banfa
9,065 Expert Mod 8TB
All memory that has been allocated needs to be freed apart from memory that is allocated with gcnew (which is automatically garbage collected for you).

However the zmq::message_t class (structure?) appears to take a pointer to function used to free the data suggesting that may be it is going to call the free function itself automatically for you.

You need to understand exactly how the zmq::message_t class works in order to understand why you are passing a pointer to the free function and what the class does with that pointer.

I have to say it looks like poor programming since the class ought to be able to take the original pointer to the data and handle the malloc and free itself rather than have the caller call malloc and pass the free function pointer to the class.
Jan 13 '09 #2
vekipeki
229 Expert 100+
1. You should not free data_, since it is being passed from the managed side, where it was allocated. During P/Invoke .NET marshaller will pin your managed object so it doesn't get moved by the GC. If your unmanaged function saves a reference to your pointer, or runs in background, then you must pin your object manually (using a GCHandle) and release it when needed.

If your object's managed representation is not the same as native (you are using the MarshalAsAttribute all over the place), then marshaller will create a copy of your object which matches the native representation, and also free it after use automatically.

In any situation, this object can be considered managed during entire call from c#, so you don't have to free it.

2. It seems like you are passing a pointer to unmanaged_free() in your message, if this means that unmanaged_free() will get called at the end with data passed as parameter, then I guess you don't need to free it. This part should be well understood before deciding it. At least create a small test app and watch if your process grows in Task Manager :)
Jan 14 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Asapi | last post by:
1. Are linkage convention and calling convention referring to the same thing? 2. Does calling convention differ between languages C and C++? 3. How does calling convention differ between...
8
by: Muthu | last post by:
I've read calling conventions to be the order(reverse or forward) in which the parameters are being read & understood by compilers. For ex. the following function. int Add(int p1, int p2, int...
7
by: Klaus Friese | last post by:
Hi, i'm currently working on a plugin for Adobe InDesign and i have some problems with that. I'm not really a c++ guru, maybe somebody here has an idea how to solve this. The plugin is...
5
by: Nick Flandry | last post by:
I'm running into an Invalid Cast Exception on an ASP.NET application that runs fine in my development environment (Win2K server running IIS 5) and a test environment (also Win2K server running IIS...
3
by: Mike | last post by:
Timeout Calling Web Service I am calling a .NET 1.1 web service from an aspx page. The web service can take several minutes to complete its tasks before returning a message to the aspx page. ...
2
by: Geler | last post by:
A theoretical question: Sorry if its a beginner question. Here is a quote from the MSDN explaning the C/C++ calling convention.. It demonstrates that the calling function is responsible to clean...
47
by: teju | last post by:
hi, i am trying 2 merge 2 projects into one project.One project is using c language and the other one is using c++ code. both are working very fine independently.But now i need to merge both...
7
by: =?Utf-8?B?UVNJRGV2ZWxvcGVy?= | last post by:
I have a C# logging assembly with a static constructor and methods that is called from another C# Assembly that is used as a COM interface for a VB6 Application. Ideally I need to build a file...
10
by: sulekhasweety | last post by:
Hi, the following is the definition for calling convention ,which I have seen in a text book, can anyone give a more detailed explanation in terms of ANSI - C "the requirements that a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...

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.