473,387 Members | 1,394 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,387 software developers and data experts.

is it okay for native code to call calloc in c# app?

I'm calling a function in a DLL from my C# app. The function returns a
int* that it allocates using calloc.
Will this be a memory leak problem for my app?
Is there a way to fix it other than adding another method to the DLL
which frees the memory?

thanks!
Nov 15 '05 #1
2 1893
If you use platform invoke framework GC can't do anything with memory
allocated inside non-Net assemblies. So I would suggest to implement
analogue of Dispose in your dll.

HTH
Alex

"Yostage" <tr********@hotmail.com> wrote in message
news:40**************************@posting.google.c om...
I'm calling a function in a DLL from my C# app. The function returns a
int* that it allocates using calloc.
Will this be a memory leak problem for my app?
Is there a way to fix it other than adding another method to the DLL
which frees the memory?

thanks!

Nov 15 '05 #2
Yostage,

Yes, this will be a problem. Generally speaking, you can not depend on
memory management routines to be the same between programs. For example, if
you allocate memory with calloc in your program, the implementation of
corresponding free mechanism might not be the same, and you will end up with
problems all over the place.

The best way to handle this would be to have the function accept a
pointer with a pre-allocated buffer of memory, which your function then
populates.

The only mechanism that is guaranteed to work for allocating and freeing
memory between disparate programs (as far as I know) is the COM memory
manager (IMalloc interface). In your C++ code, you can call CoTaskMemAlloc
and return a memory buffer allocated with that. Then, the calling code can
call CoTaskMemFree (or in .NET, the static FreeCoTaskMem method on the
Marshal class).

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Yostage" <tr********@hotmail.com> wrote in message
news:40**************************@posting.google.c om...
I'm calling a function in a DLL from my C# app. The function returns a
int* that it allocates using calloc.
Will this be a memory leak problem for my app?
Is there a way to fix it other than adding another method to the DLL
which frees the memory?

thanks!

Nov 15 '05 #3

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

Similar topics

17
by: Joe Laughlin | last post by:
I've not used C much before, so I don't know how robust or good this code is. I'd appreciate any feedback or criticisms anyone has! Thanks, Joe #include <stdio.h> #include <string.h>
1
by: Scott McFadden | last post by:
What is the proper way to pass pointers by reference from managed c++ calls to native c++ calls? In managed C++, I have a pointer to an array of structures, that I pass to a native c++ method by...
14
by: Vlad Dogaru | last post by:
Hello, I am trying to learn C, especially pointers. The following code attempts to count the appearences of each word in a text file, but fails invariably with Segmentation Fault. Please help me...
5
by: ababeel | last post by:
Hi Pretty basic question.... is doing char **p = calloc(mysize, sizeof(char *)); same as doing char **p = malloc(mysize*sizeof(char *));
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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,...
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
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,...
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.