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?