Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / October 2004

Tip: Looking for answers? Try searching our database.

IJW Memory Management

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Luis Fajardo - 05 Oct 2004 16:47 GMT
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?

Thanks for your help
Mattias Sj?gren - 05 Oct 2004 21:18 GMT
>My concern is if I should worry about destroying the objects created
>in the MANAGED section:

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

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

As long as the caller of ExecuteUnManageCode is prepared to free the
error message returned in errMsg with LocalFree it's 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?

It's as expected.

Mattias

Signature

Mattias Sjögren [MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Luis Fajardo - 07 Oct 2004 15:11 GMT
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

"Mattias Sjögren" wrote:

> >My concern is if I should worry about destroying the objects created
> >in the MANAGED section:
[quoted text clipped - 16 lines]
>
> Mattias

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.