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

Memory leak in interop

I am making interop calls to an object in a .NET component from a C++
program and am leaking memory... the following is some sample code:

////////////////////////////////////////
// .NET component (Test)
////////////////////////////////////////

// interface
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
[Guid("D3DF5658-3D3F-436b-B6A6-F10E77AE3F5F")]
public interface IDotNetInterface
{
int getString(string aParam1, string aParam2, out string aValue);
}

// class
[ClassInterface(ClassInterfaceType.None)]
[ProgId("Test.DotNetInterface")]
public class DotNetInterface : IDotNetInterface
{
public int getString(string aParam1, string aParam2, out string aValue)
{
aValue = "some text";
GC.Collect();
return 1;
}
}

///////////////////////////////////
// C++ program
///////////////////////////////////

#import "Test.tlb" no_namespace named_guids

int _tmain(int argc, _TCHAR* argv[])
{
CoInitializeEx(NULL, COINIT_MULTITHREADED);

IDotNetInterfacePtr pPtr = IDotNetInterface(__uuidof(DotNetInterface));
pPtr->AddRef();

_bstr_t lParam1 = "1";
_bstr_t lParam2 = "2";

BSTR lValue;
int lResult = pPtr->getString(lParam1, lParam2, &lValue); // <-- LEAK!!

pPtr->Release();
pPtr = NULL;
}
I've tried all sorts of things (trying to explicitly release the BSTR
memory, etc.), but no effect. What is causing this leak, and how do I solve
it???

Thanks in advance,
Matt
Nov 15 '05 #1
5 2376
Trokey,

I don't see how the runtime is causing the leak. If anything, you need
to call SysFreeString to free the string.

You can also use a CComBSTR class and it will handle this automatically.

Also, you don't need to call the static Collect method on the GC class.

What do you have to support that there is a leak?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Trokey" <ma********@hotmail.com> wrote in message
news:bp*************@ID-203151.news.uni-berlin.de...
I am making interop calls to an object in a .NET component from a C++
program and am leaking memory... the following is some sample code:

////////////////////////////////////////
// .NET component (Test)
////////////////////////////////////////

// interface
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
[Guid("D3DF5658-3D3F-436b-B6A6-F10E77AE3F5F")]
public interface IDotNetInterface
{
int getString(string aParam1, string aParam2, out string aValue);
}

// class
[ClassInterface(ClassInterfaceType.None)]
[ProgId("Test.DotNetInterface")]
public class DotNetInterface : IDotNetInterface
{
public int getString(string aParam1, string aParam2, out string aValue)
{
aValue = "some text";
GC.Collect();
return 1;
}
}

///////////////////////////////////
// C++ program
///////////////////////////////////

#import "Test.tlb" no_namespace named_guids

int _tmain(int argc, _TCHAR* argv[])
{
CoInitializeEx(NULL, COINIT_MULTITHREADED);

IDotNetInterfacePtr pPtr = IDotNetInterface(__uuidof(DotNetInterface)); pPtr->AddRef();

_bstr_t lParam1 = "1";
_bstr_t lParam2 = "2";

BSTR lValue;
int lResult = pPtr->getString(lParam1, lParam2, &lValue); // <-- LEAK!!
pPtr->Release();
pPtr = NULL;
}
I've tried all sorts of things (trying to explicitly release the BSTR
memory, etc.), but no effect. What is causing this leak, and how do I solve it???

Thanks in advance,
Matt

Nov 15 '05 #2
SCG
>> If anything, you need to call SysFreeString to free the string.

Absolutely. I believe this may be your issue.
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:u5**************@TK2MSFTNGP11.phx.gbl...
Trokey,

I don't see how the runtime is causing the leak. If anything, you need to call SysFreeString to free the string.

You can also use a CComBSTR class and it will handle this automatically.
Also, you don't need to call the static Collect method on the GC class.
What do you have to support that there is a leak?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Trokey" <ma********@hotmail.com> wrote in message
news:bp*************@ID-203151.news.uni-berlin.de...
I am making interop calls to an object in a .NET component from a C++
program and am leaking memory... the following is some sample code:

////////////////////////////////////////
// .NET component (Test)
////////////////////////////////////////

// interface
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
[Guid("D3DF5658-3D3F-436b-B6A6-F10E77AE3F5F")]
public interface IDotNetInterface
{
int getString(string aParam1, string aParam2, out string aValue);
}

// class
[ClassInterface(ClassInterfaceType.None)]
[ProgId("Test.DotNetInterface")]
public class DotNetInterface : IDotNetInterface
{
public int getString(string aParam1, string aParam2, out string aValue) {
aValue = "some text";
GC.Collect();
return 1;
}
}

///////////////////////////////////
// C++ program
///////////////////////////////////

#import "Test.tlb" no_namespace named_guids

int _tmain(int argc, _TCHAR* argv[])
{
CoInitializeEx(NULL, COINIT_MULTITHREADED);

IDotNetInterfacePtr pPtr =

IDotNetInterface(__uuidof(DotNetInterface));
pPtr->AddRef();

_bstr_t lParam1 = "1";
_bstr_t lParam2 = "2";

BSTR lValue;
int lResult = pPtr->getString(lParam1, lParam2, &lValue); // <--

LEAK!!

pPtr->Release();
pPtr = NULL;
}
I've tried all sorts of things (trying to explicitly release the BSTR
memory, etc.), but no effect. What is causing this leak, and how do I

solve
it???

Thanks in advance,
Matt


Nov 15 '05 #3
I've tried all these things, but no difference... I know there is a leak
because I monitor the private bytes in perfmon and the memory allocated by
the getString call is never released.

Matt

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:u5**************@TK2MSFTNGP11.phx.gbl...
Trokey,

I don't see how the runtime is causing the leak. If anything, you need to call SysFreeString to free the string.

You can also use a CComBSTR class and it will handle this automatically.
Also, you don't need to call the static Collect method on the GC class.
What do you have to support that there is a leak?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Trokey" <ma********@hotmail.com> wrote in message
news:bp*************@ID-203151.news.uni-berlin.de...
I am making interop calls to an object in a .NET component from a C++
program and am leaking memory... the following is some sample code:

////////////////////////////////////////
// .NET component (Test)
////////////////////////////////////////

// interface
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
[Guid("D3DF5658-3D3F-436b-B6A6-F10E77AE3F5F")]
public interface IDotNetInterface
{
int getString(string aParam1, string aParam2, out string aValue);
}

// class
[ClassInterface(ClassInterfaceType.None)]
[ProgId("Test.DotNetInterface")]
public class DotNetInterface : IDotNetInterface
{
public int getString(string aParam1, string aParam2, out string aValue) {
aValue = "some text";
GC.Collect();
return 1;
}
}

///////////////////////////////////
// C++ program
///////////////////////////////////

#import "Test.tlb" no_namespace named_guids

int _tmain(int argc, _TCHAR* argv[])
{
CoInitializeEx(NULL, COINIT_MULTITHREADED);

IDotNetInterfacePtr pPtr =

IDotNetInterface(__uuidof(DotNetInterface));
pPtr->AddRef();

_bstr_t lParam1 = "1";
_bstr_t lParam2 = "2";

BSTR lValue;
int lResult = pPtr->getString(lParam1, lParam2, &lValue); // <--

LEAK!!

pPtr->Release();
pPtr = NULL;
}
I've tried all sorts of things (trying to explicitly release the BSTR
memory, etc.), but no effect. What is causing this leak, and how do I

solve
it???

Thanks in advance,
Matt


Nov 15 '05 #4
int _tmain(int argc, _TCHAR* argv[])
{
CoInitialize(NULL);
IDotNetInterfacePtr pPtr(__uuidof(DotNetInterface));
_bstr_t lParam1 = "1";
_bstr_t lParam2 = "2";

BSTR lValue;
for (int i=0; i<100000000; i++) {
int lResult = pPtr->getString(lParam1, lParam2, &lValue);
SysFreeString(lValue); // <-- NO LEAK!!
}
}
Willy.

"Trokey" <ma********@hotmail.com> wrote in message
news:bp*************@ID-203151.news.uni-berlin.de...
I've tried all these things, but no difference... I know there is a leak
because I monitor the private bytes in perfmon and the memory allocated by
the getString call is never released.

Matt

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in message news:u5**************@TK2MSFTNGP11.phx.gbl...
Trokey,

I don't see how the runtime is causing the leak. If anything, you

need
to call SysFreeString to free the string.

You can also use a CComBSTR class and it will handle this

automatically.

Also, you don't need to call the static Collect method on the GC

class.

What do you have to support that there is a leak?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Trokey" <ma********@hotmail.com> wrote in message
news:bp*************@ID-203151.news.uni-berlin.de...
I am making interop calls to an object in a .NET component from a C++
program and am leaking memory... the following is some sample code:

////////////////////////////////////////
// .NET component (Test)
////////////////////////////////////////

// interface
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
[Guid("D3DF5658-3D3F-436b-B6A6-F10E77AE3F5F")]
public interface IDotNetInterface
{
int getString(string aParam1, string aParam2, out string aValue);
}

// class
[ClassInterface(ClassInterfaceType.None)]
[ProgId("Test.DotNetInterface")]
public class DotNetInterface : IDotNetInterface
{
public int getString(string aParam1, string aParam2, out string aValue) {
aValue = "some text";
GC.Collect();
return 1;
}
}

///////////////////////////////////
// C++ program
///////////////////////////////////

#import "Test.tlb" no_namespace named_guids

int _tmain(int argc, _TCHAR* argv[])
{
CoInitializeEx(NULL, COINIT_MULTITHREADED);

IDotNetInterfacePtr pPtr =

IDotNetInterface(__uuidof(DotNetInterface));
pPtr->AddRef();

_bstr_t lParam1 = "1";
_bstr_t lParam2 = "2";

BSTR lValue;
int lResult = pPtr->getString(lParam1, lParam2, &lValue); // <--

LEAK!!

pPtr->Release();
pPtr = NULL;
}
I've tried all sorts of things (trying to explicitly release the BSTR
memory, etc.), but no effect. What is causing this leak, and how do I

solve
it???

Thanks in advance,
Matt



Nov 15 '05 #5
Thanks everyone... after putting the call in a loop, I can see that
SysFreeString solves the problem.

Matt

"Willy Denoyette [MVP]" <wi*************@pandora.be> wrote in message
news:ek**************@TK2MSFTNGP10.phx.gbl...
int _tmain(int argc, _TCHAR* argv[])
{
CoInitialize(NULL);
IDotNetInterfacePtr pPtr(__uuidof(DotNetInterface));
_bstr_t lParam1 = "1";
_bstr_t lParam2 = "2";

BSTR lValue;
for (int i=0; i<100000000; i++) {
int lResult = pPtr->getString(lParam1, lParam2, &lValue);
SysFreeString(lValue); // <-- NO LEAK!!
}
}
Willy.

"Trokey" <ma********@hotmail.com> wrote in message
news:bp*************@ID-203151.news.uni-berlin.de...
I've tried all these things, but no difference... I know there is a leak
because I monitor the private bytes in perfmon and the memory allocated by the getString call is never released.

Matt

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote

in
message news:u5**************@TK2MSFTNGP11.phx.gbl...
Trokey,

I don't see how the runtime is causing the leak. If anything, you

need
to call SysFreeString to free the string.

You can also use a CComBSTR class and it will handle this

automatically.

Also, you don't need to call the static Collect method on the GC

class.

What do you have to support that there is a leak?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Trokey" <ma********@hotmail.com> wrote in message
news:bp*************@ID-203151.news.uni-berlin.de...
> I am making interop calls to an object in a .NET component from a C++ > program and am leaking memory... the following is some sample code:
>
> ////////////////////////////////////////
> // .NET component (Test)
> ////////////////////////////////////////
>
> // interface
> [InterfaceType(ComInterfaceType.InterfaceIsDual)]
> [Guid("D3DF5658-3D3F-436b-B6A6-F10E77AE3F5F")]
> public interface IDotNetInterface
> {
> int getString(string aParam1, string aParam2, out string aValue); > }
>
> // class
> [ClassInterface(ClassInterfaceType.None)]
> [ProgId("Test.DotNetInterface")]
> public class DotNetInterface : IDotNetInterface
> {
> public int getString(string aParam1, string aParam2, out string

aValue)
> {
> aValue = "some text";
> GC.Collect();
> return 1;
> }
> }
>
> ///////////////////////////////////
> // C++ program
> ///////////////////////////////////
>
> #import "Test.tlb" no_namespace named_guids
>
> int _tmain(int argc, _TCHAR* argv[])
> {
> CoInitializeEx(NULL, COINIT_MULTITHREADED);
>
> IDotNetInterfacePtr pPtr =
IDotNetInterface(__uuidof(DotNetInterface));
> pPtr->AddRef();
>
> _bstr_t lParam1 = "1";
> _bstr_t lParam2 = "2";
>
> BSTR lValue;
> int lResult = pPtr->getString(lParam1, lParam2, &lValue); // <--
LEAK!!
>
> pPtr->Release();
> pPtr = NULL;
> }
>
>
> I've tried all sorts of things (trying to explicitly release the BSTR > memory, etc.), but no effect. What is causing this leak, and how do I solve
> it???
>
> Thanks in advance,
> Matt
>
>



Nov 15 '05 #6

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

Similar topics

2
by: Geoff Pennington | last post by:
The memory consumed by aspnet_wp.exe just keeps going up (as tracked in Task Manager) until I restart it, so I must have a memory leak. If it makes a difference, my app makes considerable use of...
5
by: Trokey | last post by:
I am making interop calls to an object in a .NET component from a C++ program and am leaking memory... the following is some sample code: //////////////////////////////////////// // .NET...
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...
4
by: Chris | last post by:
Hi, I have an asp.net site that receives a medium amount of traffic (500-1k users per day). I'm having massive trouble at the moment with a memory leak that I just cannot isolate. The server is...
15
by: Chetan Raj | last post by:
Hi All, We have a web-application in asp.net that interacts with legacy code written in COM. The memory usage in aspnet_wp.exe increases every sec and never reduces. Using the .NET performance...
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
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...
3
by: not_a_commie | last post by:
The CLR won't garbage collect until it needs to. You should see the memory usage climb for some time before stabilizing. Can you change your declaration to use the 'out' keyword rather than a 'ref'...
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...
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: 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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.