473,753 Members | 6,868 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Memory leak when using COM library

We have found a memory leak when using a COM library with a C# application.
The leak appears only if the C# application is compiled with the /optimize
flag. It goes away when the C# application is compiled with the /debug flag.
The COM library is coded to fire asynchronous events, which are handled by
the C# application. It is in the firing and handling of these events that
the leak occurs.

Why does the optimized application leak while the debug application does
not?
Is there a fix available?
Is there another way to do asynchronous event handling that will circumvent
this problem?

Attached is a small test case that recreates the problem. The only code of
real interest in the COM library is the event firing code. If the problem
that we see is in the COM library, it would have to be in this piece of code.
However, this code is more or less the output of a wizard, it is pretty
standard stuff. The C# application is also trivial, with the only item of
any real interest being the event handler. Once again, the code has been
pared down to the point of being trivial. Any insights into asynchronous
event handling through an interop assembly is appreciated.

Thanks,
Giovanni Boschi
Sonic Software Corporation
Nov 16 '05 #1
3 2546
Giovanni,

Can you post the test case?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Giovanni Boschi" <Gi************ @discussions.mi crosoft.com> wrote in
message news:F3******** *************** ***********@mic rosoft.com...
We have found a memory leak when using a COM library with a C#
application.
The leak appears only if the C# application is compiled with the /optimize
flag. It goes away when the C# application is compiled with the /debug
flag.
The COM library is coded to fire asynchronous events, which are handled by
the C# application. It is in the firing and handling of these events that
the leak occurs.

Why does the optimized application leak while the debug application does
not?
Is there a fix available?
Is there another way to do asynchronous event handling that will
circumvent
this problem?

Attached is a small test case that recreates the problem. The only code
of
real interest in the COM library is the event firing code. If the problem
that we see is in the COM library, it would have to be in this piece of
code.
However, this code is more or less the output of a wizard, it is pretty
standard stuff. The C# application is also trivial, with the only item of
any real interest being the event handler. Once again, the code has been
pared down to the point of being trivial. Any insights into asynchronous
event handling through an interop assembly is appreciated.

Thanks,
Giovanni Boschi
Sonic Software Corporation

Nov 16 '05 #2
I have a simple test driver that reproduces it, but I haven't found a way to
attach files to these posts - if the full test code and library would help
and there's an email address I can send it to, let me know.

Thanks for your help,
G.

This is the C# code that appears to leak:

amespace SonicExample
{
/// <summary>
///
/// </summary>
public class MessageListener : CSLEAKTESTVS6Li b.IJMSMessageLi stener
{

private int msgCount = 0;

public MessageListener ()
{
}
/// <summary>
/// This called by the JMSCom frame work when a mesage is received
/// </summary>
/// <param name="pJMSMessa ge"></param>
[MTAThread]
public void onMessage(CJMSM essage pJMSMessage)
{
System.Runtime. InteropServices .Marshal.Releas eComObject(pJMS Message);
pJMSMessage = null;
GC.Collect();
++msgCount;
if ((msgCount % 1000) == 0)
Console.WriteLi ne ("Message {0}", msgCount);
}
}
}

This is the code, inside the COM library, that registers and fires
callbacks, respectively - this is fairly boilerplate stuff and works
correctly if the COM client is C++, or C# compiled as debug.

// register the callback
if (pListener != NULL)
{
hr = AtlAdvise (thisUnknown,
listenerUnknown ,
IID_IJMSMessage Listener,
&m_MessageListe nerCookie);
}

// The callback routine
// messageDelivery and Fire_onMessage get called repeatedly.
int CJMSTopicSubscr iber::messageDe livery()
{
IJMSMessage *jmsMessage = NULL;
HRESULT hr = S_OK;

// It has been verified that jmsMessage does not leak
jmsMessage = new CComObject<CJMS Message>();
if (jmsMessage != NULL)
{
// If all went well return the newly created JMS COM object
jmsMessage->AddRef();
}
else
{
fprintf (stderr, _T("Failed to create JMS Message COM Object") );
return -1;
}

if (SUCCEEDED(hr))
{
hr = Fire_onMessage( jmsMessage);
if (FAILED(hr))
{
fprintf (stderr, _T("Failed to send asynch message") );
return -1;
}

jmsMessage->Release();
jmsMessage = NULL;
}
else
{
fprintf (stderr, _T("Failed to create COM message wrapper") );
return -1;
}
return 0;
}

// Fire event.
HRESULT Fire_onMessage( IJMSMessage * pMessage)
{
HRESULT ret;
T* pT = static_cast<T*> (this);
int nConnectionInde x;
int nConnections = m_vec.GetSize() ;

for (nConnectionInd ex = 0; nConnectionInde x < nConnections;
nConnectionInde x++)
{
pT->Lock();
CComPtr<IUnknow n> sp = m_vec.GetAt(nCo nnectionIndex);
pT->Unlock();
IJMSMessageList ener* pIJMSMessageLis tener =
reinterpret_cas t<IJMSMessageLi stener*>(sp.p);

if (pIJMSMessageLi stener != NULL)
ret = pIJMSMessageLis tener->onMessage(pMes sage);
}

return ret;
}


"Nicholas Paldino [.NET/C# MVP]" wrote:
Giovanni,

Can you post the test case?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Giovanni Boschi" <Gi************ @discussions.mi crosoft.com> wrote in
message news:F3******** *************** ***********@mic rosoft.com...
We have found a memory leak when using a COM library with a C#
application.
The leak appears only if the C# application is compiled with the /optimize
flag. It goes away when the C# application is compiled with the /debug
flag.
The COM library is coded to fire asynchronous events, which are handled by
the C# application. It is in the firing and handling of these events that
the leak occurs.

Why does the optimized application leak while the debug application does
not?
Is there a fix available?
Is there another way to do asynchronous event handling that will
circumvent
this problem?

Attached is a small test case that recreates the problem. The only code
of
real interest in the COM library is the event firing code. If the problem
that we see is in the COM library, it would have to be in this piece of
code.
However, this code is more or less the output of a wizard, it is pretty
standard stuff. The C# application is also trivial, with the only item of
any real interest being the event handler. Once again, the code has been
pared down to the point of being trivial. Any insights into asynchronous
event handling through an interop assembly is appreciated.

Thanks,
Giovanni Boschi
Sonic Software Corporation


Nov 16 '05 #3
Is this the whole C# program?
[MTAThread]
public void onMessage(CJMSM essage pJMSMessage)
The MTAThread attribute can only be applied to the Main entry of an
executable program.

Please post complete samples that can be compiled and that illustrate the
problem.
Also give us some more details on how you define a leak, what tools have you
used and what kind of memory are you talking about.

Willy.

"Giovanni Boschi" <Gi************ @discussions.mi crosoft.com> wrote in
message news:06******** *************** ***********@mic rosoft.com...I have a simple test driver that reproduces it, but I haven't found a way
to
attach files to these posts - if the full test code and library would help
and there's an email address I can send it to, let me know.

Thanks for your help,
G.

This is the C# code that appears to leak:

amespace SonicExample
{
/// <summary>
///
/// </summary>
public class MessageListener : CSLEAKTESTVS6Li b.IJMSMessageLi stener
{

private int msgCount = 0;

public MessageListener ()
{
}
/// <summary>
/// This called by the JMSCom frame work when a mesage is received
/// </summary>
/// <param name="pJMSMessa ge"></param>
[MTAThread]
public void onMessage(CJMSM essage pJMSMessage)
{
System.Runtime. InteropServices .Marshal.Releas eComObject(pJMS Message);
pJMSMessage = null;
GC.Collect();
++msgCount;
if ((msgCount % 1000) == 0)
Console.WriteLi ne ("Message {0}", msgCount);
}
}
}

This is the code, inside the COM library, that registers and fires
callbacks, respectively - this is fairly boilerplate stuff and works
correctly if the COM client is C++, or C# compiled as debug.

// register the callback
if (pListener != NULL)
{
hr = AtlAdvise (thisUnknown,
listenerUnknown ,
IID_IJMSMessage Listener,
&m_MessageListe nerCookie);
}

// The callback routine
// messageDelivery and Fire_onMessage get called repeatedly.
int CJMSTopicSubscr iber::messageDe livery()
{
IJMSMessage *jmsMessage = NULL;
HRESULT hr = S_OK;

// It has been verified that jmsMessage does not leak
jmsMessage = new CComObject<CJMS Message>();
if (jmsMessage != NULL)
{
// If all went well return the newly created JMS COM object
jmsMessage->AddRef();
}
else
{
fprintf (stderr, _T("Failed to create JMS Message COM Object") );
return -1;
}

if (SUCCEEDED(hr))
{
hr = Fire_onMessage( jmsMessage);
if (FAILED(hr))
{
fprintf (stderr, _T("Failed to send asynch message") );
return -1;
}

jmsMessage->Release();
jmsMessage = NULL;
}
else
{
fprintf (stderr, _T("Failed to create COM message wrapper") );
return -1;
}
return 0;
}

// Fire event.
HRESULT Fire_onMessage( IJMSMessage * pMessage)
{
HRESULT ret;
T* pT = static_cast<T*> (this);
int nConnectionInde x;
int nConnections = m_vec.GetSize() ;

for (nConnectionInd ex = 0; nConnectionInde x < nConnections;
nConnectionInde x++)
{
pT->Lock();
CComPtr<IUnknow n> sp = m_vec.GetAt(nCo nnectionIndex);
pT->Unlock();
IJMSMessageList ener* pIJMSMessageLis tener =
reinterpret_cas t<IJMSMessageLi stener*>(sp.p);

if (pIJMSMessageLi stener != NULL)
ret = pIJMSMessageLis tener->onMessage(pMes sage);
}

return ret;
}


"Nicholas Paldino [.NET/C# MVP]" wrote:
Giovanni,

Can you post the test case?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Giovanni Boschi" <Gi************ @discussions.mi crosoft.com> wrote in
message news:F3******** *************** ***********@mic rosoft.com...
> We have found a memory leak when using a COM library with a C#
> application.
> The leak appears only if the C# application is compiled with the
> /optimize
> flag. It goes away when the C# application is compiled with the /debug
> flag.
> The COM library is coded to fire asynchronous events, which are handled
> by
> the C# application. It is in the firing and handling of these events
> that
> the leak occurs.
>
> Why does the optimized application leak while the debug application
> does
> not?
> Is there a fix available?
> Is there another way to do asynchronous event handling that will
> circumvent
> this problem?
>
> Attached is a small test case that recreates the problem. The only
> code
> of
> real interest in the COM library is the event firing code. If the
> problem
> that we see is in the COM library, it would have to be in this piece of
> code.
> However, this code is more or less the output of a wizard, it is pretty
> standard stuff. The C# application is also trivial, with the only item
> of
> any real interest being the event handler. Once again, the code has
> been
> pared down to the point of being trivial. Any insights into
> asynchronous
> event handling through an interop assembly is appreciated.
>
> Thanks,
> Giovanni Boschi
> Sonic Software Corporation


Nov 16 '05 #4

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

Similar topics

3
4671
by: Jeremy Lemaire | last post by:
Hello, I am working on cross platform code that is displaying a huge memory leak when compiled on 11.00 HPUX using the aCC -AA flag. It is not leaking on NT, LINUX, Solaris, or HPUX without the -AA flag. In another news group I came across some interesting (ok scarey) information regarding memory leaks in the STL list<...> container. I have compiled and executed the following code and verified that this does in fact leak on my system.
4
2350
by: Morten Aune Lyrstad | last post by:
Ok, now I'm officially confused. I have a large project going, which uses a win32 ui library I am developing myself. And I'm getting weird memory leaks. I don't know if I can explain what is going on, but I really need some help on this one. Ok, so I have this class defined (written by Randy Charles Morin, www.kbcafe.com) which detects memory leaks. It creates a memory check point in the constructor, and another in the destructor, and...
10
14051
by: eyh5 | last post by:
Hi, My C code (running on Soalris Unix) has some "segmentation fault" that I wish to use purify to do it. I poked around the web, and found some information about adding some lines in a Makefile file to use purify. However, my code is a rather simple single-source C program, and I didn't write a Makefile for it. I'm wondering if anybody can tell me which commands are to be entered at the Unix prompt to use purify. And, I don't know if...
16
2390
by: JCauble | last post by:
We have a large Asp.net application that is currently crashing our production servers. What we are seeing is the aspnet_wp eat up a bunch of memory and then stop unexpectedly. Does not recycle. Example: After about 5 hours with a concurrent user base of about 150 users the application raises the aspnet_wp memory usage by almost 500MB. If our server guys modify the web.config this data is released and the workerprocess goes back to a...
23
4565
by: James | last post by:
The following code will create memory leaks!!! using System; using System.Diagnostics; using System.Data; using System.Data.SqlClient; namespace MemoryLeak
4
4820
by: mast2as | last post by:
Hi again I was still debugging some code and check for memory leaks with valgrind and found out that valgrind finds a leak when i use deque<Somethingaqueue ?! I am compiling under Linux for example: #include <deque> using namespace std;
8
8321
by: vidya.bhagwath | last post by:
Hello Experts, I am using std::string object as a member variable in one of the my class. The same class member function operates on the std::string object and it appends some string to that object. My sample code is as follows. ..h file content---------- #include <stdio.h>
0
1672
by: nejucomo | last post by:
Hi folks, Quick Synopsis: A test script demonstrates a memory leak when I use pythonic extensions of my builtin types, but if I use the builtin types themselves there is no memory leak. If you are interested in how builtin/pure-python inheritance interacts
7
15715
by: Ragnar Agustsson | last post by:
Hi all I have been wandering about the best way to sandbox memory leaks in 3rd party libraries when using them from the .Net framework. I have a 3rd party library, written in C++, that leaks a lot of memory but I still had to use it. 1. After using DLLImport and seeing the memory leak I tried to load and
0
9072
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
8896
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
9653
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
9421
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
6151
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4942
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3395
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2872
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2284
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.