Marshal.FreeHGlobal causes application abort

Last post 05-12-2008 11:58 by ctacke. 3 replies.
Page 1 of 1 (4 items)
Sort Posts: Previous Next
  • 05-12-2008 7:34

    • mohunt
    • Top 75 Contributor
    • Joined on 05-12-2008
    • Posts 6

    Marshal.FreeHGlobal causes application abort

    I am developing a Smartcard application on Windows Mobile 5.0 and am experiencing an application abort on a

    Marshal.FreeHGlobal function.  A code snippet is below:

     

    <DllImport("winscard.dll", SetLastError:=True)> _

    Friend Shared Function SCardEstablishContext(ByVal dwScope As UInt32, _

    ByVal pvReserved1 As UInt32, _

    ByVal pvReserved2 As UInt32, _

    ByRef phContext As Long) As UInt32

    End Function

    Private m_hContext As Long = 0

    Private m_nLastError As Long = 0

    Private m_hContext As Long = 0

     Public Sub EstablishContext(ByVal Scope As SCOPE)

    Dim hContext As Long = Marshal.AllocHGlobal(Marshal.SizeOf(m_hContext))

    m_nLastError = SCardEstablishContext(CType(Scope, System.Int32), IntPtr.Zero, IntPtr.Zero, hContext)

    If m_nLastError <> RETURN_CODE.SCARD_S_SUCCESS Then

    Dim msg As String = "SCardEstablishContext error: " & m_nLastError

    Marshal.FreeHGlobal(hContext)

    Throw New Exception(msg)

    End If

    m_hContext = hContext

    Marshal.FreeHGlobal(hContext)

    End Sub

    The function seems to work properly until the Marshal.FreeHGlobal(hContext) statement executes then the application simply stops running with no exception thrown. 

     

    Any help with this would be greatly appreciated.

  • 05-12-2008 10:15 In reply to

    • ctacke
    • OpenNETCF Staff
    • Top 10 Contributor
    • Joined on 07-27-2007
    • Indiana
    • Posts 1,939

    Re: Marshal.FreeHGlobal causes application abort

    Looking at the docs, that's certainly expected, and if it didn't abort you'd have a leak anyway.  The call to SCardEstablishContext returns a pointer to an hContext - meaning that it makes the allocation - you shouldn't be.  So your call to the API is changing the value of that IntPtr you're storing as hContext (go ahead and check it in your code).  So that means that you're trying to free memory at an address that you don't own and that would cause an abort (we'll look into making that a bit safer if possible).

    Look at the docs. You need to simply store the handle and when you're done call SCardReleaseContext on it.

  • 05-12-2008 11:14 In reply to

    • mohunt
    • Top 75 Contributor
    • Joined on 05-12-2008
    • Posts 6

    Re: Marshal.FreeHGlobal causes application abort

    Thank you for your response and the accompanying explanation.  I have removed the memory allocation/deallocation statements from the function.  However, the link in the reference "at the docs" http://community.opennetcf.com/forums/SCardEstablishContext is returns a "The page cannot be found error".  Do you have another link for this?

     

     

  • 05-12-2008 11:58 In reply to

    • ctacke
    • OpenNETCF Staff
    • Top 10 Contributor
    • Joined on 07-27-2007
    • Indiana
    • Posts 1,939

    Re: Marshal.FreeHGlobal causes application abort

     Not sure what happened there.  I updated the link in the post above. All I did was Google for "SCardEstablishContext"

Page 1 of 1 (4 items)