Hello,
I recently completed a MC++ (VS2003) DLL that wraps a non MFC C++ DLL
and need to use it in a MC++ Console Application (no forms/guis of any
kind just output to console).
Trouble is that when I ran it and looked at memory usage (in Windows
task manager) it looked as if there was a very slow leak. To isolate
the issue:
1. I ran just my C++ DLL in a Win32 console and looked at the memory
use...perfect never changed stayed at about 1MB
2. I ran the MCC+ wrapper (around the C++ DLL) in a MC++ console,
memory started to climb after it was started and only gets relased if I
minimize the window...but then starts its climb up again slowly but
surely. Started at about 7MB, kept increaing until about 12MB then I
minimized the windows it dropped to 568K and then started climbing
again well past 12MB until I stopped it.
I figured it was probably something I did in the MC++ to C++ DLL
wrapping so I did the sample console applicatin test with the code
below to isolate the issue without any of my code and this sample still
slowly creeps up in memory use . The memory kept climbing up every so
slowly, but it still crept up.
Any help would be greatly appretiated,
Here is the sample MC++ Console project below (note I just modified the
sample on http://www.voidnish.com/articles/Sho...px?code=cbwijw
so that it passed a struct and fired in a loop) :
The project is a MC++ console application with a Unamagned class called
UClass and a Managed class called MClass with a internal __nogc class
for bridging the unmanaged callback to the managed code.There is a
unamanged struct UStruct that gets marshalled to a managed struct
MStruct in the callback.
The only place i could see as a potential place for leaking in this
code is in the static void UnmanagedBridge Callback(const UStruct item)
method in the __nogc class where I call PtrToStructure but I am a
noviceat this so I dont really know. Perhaps I am not using
PtrToStructure correctly? If you leave this sample running for some
time you will definitely see the memory to slowly creep up
I should note the really weird thing is if you minimize the console
window while its running the memory drops from about 8MB to like 568K
immediatey...wh at is that some kind of screen buffer? (even after this
it definitely starts to keep up again beyond 8MB.). My Screen buffer is
set to 8,000K BTW.
// This is the main project file for VC++ application project
// generated using an Application Wizard.
#include "stdafx.h"
#using <mscorlib.dll >
#using <System.dll>
#include <tchar.h>
#include <windows.h>
using namespace System;
using namespace System::Threadi ng;
using namespace System::Runtime ::InteropServic es;
////////////////////////////////////////////////////////////////////
// Unmanaged Code
typedef struct
{
long x;
long y;
char str[50];
} UStruct;
class UClass
{
private:
void (*pUnmanagedFun c_)(const UStruct);
public:
// Constructors
UClass() :pUnmanagedFunc _(NULL) {};
~UClass() {
UnRegisterUnman agedCallBack();
};
void RegisterUnmanag edCallBack(void (*funcPtr)(cons t UStruct item))
{
if ( pUnmanagedFunc_ == NULL )
{
pUnmanagedFunc_ = funcPtr;
}
else
{
throw("CallBack already set");
}
};
void UnRegisterUnman agedCallBack() {
pUnmanagedFunc_ = NULL;
};
void FireUnmanagedCa llback(const UStruct item)
{
if ( pUnmanagedFunc_ != NULL )
{
(*pUnmanagedFun c_)(item);
}
};
};
////////////////////////////////////////////////////////////////////
//Managed Code
[StructLayout(La youtKind::Seque ntial, CharSet=CharSet ::Ansi, Pack=8)]
__gc struct MStruct
{
public:
System::Int32 x;
System::Int32 y;
[MarshalAs(Unman agedType::ByVal TStr, SizeConst=50)]
System::String *str;
String* ToString()
{
return String::Format( "X:{0} Y:{1}
STR:{2}",Conver t::ToString(x), Convert::ToStri ng(y),str);
}
} ;
__gc class MClass
{
private:
UClass *pUnmanagedClas s_ ;
public:
MClass() : pUnmanagedClass _(NULL)
{
pUnmanagedClass _ = new UClass(); //unmanaged heap
pUnmanagedClass _->RegisterUnmana gedCallBack(&(_ UClassBridge::U nmanagedBridgeC allback));
pMe_ = this;
}
~MClass(){
delete pUnmanagedClass _;
};
void RunSimulation()
{
MStruct *item = new MStruct();
item->x = 1;
item->y = 2;
item->str = "Test";
for(int i = 0;i< 100000;i++)
{
item->x = i+1;
UStruct uItem;
Marshal::Struct ureToPtr(item,& uItem,true);
pUnmanagedClass _->FireUnmanagedC allback(uItem);
Thread::Sleep(1 00);
}
}
__delegate void DataChangedEven tHandler(MStruc t *item);
static MClass* pMe_ = 0;
DataChangedEven tHandler *pManagedDelega te_;
private: //internal bridge class
__nogc class _UClassBridge
{
public:
static void UnmanagedBridge Callback(const UStruct item)
{
Console::WriteL ine("UnmanagedB ridgeCallback: Fired");
UStruct uItem = item;
MStruct *mItem = new MStruct();
Marshal::PtrToS tructure(&uItem ,mItem);
MClass::pMe_->pManagedDelega te_->Invoke(mItem );
}
};
};
////////////////////////////////////////////////////////////////////
/// MC++ Console Application
__gc class ConsoleCallback Class
{
public:
static void Notification(MS truct *item)
{
Console::WriteL ine(S"Got Item:{0}",item->ToString());
}
};
int _tmain()
{
MClass *managedClass = new MClass();
managedClass->pManagedDelega te_ = new
MClass::DataCha ngedEventHandle r(NULL,&Console CallbackClass:: Notification);
managedClass->RunSimulation( );
return 0;
};