Releaseビルドのみ落ちる変な挙動

Releaseビルドしたときだけなぜか落ちる、しかもVisualStudio上からReleaseで実行したときは平気というなんとも不思議な状態
100%マネージドコードだけで、なんかC言語でいうスタック壊しちゃったような現象が起こった


↓のCreatePipe2関数を呼ぶと呼び出し元のスタックの変数(ldloc.0 だけ?)がNothingになる

Imports System.ComponentModel
Imports System.Runtime.InteropServices


Public Class Main

    Protected Shared Sub Main(ByVal args() As String)

        Dim commands As List(Of String) = New List(Of String)(New String() {"/bin/bash", "--login", "-i"})

        commands.ToArray()
        Console.WriteLine(commands IsNot Nothing)

        Dim read, write As IntPtr
        CreatePipe2(read, write)

        Console.WriteLine("read ={0}", read)
        Console.WriteLine("write={0}", write)
        Console.WriteLine(commands IsNot Nothing)
        commands.ToArray()

        Console.ReadKey()
    End Sub

    Public Shared Function CreatePipe( _
        ByRef hReadPipe As IntPtr, _
        ByRef hWritePipe As IntPtr, _
        ByRef lpPipeAttributes As SECURITY_ATTRIBUTES, _
        ByVal nSize As UInteger) As Boolean

        hReadPipe = CType(100, IntPtr)
        hWritePipe = CType(200, IntPtr)
        Console.WriteLine("SECURITY_ATTRIBUTES.nLength={0}", lpPipeAttributes.nLength)

        Return True
    End Function

    Public Shared Sub CreatePipe2( _
        ByRef hReadPipe As IntPtr, _
        ByRef hWritePipe As IntPtr)

        Dim security As SECURITY_ATTRIBUTES
        security.Initialize()
        'Console.WriteLine(security.nLength)
        security.bInheritHandle = True

        'Dim ptr As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(security))
        'Marshal.StructureToPtr(security, ptr, False)
        'For i As Integer = 0 To Marshal.SizeOf(security) - 1

        '    Console.WriteLine("{0:d2} = 0x{1:x2}", i, Marshal.ReadByte(ptr, i))
        'Next
        'Marshal.FreeHGlobal(ptr)

        If Not CreatePipe(hReadPipe, hWritePipe, security, 0) Then Throw New Win32Exception
    End Sub

    Public Structure SECURITY_ATTRIBUTES

        Public nLength As UInteger
        Public lpSecurityDescriptor As IntPtr
        Public bInheritHandle As Boolean

        Public Sub Initialize()

            Me.nLength = CUInt(Marshal.SizeOf(Me.GetType))
            'Console.WriteLine(Me.nLength)
        End Sub
    End Structure

End Class

これをReleaseでビルドして実行するとMainの中の「commands.ToArray()」の部分でNullReferenceExceptionが起こる
その直前の「commands IsNot Nothing」はTrueと表示されるにもかかわらず・・・
理由?しゃっぱりわかりません


直接の原因かは分からないけどSECURITY_ATTRIBUTESのnLengthが設定されなかったりする
Console.WriteLineでアクセスすると12と出る、アクセスしないと0になる、、、最適化で消えちゃった?