Bug 28290

Summary: Marshal.AllocCoTaskMem does not throw OutOfMemoryException on alloc failure
Product: [Mono] Runtime Reporter: Brandon Streiff <brandon.streiff>
Component: InteropAssignee: Bugzilla <bugzilla>
Status: RESOLVED FIXED    
Severity: normal CC: joe.friedrichsen, mono-bugs+mono, mono-bugs+runtime, vargaz
Priority: ---    
Version: 3.12.0   
Target Milestone: ---   
Hardware: PC   
OS: Windows   
Tags: Is this bug a regression?: ---
Last known good build:
Attachments: Sample program (with VS2012 project) to demonstrate

Description Brandon Streiff 2015-03-21 09:42:44 UTC
Created attachment 10440 [details]
Sample program (with VS2012 project) to demonstrate

The Microsoft .NET runtime throws OutOfMemoryException on allocation failure from Marshal.AllocCoTaskMem, as documented here:
https://msdn.microsoft.com/en-us/library/system.runtime.interopservices.marshal.alloccotaskmem%28v=vs.100%29.aspx

The Mono runtime will return a null pointer. This is not expected.

The Mono runtime does appear to throw an OutOfMemoryException if you use Marshal.AllocHGlobal.

When running the attached program using the MS.NET and the Mono runtimes, respectively:

Z:\AllocFailure\bin\Debug>AllocFailure.exe
Got OutOfMemoryException from AllocCoTaskMem(int.MaxValue)
Got OutOfMemoryException from AllocHGlobal(int.MaxValue)

Z:\AllocFailure\bin\Debug>mono AllocFailure.exe
Got null pointer from AllocCoTaskMem(int.MaxValue)
Got OutOfMemoryException from AllocHGlobal(int.MaxValue)




Mono version:

Z:\AllocFailure>mono -V
Mono JIT compiler version 3.12.0 (Visual Studio built mono)
Copyright (C) 2002-2014 Novell, Inc, Xamarin Inc and Contributors. www.mono-project.com
        TLS:           normal
        SIGSEGV:       normal
        Notification:  Thread + polling
        Architecture:  x86
        Disabled:      none
        Misc:          softdebug
        LLVM:          supported, not enabled.
        GC:            sgen
Comment 1 Zoltan Varga 2015-03-22 03:56:24 UTC
Fixed in mono master 6a74b1952ffc7ba4a5331bf1356c4035769f2480.