Forum Xamarin.iOS

Embedding Python: pointer being freed was not allocated

AdamVenturellaAdamVenturella USMember ✭✭

My setup looks as follows (I have tried this with python 2.7.9 and 3.4.3), curious if I am just missing something obvious here or if this is a larger issue.

In DidFinishLaunching(NSNotification notification) I am calling the following:

var test = Python.Py_GetVersion();
Console.WriteLine("Python Version is: {0}", test);

Python.Py_GetVersion() has been registered like this:

[DllImport("libpython2.7", CallingConvention=CallingConvention.Cdecl, ExactSpelling=true, CharSet=CharSet.Ansi)]
public unsafe static extern string Py_GetVersion();

Upon the code executing, the following error is thrown:

malloc: *** error for object 0x10d93d8a0: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug

Stacktrace:

  at <unknown> <0xffffffff>
  at (wrapper managed-to-native) object.__icall_wrapper_mono_marshal_free (intptr) <IL 0x0000d, 0xffffffff>
  at (wrapper managed-to-native) MacAppDemo.Python.Py_GetVersion () <IL 0x0001f, 0xffffffff>
  at MacAppDemo.AppDelegate.DidFinishLaunching (Foundation.NSNotification) [0x0006b] in /Users/aventurella/Dropbox/Projects/Xamarin/MacAppDemo/AppDelegate.cs:36
  at (wrapper runtime-invoke) <Module>.runtime_invoke_void__this___object (object,intptr,intptr,intptr) <IL 0x00062, 0xffffffff>
  at <unknown> <0xffffffff>
  at (wrapper managed-to-native) AppKit.NSApplication.NSApplicationMain (int,string[]) <IL 0x00093, 0xffffffff>
  at AppKit.NSApplication.Main (string[]) [0x00041] in /Users/builder/data/lanes/1503/8a0e7bca/source/maccore/src/AppKit/NSApplication.cs:94
  at MacAppDemo.MainClass.Main (string[]) [0x00007] in /Users/aventurella/Dropbox/Projects/Xamarin/MacAppDemo/Main.cs:12
  at (wrapper runtime-invoke) <Module>.runtime_invoke_void_object (object,intptr,intptr,intptr) <IL 0x00060, 0xffffffff>

Native stacktrace:

    0   MacAppDemo                          0x00000001000ec41a mono_handle_native_sigsegv + 282
    1   libsystem_platform.dylib            0x00007fff92adbf1a _sigtramp + 26
    2   ???                                 0x0000000102200260 0x0 + 4330619488
    3   libsystem_c.dylib                   0x00007fff94bd5b53 abort + 129
    4   libsystem_malloc.dylib              0x00007fff93fed937 nano_size + 0
    5   ???                                 0x000000010211d781 0x0 + 4329691009

Answers

  • ChrisHamonsChrisHamons USForum Administrator, Xamarin Team Xamurai

    I've never done anything at all with this, but my guess is that something with the string here:

    public unsafe static extern string Py_GetVersion();

    is using the wrong allocator. The error:

    malloc: *** error for object 0x10d93d8a0: pointer being freed was not allocated

    suggests it is being allocated with one allocator (pythons?) and you are trying to free it using malloc.

  • AdamVenturellaAdamVenturella USMember ✭✭

    I ended up finding the solution in the Mono Docs:

    http://www.mono-project.com/docs/advanced/pinvoke/#strings-as-return-values

    Specifically I had to do this:

    using System.Runtime.InteropServices;
    
    
    public static class Runtime
    {
        public static string Version {
            get {
                var ver = Python.Py_GetVersion();
                return Marshal.PtrToStringAnsi(ver);
            }
        }
    }
    
    public static class Python{
        [DllImport(python, CallingConvention=CallingConvention.Cdecl, ExactSpelling=true, CharSet=CharSet.Ansi)]
        public unsafe static extern IntPtr Py_GetVersion();
    
        // Note the return type here is IntPtr, and we Marshall it back to a string in the Runtime.Version getter above.
    }
    
Sign In or Register to comment.