473,387 Members | 1,590 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,387 software developers and data experts.

c++ com interop use c# dll memory leak

1
Hello,

I have a Problem with my c++ program that uses a c# dll as com- object. It seems to have a Memory leak. The used Memory increases slowly. Can anybody help me?

I have attached the code snipped of the c# code and of the c++ dll that uses the c# dll.

c++ code:


#import "C:\temp\test_con.tlb" named_guids

bool TestCommunicator::Connect ()
{

long count = 0;
unsigned char* data = NULL;

HRESULT hr;
hr = CoInitialize(NULL);

p.CreateInstance(__uuidof(TestCon_Con::Test_Con));


if (p->Connect())
{
while ( m_stop == false )
{
p->ReadPDOData3 (&count, &data);
}

p->DisConnect();
p.Release();
}

CoUninitialize();
}


c# code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;


namespace TestCon_Con
{
[Guid("C751C166-2094-4F1F-A47D-9556991F460B")]
public interface TestInterface
{
[DispId(1)]
bool Connect();

[DispId(2)]
bool DisConnect();

[DispId(3)]
byte[] GetBusData();

[DispId(4)]
void ReadPDOData3([In, Out] ref int nNumElements,
[In, Out, MarshalAs(UnmanagedType.LPArray, SizeParamIndex=0)] ref byte [] bArray );

}


[Guid("47361D08-F31D-416A-B599-97F473ACC276"),
InterfaceType(ComInterfaceType.InterfaceIsIDispatc h)]
public interface Test_Events
{
}

[Guid("7A0E9679-FD21-486D-BAE7-9387F0C2B9F1"),
ClassInterface(ClassInterfaceType.None),
ComSourceInterfaces(typeof(Test_Events))]
public class Test_Con : TestInterface
{


public bool Connect()
{
return true;
}


public bool DisConnect()
{
return true;
}


public byte[] GetBusData()
{
return null;
}


public void ReadPDOData3(ref int count, ref byte[] data)
{
data = new byte [15];
for (int i = 0; i < 15; i++)
{
data[i] = i;
}
count = data.Length;
}
}
}
Jun 26 '14 #1
1 2077
weaknessforcats
9,208 Expert Mod 8TB
You have this code:

Expand|Select|Wrap|Line Numbers
  1. unsigned char* data = NULL;
  2.  
and this code:

Expand|Select|Wrap|Line Numbers
  1. while ( m_stop == false )
  2.  {
  3.  p->ReadPDOData3 (&count, &data);
  4. etc...
  5.  
and this code:

Expand|Select|Wrap|Line Numbers
  1. public void ReadPDOData3(ref int count, ref byte[] data)
  2.  {
  3.  data = new byte [15];
  4. etc...
So I see data defined as an unsigned char* whose address is repeatedly passed to ReadPDOData3 where a heap allocation is made and the address of the allocation placed in data. But I never see where data is deleted before overstoring it. In fact I never see a delete on data at all. This looks like a 15 byte leak on every ReadPDOData3 call.

Using naked pointers like this is a big no-no in C++. Consider using a smart pointer instead.
Jun 26 '14 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Chris Oates | last post by:
I'm using interop to bring in user32.dll and gdi32.dll, and I'm finding that I appear to have some kind of memory leak in the CLR. use of GC.GetTotalMemory() shows a lot of motion, but it does not...
3
by: Giovanni Boschi | last post by:
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...
0
by: John | last post by:
We are using Authorization Manager in an ASP.NET application. We are using Microsoft.Interop.Security.AzRoles. We appear to have a memory leak when calling the method...
9
by: Anton | last post by:
{Willy Skjveland} Hi, how can I trace a Memory leak in aspnet_wp.exe? {Rheena} One moment please while I search it for you. It may take me a few moments {Willy Skjveland} I need to find out...
23
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
2
by: bbembi_de | last post by:
Hello, I use a C++ Dll in .Net 1.1 with Dllimport. I call one C++ method in a loop and the memory of my .Net application rises proportional. What am I doing wrong calling this method? ...
12
by: Simon | last post by:
Hi all, I'm having a baffling problem with a windows service that I'm working on. Basically, I am using a typed dataset to insert a large number of rows into an SQL Server 2005 database. But...
1
by: sabys | last post by:
I've been noticing a memory leak with the following sample code snippet. Can someone please advise. Have a C# Winforms app with the following code on a button-click event. private void...
22
by: Peter | last post by:
I am using VS2008. I have a Windows Service application which creates Crystal Reports. This is a multi theaded application which can run several reports at one time. My problem - there is a...
0
by: Kent Bolton | last post by:
We have had the same issue ... it seems that the COM Interop wrapper leaks quite a lot of memory when iterating through the faxes in the folders. We found that we could avoid this by iterating...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...

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.