473,803 Members | 3,534 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Freeing Memory allocted in unmanged C code

skg
I am passing the address of pointer like char** from managed extension and
getting the
its initialized value from a C library dll.

How can i free the memory from the code in Managed Extension ?

Here is a sample C function i am calling from managed extension.

C dll Code
==============

extern "C" void __declspec(dlle xport) Test(char **p)
{

int nSize = 10000;

*p= (char*)::malloc (nSize);

while(--nSize)

*(*p+nSize)='A' ;

}

// Iam currently calling back to C dll to free the memory it allocated.

extern "C" void __declspec(dlle xport) TestFree(char **p)

{

::free(*p);

}

Managed Extension Code Function.
=============== =====
StringBuilder *strB;

char *pszString;

Test(&pszString );

strB->Append(pszStri ng);

//Marshal::FreeCo TaskMem((IntPtr )pszString); //This throws exception.after
certain number of iterations of calling this function

TestFree(&pszSt ring); // I am currently calling this since above statement
does not work.

How can i free the memory in Managed Extension ?
Thanks!!!!
Nov 17 '05 #1
1 1382
Memory allocated using malloc() must be freed by calling free() as you did.
Memory allocated using CoTaskMemAlloc must be freed using FreeCoTaskMem.
Willy.
"skg" <sk*@yahoo.co m> wrote in message
news:eP******** ******@tk2msftn gp13.phx.gbl...
I am passing the address of pointer like char** from managed extension and
getting the
its initialized value from a C library dll.

How can i free the memory from the code in Managed Extension ?

Here is a sample C function i am calling from managed extension.

C dll Code
==============

extern "C" void __declspec(dlle xport) Test(char **p)
{

int nSize = 10000;

*p= (char*)::malloc (nSize);

while(--nSize)

*(*p+nSize)='A' ;

}

// Iam currently calling back to C dll to free the memory it allocated.

extern "C" void __declspec(dlle xport) TestFree(char **p)

{

::free(*p);

}

Managed Extension Code Function.
=============== =====
StringBuilder *strB;

char *pszString;

Test(&pszString );

strB->Append(pszStri ng);

//Marshal::FreeCo TaskMem((IntPtr )pszString); //This throws
exception.after certain number of iterations of calling this function

TestFree(&pszSt ring); // I am currently calling this since above statement
does not work.

How can i free the memory in Managed Extension ?
Thanks!!!!

Nov 17 '05 #2

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

Similar topics

5
22979
by: disco | last post by:
I am working on this example from a book "C Primer Plus" by Prata 4th edition - p. 672. There is no erata on this problem at the publisher's website. 1) Is it a violation of copyright laws to post example code from a book to a newsgroup? 2) The program crashes as it tries to free memory and I would like to know the best way to correct THIS section of the code. By assigning current = head, current now points to the first structure in...
6
2766
by: Fernando Cacciola | last post by:
Help me out here please: While watching Brad Abraham's MSDN TV talk about the Dispose pattern, refering to: public virtual void Dispose ( bool disposing ) { if ( disposing ) { <-- WHAT GOES HERE -->
2
1412
by: Ravi Ambros Wallau | last post by:
Hi: We have an ASP.NET application that has a memory consumption issue - it's go up to 150 MB in a few time (some hours). The application uses two Microsoft Application Blocks, the User Interface and the Data Access. The User Interface block was highly modified to support our needs. One of the changes is a process that "cleanup" unused tasks, when a fresh new one is open. This works fine, but to deallocate the old task I only remove it...
4
36539
by: Atul Sureka | last post by:
Hi, I want to free the object memory in C# - like we do using 'delete' keyword in C++. Lets say I have an object of some class and I want to explicitly free the memory. C# do not have any free or delete keyword to do so. Also I don't want to wait for the GC to be called.
0
10550
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10295
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
9125
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
7604
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
5501
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5633
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4275
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
2
3799
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2972
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.