By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,273 Members | 1,037 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,273 IT Pros & Developers. It's quick & easy.

MC++ and C# Console Application Memory issue?

P: n/a
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 UnmanagedBridgeCallback(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...what 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::Threading;
using namespace System::Runtime::InteropServices;

////////////////////////////////////////////////////////////////////
// Unmanaged Code
typedef struct
{
long x;
long y;
char str[50];

} UStruct;

class UClass
{
private:
void (*pUnmanagedFunc_)(const UStruct);

public:
// Constructors
UClass() :pUnmanagedFunc_(NULL) {};
~UClass() {
UnRegisterUnmanagedCallBack();
};

void RegisterUnmanagedCallBack(void (*funcPtr)(const UStruct item))
{
if ( pUnmanagedFunc_ == NULL )
{
pUnmanagedFunc_ = funcPtr;
}
else
{
throw("CallBack already set");
}
};

void UnRegisterUnmanagedCallBack() {
pUnmanagedFunc_ = NULL;
};

void FireUnmanagedCallback(const UStruct item)
{
if ( pUnmanagedFunc_ != NULL )
{
(*pUnmanagedFunc_)(item);
}
};
};

////////////////////////////////////////////////////////////////////
//Managed Code
[StructLayout(LayoutKind::Sequential, CharSet=CharSet::Ansi, Pack=8)]
__gc struct MStruct
{
public:
System::Int32 x;
System::Int32 y;

[MarshalAs(UnmanagedType::ByValTStr, SizeConst=50)]
System::String *str;

String* ToString()
{
return String::Format("X:{0} Y:{1}
STR:{2}",Convert::ToString(x),Convert::ToString(y) ,str);
}

} ;

__gc class MClass
{
private:
UClass *pUnmanagedClass_ ;

public:
MClass() : pUnmanagedClass_(NULL)
{
pUnmanagedClass_ = new UClass(); //unmanaged heap
pUnmanagedClass_->RegisterUnmanagedCallBack(&(_UClassBridge::Unmana gedBridgeCallback));
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::StructureToPtr(item,&uItem,true);

pUnmanagedClass_->FireUnmanagedCallback(uItem);
Thread::Sleep(100);
}
}

__delegate void DataChangedEventHandler(MStruct *item);
static MClass* pMe_ = 0;
DataChangedEventHandler *pManagedDelegate_;

private: //internal bridge class
__nogc class _UClassBridge
{
public:
static void UnmanagedBridgeCallback(const UStruct item)
{
Console::WriteLine("UnmanagedBridgeCallback: Fired");

UStruct uItem = item;
MStruct *mItem = new MStruct();
Marshal::PtrToStructure(&uItem,mItem);

MClass::pMe_->pManagedDelegate_->Invoke(mItem);
}
};
};

////////////////////////////////////////////////////////////////////
/// MC++ Console Application
__gc class ConsoleCallbackClass
{
public:
static void Notification(MStruct *item)
{
Console::WriteLine(S"Got Item:{0}",item->ToString());
}
};

int _tmain()
{
MClass *managedClass = new MClass();
managedClass->pManagedDelegate_ = new
MClass::DataChangedEventHandler(NULL,&ConsoleCallb ackClass::Notification);

managedClass->RunSimulation();

return 0;
};

Nov 30 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.