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

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(dllexport) 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(dllexport) TestFree(char **p)

{

::free(*p);

}

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

char *pszString;

Test(&pszString);

strB->Append(pszString);

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

TestFree(&pszString); // 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 1368
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.com> wrote in message
news:eP**************@tk2msftngp13.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(dllexport) 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(dllexport) TestFree(char **p)

{

::free(*p);

}

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

char *pszString;

Test(&pszString);

strB->Append(pszString);

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

TestFree(&pszString); // 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
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...
6
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...
2
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...
4
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.