Bug 21996 - Missing StructLayout attribute on CriticalHandle
Summary: Missing StructLayout attribute on CriticalHandle
Alias: None
Product: Class Libraries
Classification: Mono
Component: mscorlib ()
Version: master
Hardware: PC Linux
: --- normal
Target Milestone: Untriaged
Assignee: Rodrigo Kumpera
Depends on:
Reported: 2014-08-11 10:18 UTC by Gusman
Modified: 2016-04-14 06:42 UTC (History)
2 users (show)

Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Gusman 2014-08-11 10:18:52 UTC
CriticalHandle class does not have the [StructLayout] attribute, and without that attribute it cannot be used as parameter/result on native API calls.


using System;
using System.Runtime.InteropServices;

namespace debugService
	class MainClass

		public static void Main (string[] args)
			var handle = new Tryhandle ();

			Tryhandle.dlclose (handle); //Type System.Runtime.InteropServices.CriticalHandle which is passed to unmanaged code must have a StructLayout attribute.

	class Tryhandle : CriticalHandle

		public Tryhandle() : base(IntPtr.Zero){

		[DllImport("dl", SetLastError = true)]
		public static extern int dlclose(Tryhandle hModule);

		#region implemented abstract members of CriticalHandle

		protected override bool ReleaseHandle ()
			return dlclose (this) == 0;

		public override bool IsInvalid {
			get {
				return this.handle == IntPtr.Zero;


Comment 1 Marek Safar 2014-08-12 03:15:16 UTC
That's feature. In general abstract .net classes are never intended to be passed directly to unmanaged code because a specific layout cannot be guaranteed.
Comment 2 Gusman 2014-08-12 03:33:49 UTC
Then why the SafeHandle which uses the same exact pattern works and has the StructLayout attribute?

It must work in the same way as a SafeHandle, a CriticalHandle is a feature to wrap an IntPtr with a custom finalizer to ensure all the unmanaged resources are freed in case of error.
Comment 3 Marek Safar 2014-08-12 03:42:50 UTC
I don't know why Rodrigo added it to SafeHandle. It looks like bug to me.
Comment 4 Gusman 2014-08-12 04:10:26 UTC
It's not a bug, it's clearly docummented on the mono base documentation:


"There are two other issues with classes. First of all, classes by default use LayoutKind.Auto layout. This means that the ordering of class data members is unknown, and won't be determined until runtime. The runtime can rearrange the order of members in any way it chooses, to optimize for access time or data layout space. As such, you MUST use the StructLayout attribute and specify a LayoutKind value of LayoutKind.Sequential or LayoutKind.Explicit."


"The runtime support currently has:

    Support for passing SafeHandles on P/Invoke parameters (with AddRef/Release)
    Support for passing SafeHandles on P/Invoke structures (no AddRef/Release yet).
    Support for creating SafeHandles-derivatives on return values fromP/Invoke.
    Support for "ref SafeHandles" on P/Invoke parameters.
    throwing ArgumentNullException if null SafeHandles are passed to unmanaged code."
Comment 5 Rodrigo Kumpera 2014-08-26 00:11:54 UTC
SafeHandle has it since it's exposed to the runtime. It's there so  fields won't be reordered.
Comment 6 Rodrigo Kumpera 2016-04-14 06:42:41 UTC
CriticalHandle is not seq layout on dotnet, no reason for us to be.