473,624 Members | 2,272 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

MC++ and C# Console Application Memory issue?

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;
};

Nov 30 '05 #1
0 1601

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

43
12137
by: Dom | last post by:
can someone please help me display text in the console cout << "Testing"; in a different colour to the default one
0
410
by: Daniel Lidström | last post by:
Hi, I've been trying a long time now to generate some XML using MC++ and XmlSerializer. I have a piece of C# code that produces exactly what I want, but I simply can't get the MC++ code to write the same thing. Below I have included two minimal compilable samples that illustrate my problem. The C# code produces this XML: <?xml version="1.0" encoding="utf-8"?>
28
2061
by: carlbono | last post by:
I need a way for my console application to stay running and be able to be called at any time with some parameters, do it's work, then wait to be called again, repeat. Here's the details: I have a C# engine that's used to run a lot of complex financial calculations and return a report based upon the calculated results. My program is called by another program... let's just call it "MasterApp" and i'll call my program "Calc". Calc is...
5
2923
by: Scott Chang | last post by:
Hi all, I copied a set of VC++ version 6 source code of the 'cppdll'(2 projects) from a website and put the cppdll.cpp, cppdll.def, cpp.h, (as the 1st project) and test.cpp (as the 2nd project) into my Microsoft VC++ .NET 2002 - Windows XP Pro PC: //----cppdll.cpp---- #include "cppdll.h" //prevent function name from being mangled
6
4747
by: William F. Kinsley | last post by:
I am thinking of porting an existing MFC application to MC++ and I have created a simple MFC application to test the environment. My sample MFC application is compilied with the /clr switch. I added a basic WinForm class(via the wizard). In my MFC WinApp class I have a menu message handler to display the standard About Dialog. In the message handler I create, show and close the window. Everything appears to be working fine until I close...
1
1018
by: Eyal | last post by:
Hi, We have a class written in MC++ that has a data member of a class written in C#. When the MC++ goes out of scope, it's destructor is properly called and it's memory is released. However, the C# class Dispose() never gets called, and therefore it's memory is never released. Is this a known issue? Are we doing something wrong? or do we always have to explicitly call Dispose() on all C# objects embedded in MC++ classes? Currently this...
11
1530
by: John E Katich | last post by:
Being an old MFC guy, I'm having a heck of a time working with MC++. Can anybody tell me why the following code outputs 65 instead of an A. And yes I know 65 is the hex value for A, but why is it getting converted? And how to I get an A to be outputted? char c = 'A'; StringBuilder *s = new StringBuilder; s->Append(c); Console::WriteLine(s);
7
1424
by: Kevin Frey | last post by:
Using .NET 1.1. We have a mixed-mode assembly written in Managed C++ that we are using from an ASP.NET application that has been coded using C#. The mixed-mode assembly has its own "initialisation" routine to cater for any potential "mixed-mode DLL loading problem". Some of our code uses thread-local-storage, and in the DllMain for my mixed-mode assembly I did the appropriate initialisation of the thread-local-storage object on...
10
6329
by: Stephany Young | last post by:
When one uses the System.Diagnostics.Process.Start method to launch a common or garden Console application, one can set the WindowStyle property of the StartInfo object to ProcessWindowStyle.Hidden so that the window for the Console application is not visible. However, when using some of the 'advanced' properties of the StartInfo object, like Username, Password and Domain, the WindowsStyle property of the StartInfo object is ignored....
0
8242
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8177
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8681
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8341
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7170
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6112
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4084
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4183
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1488
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.