[Mono-list] Failing dlopen on Linux via DllImport with error message 'dlopen: invalid caller'

Jean-Michel.Perraud at csiro.au Jean-Michel.Perraud at csiro.au
Tue Nov 26 00:34:15 UTC 2013


Thanks for the best practice advice; I'll fix this.

Regarding the dlopen failure, this does seem to occur deep in glibc, http://code.woboq.org/userspace/glibc/elf/dl-open.c.html, way in where this is unintelligible to me. 
  /* Check whether _dl_open() has been called from a valid DSO.  */
193	  if (__check_caller (args->caller_dl_open,
194			      allow_libc|allow_libdl|allow_ldso) != 0)
195	    _dl_signal_error (0, "dlopen", NULL, N_("invalid caller"));

J-M

-----Original Message-----
From: Jonathan Pryor [mailto:jonpryor at vt.edu] 
Sent: Tuesday, 26 November 2013 11:21 AM
To: Perraud, Jean-Michel (CLW, Black Mountain)
Cc: mono-list
Subject: Re: [Mono-list] Failing dlopen on Linux via DllImport with error message 'dlopen: invalid caller'

On Nov 25, 2013, at 5:47 PM, Jean-Michel.Perraud at csiro.au wrote:
>      [DllImport("libdl")]
>      [return: MarshalAs(UnmanagedType.LPStr)]
>      private static extern string dlerror();

You should (almost?) never use `string` (or any other reference type) as the return type in a P/Invoke method:

	http://mono-project.com/DllImport#Classes_and_Structures_as_Return_Values
> The CLI assumes that all memory that is passed between the CLI/unmanaged code boundary is allocated via a common memory allocator. The developer does not get a choice in which memory allocator is used. For managed code, the Marshal.AllocCoTaskMem method can be used to allocate memory, Marshal.FreeCoTaskMem is used to free the memory allocated by Marshal.AllocCoTaskMem, andMarshal.ReAllocCoTaskMem is used to resize a memory region originally allocated by Marshal.AllocCoTaskMem.
> 
> Since classes are passed by reference, a pointer is returned, and the runtime assumes that it must free this memory to avoid a memory leak.

You should instead use IntPtr as the return type, then use Marshal.PtrToStringAnsi() to convert the IntPtr into a string.

 - Jon



More information about the Mono-list mailing list