all groups > dotnet interop > october 2004 >
You're in the

dotnet interop

group:

IJW Memory Management


IJW Memory Management Luis Fajardo
10/5/2004 8:47:03 AM
dotnet interop:
I need the help of the C++ gurus out there. I have the following code, it's
working great, but I'm concern about the lack memory management I'm doing. I
just want to confirm if I'm doing something wrong or my code is fine. I
don't want to put this code in production and find out in some days that my
servers are going to crash.

I have the need to integrate an old system, written in C with .NET. What
I'm doing is using an intermidiate C++.NET class as a bridge to get benefit
of the IJW technology, here is a sample code:

#include "stdafx.h"
#include "MyCPPClass.Interop.h"

#pragma managed
#using <mscorlib.dll>
#using "MyCSharpAssembly.dll"
using namespace System;
using namespace System::Runtime::InteropServices;
using namespace MyCSharpAssembly;

void ExecuteManageCode(char *msg, char **errMsg)
{
try{
MyCSharpAssembly::CSharpClass *cSharpClass = new
MyCSharpAssembly::CSharpClass();
cSharpClass->set_Msg(msg);
cSharpClass->Execute();
}
catch(Exception *e){
*errMsg =
(char*)Marshal::StringToHGlobalAnsi(e->get_Message()).ToPointer();
}
}

#pragma unmanaged
extern "C" {
__declspec(dllexport) void __stdcall ExecuteUnManageCode(char *msg, char
**errMsg)
{
ExecuteManageCode(msg, errMsg);
}
}


I modify the original code for simplicity, but this is pretty much what I'm
doing. My concern is if I should worry about destroying the objects created
in the MANAGED section:

MyCSharpAssembly::CSharpClass *cSharpClass = new
MyCSharpAssembly::CSharpClass();

or the GC should take care of that, and the other question is that in the
UNMANAGED section, should I do anything important here, or it's pretty much
fine?


NOTE: I noticed that after excuting my code, if the C application(client)
is running I can't override or replace the C++.NET assembly, seems to be
"lock" by my C application, no matter if the execution to the method finished
already, is this OK?

Re: IJW Memory Management Mattias Sjögren
10/5/2004 10:18:45 PM

[quoted text, click to view]

No, managed objects are handled by the CLR garbage collector.


[quoted text, click to view]

As long as the caller of ExecuteUnManageCode is prepared to free the
error message returned in errMsg with LocalFree it's fine.


[quoted text, click to view]

It's as expected.



Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Re: IJW Memory Management Luis Fajardo
10/7/2004 7:11:06 AM
Thanks for your comments.

I have one more question, I'm kind of using what I know and I'm not consider
myself any close to a C++ expert, I use it when I need it, and pretty much
copy/paste code from samples that I found related what I need. My question
for you is, do you see a "BETTER" way to do this integration between C and C#
other than a C++.NET IJW Bridge?

Thanks

[quoted text, click to view]
AddThis Social Bookmark Button