Stack Memory Leak in C#



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)


.



Relevant Pages

  • Re: If Macs have no spyware....
    ... >had made a complete code review of its operating system and removed all ... and writing new data into those memory locations would ... >but when the data exists on the stack, it can cause very large problems. ... >location that needs to be written in place of the correct execution ...
    (comp.sys.mac.advocacy)
  • Re: If Macs have no spyware....
    ... First you yammer about being a Mac advocate, then bad mouth me for dumping XP in favor of a Mac. ... Supposedly Microsoft had made a complete code review of its operating system and removed all the buffers which could overflow. ... the fundamental problem is that the basic architecture of Windows has two fatal flaws in its memory management and while these remain in the software the ad hoc patches will never be enough to make Windows a secure operating system. ... These problems are bad enough when dealing with data in the one routine but when the data exists on the stack, it can cause very large problems. ...
    (comp.sys.mac.advocacy)
  • Re: When is "volatile" used instead of "lock" ?
    ... to get the address of a stack variable to a background thread. ... I'm suggesting that the memory model ... lock pattern works without making the instance member volatile; ... fields shared amongst more than one thread despite following the locking ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Maybe we should stop "Paging Beth Stone" already...
    ... I'll want to work on my OS while running my OS, so the assembler that it's written with has to run under it. ... You have to swap CR3 if you want seperate memory spaces. ... The alternate stacks aren't used by the processor unless the task calls a different protection level, so they're not part of the TSS swap. ... This lets any application use up to a gigabyte of stack before Linux is forced to tell it that it's gone too far. ...
    (alt.lang.asm)
  • Re: Resolving internal links
    ... You have to pass all workspace in via ... Which highlights another issue - you could not use Fortify (or similar code which uses these functions to replace the memory allocators) within the main application code unless you also used it within the module. ... At best you'll reference memory which does not exist and cause an abort. ... will only be able to use 256 bytes of stack space IIRC. ...
    (comp.sys.acorn.programmer)