NullReferenceException Object reference not set to an instance of an object (CocosSharp)

I've been using Cocossharp and Cocossharp.Forms to develop a boardgame. All code is located in a PCL library and only few dependency services and Content are located in platform specific projects for Android, iOS and WP8.

The Android version is working and has already been deployed to Google Play, but in the iOS version i get a "random" exit of the game. Some times an Exception is visible in Xamarin.Insights, but often it is not. The error occurs more often on real device than in the emulator.

SIGABRT Crash in wrapper_stelemref_object_virt_stelemref_class_intptr_object
Thread 0 SIGABRT Crash in wrapper_stelemref_object_virt_stelemref_class_intptr_object 0 libsystem_kernel.dylib __pthread_kill (in libsystem_kernel.dylib) + 8 1 libsystem_c.dylib abort (in libsystem_c.dylib) + 109 2 StarOfAfricaiOS print_exception(MonoObject*, bool, NSMutableString*) (in a58c7d3f5a543771b616b7d507f16739) (runtime.m:782) 3 StarOfAfricaiOS mono_invoke_unhandled_exception_hook (in a58c7d3f5a543771b616b7d507f16739) (mini-exceptions.c:2615) 4 StarOfAfricaiOS mono_handle_exception_internal (in a58c7d3f5a543771b616b7d507f16739) (mini-exceptions.c:1574) 5 StarOfAfricaiOS mono_handle_exception (in a58c7d3f5a543771b616b7d507f16739) (mini-exceptions.c:1799) 6 StarOfAfricaiOS handle_signal_exception (in a58c7d3f5a543771b616b7d507f16739) (exceptions-arm.c:521) 7 StarOfAfricaiOS wrapper_stelemref_object_virt_stelemref_class_intptr_object (in a58c7d3f5a543771b616b7d507f16739) + 164 8 StarOfAfricaiOS CocosSharp_CCNode_Visit_CocosSharp_CCAffineTransform_ (in a58c7d3f5a543771b616b7d507f16739) + 308 9 StarOfAfricaiOS CocosSharp_CCLayer_Visit_CocosSharp_CCAffineTransform_ (in a58c7d3f5a543771b616b7d507f16739) + 268 10 StarOfAfricaiOS CocosSharp_CCNode_Visit_CocosSharp_CCAffineTransform_ (in a58c7d3f5a543771b616b7d507f16739) + 308 11 StarOfAfricaiOS CocosSharp_CCNode_Visit (in a58c7d3f5a543771b616b7d507f16739) + 88 12 StarOfAfricaiOS CocosSharp_CCGameView_Draw (in a58c7d3f5a543771b616b7d507f16739) + 156 13 StarOfAfricaiOS CocosSharp_CCGameView_OnRenderFrame_OpenTK_FrameEventArgs (in a58c7d3f5a543771b616b7d507f16739) + 168 14 StarOfAfricaiOS CocosSharp_CCGameView_RunIteration_Foundation_NSTimer (in a58c7d3f5a543771b616b7d507f16739) + 88 15 StarOfAfricaiOS Foundation_NSTimerActionDispatcher_Fire_Foundation_NSTimer (in a58c7d3f5a543771b616b7d507f16739) + 52 16 StarOfAfricaiOS wrapper_runtime_invoke_object_runtime_invoke_dynamic_intptr_intptr_intptr_intptr (in a58c7d3f5a543771b616b7d507f16739) + 256 17 StarOfAfricaiOS mono_jit_runtime_invoke (in a58c7d3f5a543771b616b7d507f16739) (mini-runtime.c:2325) 18 StarOfAfricaiOS mono_runtime_invoke (in a58c7d3f5a543771b616b7d507f16739) (object.c:2783) 19 StarOfAfricaiOS native_to_managed_trampoline_3(objc_object*, objc_selector*, _MonoMethod**, objc_object*, char const*, char const*, char const*) (in a58c7d3f5a543771b616b7d507f16739) (registrar.m:105) 20 StarOfAfricaiOS -[__Xamarin_NSTimerActionDispatcher xamarinFireSelector:] (in a58c7d3f5a543771b616b7d507f16739) (registrar.m:2546) 21 Foundation __NSFireTimer (in Foundation) + 65 22 CoreFoundation __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ (in CoreFoundation) + 15 23 CoreFoundation __CFRunLoopDoTimer (in CoreFoundation) + 937 24 CoreFoundation __CFRunLoopRun (in CoreFoundation) + 1501 25 CoreFoundation CFRunLoopRunSpecific (in CoreFoundation) + 517 26 CoreFoundation CFRunLoopRunInMode (in CoreFoundation) + 109 27 GraphicsServices GSEventRunModal (in GraphicsServices) + 161 28 UIKit UIApplicationMain (in UIKit) + 145 29 StarOfAfricaiOS wrapper_managed_to_native_UIKit_UIApplication_UIApplicationMain_int_string___intptr_intptr (in a58c7d3f5a543771b616b7d507f16739) + 272 30 StarOfAfricaiOS UIKit_UIApplication_Main_string___intptr_intptr (in a58c7d3f5a543771b616b7d507f16739) + 52 31 StarOfAfricaiOS UIKit_UIApplication_Main_string___string_string (in a58c7d3f5a543771b616b7d507f16739) + 204 32 StarOfAfricaiOS StarOfAfrica_iOS_Application_Main_string__ (in a58c7d3f5a543771b616b7d507f16739) + 72 33 StarOfAfricaiOS wrapper_runtime_invoke_object_runtime_invoke_dynamic_intptr_intptr_intptr_intptr (in a58c7d3f5a543771b616b7d507f16739) + 256 34 StarOfAfricaiOS mono_jit_runtime_invoke (in a58c7d3f5a543771b616b7d507f16739) (mini-runtime.c:2325) 35 StarOfAfricaiOS mono_runtime_invoke (in a58c7d3f5a543771b616b7d507f16739) (object.c:2783) 36 StarOfAfricaiOS mono_runtime_exec_main (in a58c7d3f5a543771b616b7d507f16739) (object.c:4038) 37 StarOfAfricaiOS xamarin_main (in a58c7d3f5a543771b616b7d507f16739) (monotouch-main.m:404) 38 StarOfAfricaiOS main (in a58c7d3f5a543771b616b7d507f16739) (main.armv7.m:85) 39 libdyld.dylib start (in libdyld.dylib) + 3

NullReferenceException Object reference not set to an instance of an object (CocosSharp)
System.NullReferenceException Object reference not set to an instance of an object at CocosSharp.CCNode.Visit (CocosSharp.CCAffineTransform& parentWorldTransform) <0xcadea8 + 0x00108> in <filename unknown>:0 at CocosSharp.CCNode.Visit (CocosSharp.CCAffineTransform& parentWorldTransform) <0xcadea8 + 0x00133> in <filename unknown>:0 at CocosSharp.CCLayer.Visit (CocosSharp.CCAffineTransform& parentWorldTransform) <0xd5fda8 + 0x0010b> in <filename unknown>:0 at CocosSharp.CCNode.Visit (CocosSharp.CCAffineTransform& parentWorldTransform) <0xcadea8 + 0x00133> in <filename unknown>:0 at CocosSharp.CCNode.Visit () <0xcade44 + 0x00057> in <filename unknown>:0 at CocosSharp.CCGameView.Draw () <0xca424c + 0x0009b> in <filename unknown>:0 at CocosSharp.CCGameView.OnRenderFrame (OpenTK.FrameEventArgs e) <0xca6000 + 0x000a7> in <filename unknown>:0 at CocosSharp.CCGameView.RunIteration (Foundation.NSTimer timer) <0xca5f9c + 0x00057> in <filename unknown>:0 at Foundation.NSTimerActionDispatcher.Fire (Foundation.NSTimer timer) <0x3a5304 + 0x00033> in <filename unknown>:0 at (wrapper managed-to-native) UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr) at UIKit.UIApplication.Main (System.String[] args, IntPtr principal, IntPtr delegate) <0x361234 + 0x00033> in <filename unknown>:0 at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) <0x36115c + 0x000cb> in <filename unknown>:0 at StarOfAfrica.iOS.Application.Main (System.String[] args) <0x63c74 + 0x00047> in <filename unknown>:0

I've pinpointed the error to occur when using my GenericPopup view, when the user clicks a button and a result is expected to be returned.

GenericPopup view
`private void ButtonClick(List touches, CCEvent touchEvent)
try {
if (touches.Count > 0) {
CCPoint firstTouch = touches [0].Location;
foreach(ImageButton ib in this.Buttons) {
CCRect spriteRect = new CCRect (ib.BoundingBoxTransformedToWorld.MinX - (ib.ScaledContentSize.Width / 2),
ib.BoundingBoxTransformedToWorld.MinY - (ib.ScaledContentSize.Height / 2),

            if (spriteRect.ContainsPoint (firstTouch) && ib.EnabledButton) {
                ib.DisableButton ();
} catch(Exception e) {
    throw new GameboardViewException ("ButtonClick", e);


public async Task GetResults()
// Wait for the user to click Continue.
buttonClicked = new TaskCompletionSource();
Object o = await buttonClicked.Task;
return o;

Result is expected to return to the Gameboard view
`public async Task<GameRules.ActionType> SelectPostAction(Player player)
try {
gamestatus.Text = "SelectPostAction";
ImageButton pay = new ImageButton(sprites.GetSprite(GameRules.Sprite.GOLDCOIN), "", GameRules.ActionType.PAY_SEARCH);
if (player.coins < GameRules.PRICE_SEARCH)
ImageButton spin = new ImageButton(sprites.GetSprite(GameRules.Sprite.WHEEL), "", GameRules.ActionType.SPIN);
spin.Scale = 0.5f;

            List<ImageButton> buttons = new List<ImageButton>() { pay, spin };
            String text = "Pay " + GameRules.PRICE_SEARCH + " goldcoins, to reveal the treasure, or spin 4, 5 or 6 at the wheel of fortune.";
            var Popup = new GenericPopup(Constants.Layout.Layout2, "Pay or spin", text, sprites.GetSprite(GameRules.Sprite.POPUP), buttons);
            Popup.ScaleX = scaleWidth * CCSprite.DefaultTexelToContentSizeRatios.Width;
            Popup.ScaleY = scaleHeight * CCSprite.DefaultTexelToContentSizeRatios.Height;
            Popup.Position = this.ContentSize.Center;
            this.AddChild(Popup, 3);
            var result = await Popup.GetResults();
            this.RemoveChild(Popup, true);
            return (GameRules.ActionType)result;
        catch (Exception e)
            throw new GameboardViewException("SelectPostAction error", e);

I thought it had to do with the GC, so i tried setting GC to run every second, but the error was still random occuring (Still related to button clicks).

Any ideas how to find the root of this error?

Kind regards

Package info:
<packages> <package id="CocosSharp" version="" targetFramework="xamarinios10" /> <package id="CocosSharp.Forms" version="" targetFramework="xamarinios10" /> <package id="SQLite.Net.Core-PCL" version="3.1.1" targetFramework="xamarinios10" /> <package id="SQLite.Net-PCL" version="3.1.1" targetFramework="xamarinios10" /> <package id="Xamarin.Forms" version="" targetFramework="xamarinios10" /> <package id="Xamarin.Google.iOS.Analytics" version="" targetFramework="xamarinios10" /> <package id="Xamarin.Insights" version="1.11.3" targetFramework="xamarinios10" /> </packages>



  • I think i found the problem in this code
    var result = await Popup.GetResults(); this.RemoveChild(Popup, true);

    My popup view is removed before all code has completed in the Popup class.

    What is the best practice, when i need to get an result from a popup view?

Sign In or Register to comment.