473,322 Members | 1,540 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Ref count problem when using managed extensions

I have imported a legacy C++ project into a VisualStudio.NET 2003 solution.
I have some new C# projects in the solution as well. I am using the managed
extensions in my C++ project so that I can use the managed C# objects within
my C++ project. Here's the problem: I am getting a debug assertion in
cmdtarg.cpp (line 48) of my main CWinApp derived class when I exit the
application:

CCmdTarget::~CCmdTarget()
{
....
ASSERT(m_dwRef <= 1); // <<<< Line 48 where I am getting the assertion.
....
}

The strange thing is that I only get this assertion if I have created a
managed gcroot<> object within the main CWinApp derived class. Why does
creating a managed object increase the ref count on CWinApp? That seems
very odd. Here is how I am creating the managed object within CWinApp:

In the header:
#using <mscorlib.dll>
#using <system.dll>
#using <system.windows.forms.dll>
#using "PetToolTCPLib.dll" // Library of my C# managed classes
using namespace PetToolTCPLib;
using namespace System;

In the .cpp:
#pragma push_macro("new")
#undef new
m_pTCPTool = new CToolMain();
#pragma pop_macro("new")

m_pTCPTool->Show();

In the destructor of the .cpp:
m_pTCPTool->Close();
m_pTCPTool->Dispose();
m_pTCPTool = NULL;

Once again, my question is: Why does creating a managed object increase the
ref count on CWinApp?
How do I resolve this problem?

Ben
Nov 17 '05 #1
3 1787
I solved the problem. I was not properly "disposing" my gcroot object.
Doing so now enables the CWinApp ref count to decrement and shutdown without
the assertion. I'm still not sure why that refcount is affected by my
gcroot object.

"Ben Terry" <bt@charter.net> wrote in message
news:eC****************@TK2MSFTNGP10.phx.gbl...
I have imported a legacy C++ project into a VisualStudio.NET 2003 solution. I have some new C# projects in the solution as well. I am using the managed extensions in my C++ project so that I can use the managed C# objects within my C++ project. Here's the problem: I am getting a debug assertion in
cmdtarg.cpp (line 48) of my main CWinApp derived class when I exit the
application:

CCmdTarget::~CCmdTarget()
{
...
ASSERT(m_dwRef <= 1); // <<<< Line 48 where I am getting the assertion.
...
}

The strange thing is that I only get this assertion if I have created a
managed gcroot<> object within the main CWinApp derived class. Why does
creating a managed object increase the ref count on CWinApp? That seems
very odd. Here is how I am creating the managed object within CWinApp:

In the header:
#using <mscorlib.dll>
#using <system.dll>
#using <system.windows.forms.dll>
#using "PetToolTCPLib.dll" // Library of my C# managed classes
using namespace PetToolTCPLib;
using namespace System;

In the .cpp:
#pragma push_macro("new")
#undef new
m_pTCPTool = new CToolMain();
#pragma pop_macro("new")

m_pTCPTool->Show();

In the destructor of the .cpp:
m_pTCPTool->Close();
m_pTCPTool->Dispose();
m_pTCPTool = NULL;

Once again, my question is: Why does creating a managed object increase the ref count on CWinApp?
How do I resolve this problem?

Ben

Nov 17 '05 #2
Scratch that...still haven't solved the problem. Anyone have any ideas
about this one?

"Ben Terry" <bt@charter.net> wrote in message
news:eC****************@TK2MSFTNGP10.phx.gbl...
I have imported a legacy C++ project into a VisualStudio.NET 2003 solution. I have some new C# projects in the solution as well. I am using the managed extensions in my C++ project so that I can use the managed C# objects within my C++ project. Here's the problem: I am getting a debug assertion in
cmdtarg.cpp (line 48) of my main CWinApp derived class when I exit the
application:

CCmdTarget::~CCmdTarget()
{
...
ASSERT(m_dwRef <= 1); // <<<< Line 48 where I am getting the assertion.
...
}

The strange thing is that I only get this assertion if I have created a
managed gcroot<> object within the main CWinApp derived class. Why does
creating a managed object increase the ref count on CWinApp? That seems
very odd. Here is how I am creating the managed object within CWinApp:

In the header:
#using <mscorlib.dll>
#using <system.dll>
#using <system.windows.forms.dll>
#using "PetToolTCPLib.dll" // Library of my C# managed classes
using namespace PetToolTCPLib;
using namespace System;

In the .cpp:
#pragma push_macro("new")
#undef new
m_pTCPTool = new CToolMain();
#pragma pop_macro("new")

m_pTCPTool->Show();

In the destructor of the .cpp:
m_pTCPTool->Close();
m_pTCPTool->Dispose();
m_pTCPTool = NULL;

Once again, my question is: Why does creating a managed object increase the ref count on CWinApp?
How do I resolve this problem?

Ben

Nov 17 '05 #3
Hello Ben,

Ben Terry wrote:
I am using the
managed
extensions in my C++ project so that I can use the managed C# objects within
my C++ project. Here's the problem: I am getting a debug assertion in
cmdtarg.cpp (line 48) of my main CWinApp derived class when I exit the
application:


I had the same problem. This seems to be a known issue with the currnet
version of .Net.
Leo Chen from Microsoft posted a message to
microsoft.public.dotnet.languages.csharp
Currently, we can work around this problem using following methods:

1. Handle the CWinApp::ExitInstance. Use code similar to the following
to
release the additional reference to the message filter.
#include <afxole.h>

BOOL CMFCDlgApp::ExitInstance()
{
COleMessageFilter* pFilter = AfxOleGetMessageFilter();
ULONG lCount = pFilter->m_xMessageFilter.Release();
if(lCount<=1)
pFilter->m_xMessageFilter.AddRef();
return CWinApp::ExitInstance();
}
2. Another method can be applied on the C# side. We can call GC.Collect >tocollect the object references after the form is closed:


public bool ShowDbx()
{DTest1 dlg1 = new DTest1();
dlg1.ShowDialog();
dlg1.Close();
dlg1.Dispose();

GC.Collect();

return true;
}

I first tried the solution number one, but then realized I would have to
call GC.Collect() after every MessageBox::Show, which somehow didn't
look very promising to me.

So I chose the 2nd solution which seems to work just fine.

Hope that helps,

Ecki
Nov 17 '05 #4

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

Similar topics

14
by: Ron James | last post by:
C++ is a great language. You can instantiate an object on the stack, grab whatever resources you need, and free them in the destructor. Object destructors are called in the correct order (the...
2
by: Jay | last post by:
Hi all, I'm looking for creating managed extensions, to interface between .Net and orbix 3.x or 6.1. can anyone please help me in finding out the information for creating managed extensions to...
5
by: Adam McKee | last post by:
We are using Visual Studio.NET 2003 in our project with .NET framework 1.1. One of our libraries is a mixed-mode dll assembly consisting of one managed C++ library, and several unmanaged C++...
8
by: Mas L via DotNetMonster.com | last post by:
Hi, I have a c++ source code which I can compile to be a DLL (in VS.NET 2003). And I need to use it in a C# program. After I compiled/build the C++ code to a DLL, I add it as a Reference in my...
3
by: Gustavo L. Fabro | last post by:
Greetings! I'm a newbie in Visual C++ .NET (have programmed in Borland C++ and Builder for long) and I am trying to do a very simple thing, but I'm stuck. I created an (unmanaged) DLL project...
8
by: marcus.kwok | last post by:
I am having a weird problem and I have can't figure out why it is happening. I create an OpenFileDialog and set a filename filter. When the dialog first opens, the filter works correctly, and...
9
by: DrZogg | last post by:
It can't be... say it isn't so... Yes.. there is a fly in the ointment This bug requires 3 projects... yes this is a real world production issue not some tinker toy thing. Start with a C#...
2
by: kelvin.koogan | last post by:
Our company has a mix of users with VS2003 & VS2005. I'm am having difficulty working out how components written with these 2 tools can be written together. I want to be able to a) incorporate a...
1
by: Patrick Philippot | last post by:
Hi, According to this discussion: http://forums.msdn.microsoft.com/en-US/netfxbcl/thread/1428326d-7950-42b4-ad94-8e962124043e/ it is very unsafe to write shell extensions in managed code...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.