Re: Garbage collection

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Jay B. Harlow [MVP - Outlook] (Jay_Harlow_MVP_at_msn.com)
Date: 08/15/04


Date: Sun, 15 Aug 2004 12:07:32 -0500

Reinier,
I have not used Pinned memory so I cannot address that issue itself. You may
want to ask "down the hall" in the microsoft.public.dotent.framework.interop
newsgroup as that is where all the interop people hangout.

I would suggest defining lpData as an System.IntPtr instead of an Integer as
it is really a pointer to data & not an Integer. If you ever try to run this
on the 64 bit version of .NET you will have an obscure error that may be
hard to find... IntPtr will be the correct size for a pointer on both 32Bit
.NET and 64Bit .NET!

> Structure MIDIHDR
        Dim lpData as IntPtr
> Dim ...(other integer data)
> End Structure

    mhdr.lpData = GCHSysEx.AddrOfPinnedObject

Hope this helps
Jay

"Reinier VDW" <ReinierVDW@discussions.microsoft.com> wrote in message
news:E2BD5340-1B00-4BBC-AD22-536B7B06A780@microsoft.com...
> Hi,
>
> I am using a MIDI header structure MIDIHDR. The header contains a pointer
> lpData to a string sSysEx, which is filled by an API call back routine
using
> that pointer.
> The structure, an instance mhdr of the header, the string and its handle
are
> declared on main module level:
> <StructLayout(LayoutKind.Sequential)> _
> Structure MIDIHDR
> Dim lpData as Integer
> Dim ...(other integer data)
> End Structure
> Public mhdr As MIDIHDR
> Public sSysEx As String = New String("RRR")
> Public GCHSysEx as GCHandle = GCHandle.Alloc(sSysEx,
GCHandleType.Pinned)
>
> In a particular sub the header is filled and the midi input is started:
> mhdr.lpData = GCHSysEx.AddrOfPinnedObject.ToInt32
> mhdr. ...etc...
> lRet = midiInStart
> and the Midi call back routine fills the string. All OK so far.
>
> However, the sub can only be called once. When it is called again, the
> Garbage Collector appears to have moved the string and the pointer is
> invalid. As far as I understand, that should not happen as the memory is
> pinned and the handle is not yet unpinned by a GCHSysEx.Free statement.
>
> Anyone who knows how to pin memory in a correct way or knows about the
> 'scope' of a pinned object?
>
> Thanks,
>
> Reinier
>



Relevant Pages

  • Re: This is getting really weird.
    ... I thought 4 bytes for reference count and 4 for string length. ... > There should be no memory allocation for that line. ... > manager may allocate more space than requested for its own efficiency. ... > that New returned with a pointer to the string constant. ...
    (alt.comp.lang.borland-delphi)
  • Re: [C++] Help!: map<userdefined*, vector<int>>
    ... >> The map will be sorted along pointer values. ... a constant string and as such has an address in memory. ... The only way to do that, if you used the very same memory ... >> and see if the sorted map output reflects that change. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Problem in writing structure to Binary file in C lang
    ... You allocate just enough memory ... to store a pointer, but rather likely not enough memory to store ... a string. ... pointer to a string then you write over memory yiu don't own. ...
    (comp.lang.c)
  • weird GCHandle behavior
    ... I need to pass the address of a variableto some win32 API functions like ... pointer to write data to the memory location. ... string to it, ...
    (microsoft.public.dotnet.languages.vb)
  • Re: P/Invoke - TAPI - LINEDEVCAPS-structure!?
    ... > When you call TSPI_lineGetDevCaps and pass it an instance of the structure,> the function may tell you that the block of memory you passed is too small. ... the explicit fields declared as being part of the structure are> just the 'header' part of the memory layout of the information. ... The dwLineNameOffset field> of the structure tells you how many bytes into the structure the start of> the line name string is, and the dwLineNameSize field tells you how> many bytes of string data are stored there for the name. ... >> How do I have to imagine what this structure looks like ans how itīs ...
    (microsoft.public.dotnet.framework.compactframework)