[csharp,iOS] Fix callbacks on AOT #386

Closed
rmarinho wants to merge 6 commits from fix-logger-xamarinios into master
rmarinho commented 2017-02-10 08:12:26 -08:00 (Migrated from github.com)

When using AOT mode on Mono one can't use instance methods for callbacks, the compiler needs the MonoPInvokeCallback attribute on a static method to know how to get back to the managed world.
This worked fine without the change in JIT mode.

I not sure if we should use a MONO flag for this stuff as this could be needed for other usage that not only on iOS.

The adicional change is that one should as good practice call Dispose on the YogaNode when using callbacks to make sure we remove the handler from our dictionary, i was trying to write a test for this.. but i saw TestMeasureFuncWithDestructor was already there, but it doesn't seem correct to me, can you check @splhack.

When using AOT mode on Mono one can't use instance methods for callbacks, the compiler needs the MonoPInvokeCallback attribute on a static method to know how to get back to the managed world. This worked fine without the change in JIT mode. I not sure if we should use a __MONO__ flag for this stuff as this could be needed for other usage that not only on iOS. ~~The adicional change is that one should as good practice call Dispose on the YogaNode when using callbacks to make sure we remove the handler from our dictionary, i was trying to write a test for this.. but i saw TestMeasureFuncWithDestructor was already there, but it doesn't seem correct to me, can you check @splhack.~~
splhack commented 2017-02-10 09:08:54 -08:00 (Migrated from github.com)

I'll look into this.

I'll look into this.
rmarinho commented 2017-02-10 11:18:54 -08:00 (Migrated from github.com)

Ok after internal feedback from Alex Corrado i got a new implementation for the fix using the native ref context of Yoga.

Ok after internal feedback from Alex Corrado i got a new implementation for the fix using the native ref context of Yoga.
rmarinho commented 2017-02-10 11:19:25 -08:00 (Migrated from github.com)

The test for the Parent destructor is failing for me on AOT .. still looking at that one.

The test for the Parent destructor is failing for me on AOT .. still looking at that one.
splhack commented 2017-02-10 11:36:05 -08:00 (Migrated from github.com)

can you take a look at #387 ?

can you take a look at #387 ?
chkn commented 2017-02-10 12:17:20 -08:00 (Migrated from github.com)

@splhack It looks like a more complicated way of doing the same thing? I do see one improvement: using a weak GCHandle and only allocating one of them.

Also note that neither PR appears to ever free the GCHandle

@splhack It looks like a more complicated way of doing the same thing? I do see one improvement: using a weak GCHandle and only allocating one of them. Also note that neither PR appears to ever free the GCHandle
splhack commented 2017-02-10 12:59:10 -08:00 (Migrated from github.com)

Ah, I haven't seen the updated diff.

Ah, I haven't seen the updated diff.
splhack commented 2017-02-10 13:56:50 -08:00 (Migrated from github.com)

revised in #388

revised in #388
facebook-github-bot commented 2017-02-10 14:02:56 -08:00 (Migrated from github.com)

@splhack has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@splhack has imported this pull request. If you are a Facebook employee, you can view this diff [on Phabricator](https://phabricator.intern.facebook.com/D4546030).
splhack commented 2017-02-10 15:06:39 -08:00 (Migrated from github.com)

Could you rebase this onto the current master too? I'll merge this and #388

Could you rebase this onto the current master too? I'll merge this and #388
rmarinho commented 2017-02-10 17:07:09 -08:00 (Migrated from github.com)

It's rebased on top of master..

It's rebased on top of master..
facebook-github-bot commented 2017-02-10 17:34:28 -08:00 (Migrated from github.com)

@splhack has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@splhack has imported this pull request. If you are a Facebook employee, you can view this diff [on Phabricator](https://phabricator.intern.facebook.com/D4546030).
rolfbjarne (Migrated from github.com) reviewed 2017-02-13 00:17:04 -08:00
@@ -44,0 +39,4 @@
Logger(level, message);
}
if (level == YogaLogLevel.Error)
rolfbjarne (Migrated from github.com) commented 2017-02-13 00:11:20 -08:00

You need to store the delegate in a static field, otherwise the GC will collect it and the app will crash when the callback is called:

static Func _logger_internal = LoggerInternal;

Native.YGInteropSetLogger (_logger_internal);
You need to store the delegate in a static field, otherwise the GC will collect it and the app will crash when the callback is called: static Func _logger_internal = LoggerInternal; Native.YGInteropSetLogger (_logger_internal);
@@ -568,1 +577,3 @@
private YogaSize MeasureInternal(
#if __IOS__
[ObjCRuntime.MonoPInvokeCallback(typeof(YogaMeasureFunc))]
#endif
rolfbjarne (Migrated from github.com) commented 2017-02-13 00:16:55 -08:00

Your patch never calls GCHandle.Free.

Also you allocate a GCHandle both SetMeasureFunction and SetBaselineFunction, if both are called one of the values stored with YGNodeSetContext will be overwritten. The same will happen if either function is called more than once.

Your patch never calls `GCHandle.Free`. Also you allocate a GCHandle both SetMeasureFunction and SetBaselineFunction, if both are called one of the values stored with YGNodeSetContext will be overwritten. The same will happen if either function is called more than once.
rmarinho (Migrated from github.com) reviewed 2017-02-13 02:34:21 -08:00
@@ -44,0 +39,4 @@
Logger(level, message);
}
if (level == YogaLogLevel.Error)
rmarinho (Migrated from github.com) commented 2017-02-13 02:33:47 -08:00

@splhack can you take a note on this in your PR ?

@splhack can you take a note on this in your PR ?
@@ -568,1 +577,3 @@
private YogaSize MeasureInternal(
#if __IOS__
[ObjCRuntime.MonoPInvokeCallback(typeof(YogaMeasureFunc))]
#endif
rmarinho (Migrated from github.com) commented 2017-02-13 02:34:15 -08:00

@splhack pr #388 fixes this.

@splhack pr #388 fixes this.
splhack (Migrated from github.com) reviewed 2017-02-13 08:40:37 -08:00
@@ -44,0 +39,4 @@
Logger(level, message);
}
if (level == YogaLogLevel.Error)
splhack (Migrated from github.com) commented 2017-02-13 08:40:37 -08:00

sure

sure

Pull request closed

Sign in to join this conversation.
No description provided.