473,387 Members | 1,536 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.

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
Jul 21 '05 #1
5 2644
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

Jul 21 '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


Jul 21 '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


Jul 21 '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



Jul 21 '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
>
>



Jul 21 '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...
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: 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:
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?
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
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,...
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.