Monobjc / monobjc

Git Repository for the Monobjc Project
http://www.monobjc.net/
17 stars 3 forks source link

MethodNotFoundException in GetExtern(), possibly during JIT #415

Open aarononeal opened 11 years ago

aarononeal commented 11 years ago

I ran into this issue today and wanted to document it.

void Start() {
    ObjectiveCRuntime.Initialize();
    var x = NSApplication.NSApplicationLaunchRemoteNotificationKey;
}

Running that I get MethodNotFoundException.

What seems to happen is Start() is JIT compiled, NSApplicationLaunchRemoteNotificationKey is resolved, which resolves ObjectiveCRuntime.GetExtern() which internally uses GetInstanceInternal. The latter is an internal method which hasn't been registered yet because the bridge hasn't been bootstrapped because ObjectiveCRuntime.Initialize() hasn't actually executed yet. More specifically, the static constructor for ObjectiveCRuntime hasn't executed yet because Start() is still being compiled.

Moving the constant extern to a separate method works around it.

void Start() {
    ObjectiveCRuntime.Initialize();
    CheckConstant();
}
void CheckConstant() {
    var x = NSApplication.NSApplicationLaunchRemoteNotificationKey;
}