[Mono-dev] Re: DllImport on mono at linux

Robert Jordan robertj at gmx.net
Mon Jun 12 17:55:02 EDT 2006


shw wrote:
> Hi!
> I've made some app with mono. To generate and check serial numbers it
> uses binary library.
> On windows everything is working - on linux - not.
> 
> I've tried to compile some different ways and came to compile library with:
> g++ -shared -fPIC -o libpossible-keys.so *.cpp
> 
> with this kind of compilation mono finds library, finds function but
> still throws an error:
> 
> Mono-INFO: DllImport attempting to load: 'possible-keys'.
> Mono-INFO: DllImport loading location: 'libpossible-keys.so'.
> Mono-INFO: DllImport error loading library: 'libpossible-keys.so: nie
> moĹźna otworzyÄ pliku obiektu dzielonego: Nie ma takiego pliku ani
> katalogu'. (in english it is sth like "couldn't open shared object
> file: no file or directory")
> Mono-INFO: DllImport loading library: './libpossible-keys.so'.
> Mono-INFO: Searching for 'CreateActivation'.
> Mono-INFO: Probing 'CreateActivation'.
> Mono-INFO: Found as 'CreateActivation'.
> Wrapper tester
> *** glibc detected *** free(): invalid next size (fast): 0x0824cae0 ***
> 
> What is wrong?
> The same code on Windows works, when i compile source without -shared
> and run with sth like
> ./libpossible-keys.so arguments_to_generate_activation_key
> it works, but as method with DllImport - not.

This sounds to me like this bad sample:

// C
char *foo ()
{
	return "bar";
}

// C#
[DllImport(...)]
static extern string foo ();

The runtime always tries to release the returned pointer with free (),
when the managed signature has a string retval.

Of course, this doesn't work in the sample above, because "bar" is
a constant. MS.NET doesn't crash on that, maybe because Win32's
free () is able to detect this case.

Either return freeable memory (for the sample: return strdup ("bar"))
or change the signature to IntPtr foo () and marshal the retval
yourself.

Robert




More information about the Mono-devel-list mailing list