Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

I've been using my bindings and static Accelerate Obj C library for a long time in Xamarin.iOS. Due to the unified API and the 64 bit arch in newer iOS devices i was forced to successfully update and compile my new version of library and link it with appropriate bindings.

To achieve good performance and no loss of time between data transfer obj-C<->C# due to working with big chunks of data (big image arrays) i decided to bind arrays from c# to obj-c using unsafe code and generally wrapping the array using an IntPtr, i.e:

private IntPtr WrapIntArray(int[] array) {
    IntPtr intPtr;
    unsafe
    {
        fixed (int* pArray = array)
        {
            intPtr = new IntPtr((void *) pArray);
        }
    }
    return intPtr;
}

This IntPtr is then used to be passed to the exported bound method:

[Export("....")]
void method(IntPtr input, IntPtr output);

My library works well in armv7 devices if run on monotouch 32 bits.

When i decided today to run in a build configuration of armv7+arm64 my library stopped working and i have a crash. After investigating and debugging i found that now when i declare my IntPtr wrapper for array in C# its size 8 bytes (64 bits).

Because i use the obj-c library to set the "output" of the method by assigning values of 32 bits to the array (i.e. output[43] = 2) causes the crash.

Can someone please help me understand how can i cope with this new case the way i do bindings and without changing my obj-c library?

Thanks

Antonio

share|improve this question

1 Answer 1

The biggest problem with your sample above is that you get the address to the array and the address is only guaranteed to remain fixed for the duration of the "fixed" statement. Once you are out of it, the GC might move the data.

Now, since these are images, and thus likely large, the GC will likely not move your data, but that is a matter of heuristics for the GC. It is best to avoid the practice.

The IntPtrs on your method declaration should be visible in C as "void *" or something equivalent. So it really depends on what you are doing on the C side.

share|improve this answer
    
Thanks for your valuable insight Miguel.My issue was solved when i made the bindings forced to use 32 bit structures (int,float) instead of new nints/nfloats.I've been using this way because its pretty efficient to move from-to ObjC.For us it is critical to be efficient on this aspect. F.ex:in Android we had to recur to the use of Java.Nio direct byte buffers to have efficient bindings to Renderscript.First,in Xamarin.iOS,i tried NSArray but it was pretty inefficient.Do you have any other "safe" way to bind big array of data?Thanks again for your time and a pleasure to talk to you! Antonio – Antonio Lopes Mar 29 at 18:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.