473,406 Members | 2,549 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,406 software developers and data experts.

Re: Memory leak using pInvoke

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' keyword?
Jun 27 '08 #1
3 3191
Could you tell me why we should use 'out' keyword instead of 'ref' keyword?

"not_a_commie" wrote:
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' keyword?
Jun 27 '08 #2
On Tue, 24 Jun 2008 01:40:00 -0700, Victor
<Vi****@discussions.microsoft.comwrote:
Could you tell me why we should use 'out' keyword instead of 'ref'
keyword?
To the extent that your rising memory footprint is related to the
allocation of a BITMAPINFOHEADER object before calling the
VfwDecoder_GetOutputBitmapInfoHeader() function, using an "out" parameter
instead of "ref" allows you to avoid allocating the object, since it
wouldn't need to be initialized before passing to the function.

That said, I'm not really clear on the code example. The DllImport you
show is for VfwDecoder_DecompressNative, not
VfwDecoder_GetOutputBitmapInfoHeader, and you don't show us how you've
actually declared BITMAPINFOHEADER in C#. I'm relatively inexperienced
with p/invoke, so maybe it wouldn't help me understand your question
better anyway. But it could be useful to someone else with more
experience with respect to identifying anything about your code that might
be an issue.

All that said, the real bottom line here is that as long as you're dealing
with managed references to objects, the fact that your memory usage climbs
is not really a problem. The framework manages its use of actual
allocated memory from the OS independent of your own use of managed
memory, and it's not uncommon to see the process memory allocations be
larger than your application's actual use of managed memory.

Pete
Jun 27 '08 #3
Unfortunatley I don't have a solution to the OPs problem, but I have a
similar problem and have done some troubleshooting on it. I'm in way over my
head here so please don't flame me if I state the obvious.
I have written a very small test program.
C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace leakapp {
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
internal class DataContainer {
public Int32 m_arrayBindIndex = 12;
}

class Program {

static void Main(string[] args) {
Console.WriteLine("Begin...");
Console.ReadLine();
DataContainer ctx = new DataContainer();
long count = 100000;
for (long q = 0; q < 5; q++) {
for (long i = 0; i < count; i++) {
LeakClass(ref ctx);
}
Console.WriteLine("Outer: " + q.ToString());
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
}

Console.WriteLine("Done");
Console.ReadLine();
}
/// <summary>
/// Call the dumy dll with a reference to the DataContainer.
/// </summary>
[DllImport("foo.dll", EntryPoint = "LeakClass", CallingConvention =
CallingConvention.Cdecl)]
public static extern int LeakClass(ref DataContainer cl);

}
}

foo.h:
typedef struct _DataContainer {
int m_arrayBindIndex;
} DataContainer;

extern "C" {
// Declaration of the compression method
__declspec(dllexport) void LeakClass(DataContainer **flum);
};

foo.cpp:
// This is the main DLL file.
#include <objbase.h>
#include "foo.h"

void LeakClass(DataContainer ** flum) {
}
(Don't comment on the usefullness of the code, it is only an example, as is
probably obvious from the naming etc. :-))

Running the program and attaching DebugDiag will reveal that on every call
to the unmanaged method there is a leak of 4 bytes. The leak will be on the
default process heap due to a call to CoTaskMemAlloc (used by the
interop-marshaler). DebugDiag reports that the allocations come from
mscorwks!BlittablePtrMarshalerBase::ConvertSpaceCL RToNative+23 . I can even
remove the only field in the class DataContainer passed to the unmanaged
method, this will result in a leak of 1 byte per call. The leak is not in
managed memory, but just to avoid all replys about "The GC hasn't run yet.."
etc. I included the GC.Collect()-sequence.
The above problem arises in our production servers. These small memory leaks
will fragment the default process heap (CoTaskMemAlloc heap) eventualy
crashing the app.
So, am I doing something stupid here? I have read about pinvoke and interop
marshaling but I can't find anything that is wrong with the code. Changing
the managed definition to a struct solves the problem. I would still like to
know what is wrong with the code I posted above.
Hope someone has some insight into thi.
/Erik
"Peter Duniho" wrote:
On Tue, 24 Jun 2008 01:40:00 -0700, Victor
<Vi****@discussions.microsoft.comwrote:
Could you tell me why we should use 'out' keyword instead of 'ref'
keyword?

To the extent that your rising memory footprint is related to the
allocation of a BITMAPINFOHEADER object before calling the
VfwDecoder_GetOutputBitmapInfoHeader() function, using an "out" parameter
instead of "ref" allows you to avoid allocating the object, since it
wouldn't need to be initialized before passing to the function.

That said, I'm not really clear on the code example. The DllImport you
show is for VfwDecoder_DecompressNative, not
VfwDecoder_GetOutputBitmapInfoHeader, and you don't show us how you've
actually declared BITMAPINFOHEADER in C#. I'm relatively inexperienced
with p/invoke, so maybe it wouldn't help me understand your question
better anyway. But it could be useful to someone else with more
experience with respect to identifying anything about your code that might
be an issue.

All that said, the real bottom line here is that as long as you're dealing
with managed references to objects, the fact that your memory usage climbs
is not really a problem. The framework manages its use of actual
allocated memory from the OS independent of your own use of managed
memory, and it's not uncommon to see the process memory allocations be
larger than your application's actual use of managed memory.

Pete
Jul 1 '08 #4

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

Similar topics

3
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...
32
by: John | last post by:
Hi all: When I run my code, I find that the memory that the code uses keeps increasing. I have a PC with 2G RAM running Debian linux. The code consumes 1.5G memory by the time it finishes...
20
by: jeevankodali | last post by:
Hi I have an .Net application which processes thousands of Xml nodes each day and for each node I am using around 30-40 Regex matches to see if they satisfy some conditions are not. These Regex...
7
by: Rich Denis | last post by:
Hello, I have been trying to solve a mysterious memory leak problem and was hoping that you could help me out on my stuck point. First a bit of background. We have two app servers in an app...
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
7
by: Salvador | last post by:
Hi, I am using WMI to gather information about different computers (using win2K and win 2K3), checking common classes and also WMI load balance. My application runs every 1 minute and reports...
3
by: Jim Land | last post by:
Jack Slocum claims here http://www.jackslocum.com/yui/2006/10/02/3-easy-steps-to-avoid-javascript- memory-leaks/ that "almost every site you visit that uses JavaScript is leaking memory". ...
7
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...
4
by: O.B. | last post by:
I've got this C# .NET 2005 application that has some unmanaged code. At random times, I get: An unhandled expection of type 'System.AccessViolationException' occurred in Unknown Module. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
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
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,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.