Stack Memory Leak in C#
- From: "Robert Bouillon" <djwhizzard@xxxxxxxxxxx>
- Date: Tue, 26 Apr 2005 12:08:01 -0400
I've got a memory leak problem with my Stack in a class I created for Serial
Port Communication.
I've narrowed it doen to a subclass I created to monitor the Win32 events
fired for the Serial Port (Using WaitCommEvent).
The "Status" subclass allocates unmanaged memory for the Event Handle and
Overlapped struct in the ctor and frees the memory in the
(IDisposable).Dispose method for the object. An infinite loop checks the
event / status on a background thread. No memory is allocated explicitly in
this loop, however I'm definately leaking my stack memory from within this
loop.
This leads me to believe it's a problem with my P/Invoke declarations. The
only thing I do differently than most other P/Invoke statements I've seen is
rather than using "out uint" for event masks, I'm using "out MyEnum". I
think this is where my problem lies, however it takes me almost an hour to
reproduce this error so I'm having some difficulty narrowing it down.
<code>
[Flags]
private enum ModemStatus : uint
{
MS_CTS_ON = 0x0010,
MS_DSR_ON = 0x0020,
MS_RING_ON = 0x0040,
MS_RLSD_ON = 0x0080
}
[DllImport("kernel32.dll")]
private static extern bool GetCommModemStatus(IntPtr hFile, out
ModemStatus lpModemStat);
</code>
Do I have to clean lpModemStat explicitly?
Is this possibly a problem with P/Invoke?
How can I explicitly free lpModemStat for testing?
Is there a way for me to watch my stack allocation at runtime?
Thanks in Advance
(P.S. Sorry for the duplicate post. This was originally posted in
microsoft.public.dotnet.framework until I realized that it's likely an
interop issue)
.
- Follow-Ups:
- Re: Stack Memory Leak in C#
- From: Gabriel Lozano-Morán
- Re: Stack Memory Leak in C#
- Prev by Date: Re: How to register .Net dll (.tlb) file to registry
- Next by Date: RE: Access Interop - List/Edit reports
- Previous by thread: How to register .Net dll (.tlb) file to registry
- Next by thread: Re: Stack Memory Leak in C#
- Index(es):
Relevant Pages
|