Bug 15838 - Failed to marshal the Objective-C object 0x140cb9f0 (type: MonoTouch_GKSession_ReceivedObject). Could not find an existing managed instance for this object, nor was it possible to create a new managed instance (because the type 'MonoTouch.GameKit.GKSessio
Summary: Failed to marshal the Objective-C object 0x140cb9f0 (type: MonoTouch_GKSessio...
Alias: None
Product: iOS
Classification: Xamarin
Component: Xamarin.iOS.dll ()
Version: 7.0.0.x
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: 7.0.6
Assignee: Rolf Bjarne Kvinge [MSFT]
Depends on:
Reported: 2013-10-30 21:31 UTC by dj_technohead
Modified: 2013-12-16 09:48 UTC (History)
3 users (show)

Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description dj_technohead 2013-10-30 21:31:25 UTC
Randomly getting an app crash using UIGameKit with the following information:

Failed to marshal the Objective-C object 0x140cb9f0 (type: MonoTouch_GKSession_ReceivedObject). Could not find an existing managed instance for this object, nor was it possible to create a new managed instance (because the type 'MonoTouch.GameKit.GKSession+ReceiverObject' does not have a constructor that takes one IntPtr argument).
Additional information:
	Selector: receiveData:fromPeer:inSession:context:
	Method: MonoTouch.GameKit.GKSession/ReceiverObject:Receive (MonoTouch.Foundation.NSData,string,MonoTouch.GameKit.GKSession,intptr)

  at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
  at MonoTouch.UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0004c] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:38 
  at SmartShare.Touch.Application.Main (System.String[] args) [0x00030] in /Users/dj_technohead/Projects/Teraport/Mobile/MonoCross/SmartShare/SmartShare.Touch/Main.cs:26 

The GKSession is persisted in a class member field so I don't believe it can be going out of scope. The app does not directly reference any ReceivedObject class.
Comment 1 Rolf Bjarne Kvinge [MSFT] 2013-10-31 08:58:03 UTC
The ReceivedObject instance is created if you attach an event handler to the ReceiveData event (and stored in the GKSession instance), and cleared out if you call SetDataReceiveHandler (but this would cause any subsequent calls to receiveData:fromPeer:inSession:context: to be routed to the data receive handler you specify when you call SetDataReceiveHandler).

Which means that the only possibility is that the GKSession has somehow been freed by the GC.

This should be easy to verify: store the GKSession in a static list, which will prevent it from ever being collected until it's removed from that list. Could you try this and see if the crash goes away?
Comment 2 dj_technohead 2013-11-01 21:55:22 UTC
Hi Rolf,
   the GKSession object is stored as a member of a class GKConnection which itself is a member of a class GKConnectionManager which is a static class. This should prevent it from being GCed, right?
Comment 3 dj_technohead 2013-11-03 16:42:22 UTC
Hi Rolf,
   I've tried persisting the GKSession directly in the AppDelegate instance (which is also a static class), and I'm still seeing the crash occur.

Comment 4 Rolf Bjarne Kvinge [MSFT] 2013-11-04 04:24:17 UTC
Hi Dennis,

Is this something you can create a test case for (or could I get access to your project)?
Comment 6 Rolf Bjarne Kvinge [MSFT] 2013-11-05 06:01:28 UTC
I can reproduce the problem if I tap Connect several times (every time it creates a new GKSession object and overwrites the previous one).

Are you sure you're only creating one GKSession object?
Comment 7 dj_technohead 2013-11-05 13:25:32 UTC
I am only pressing connect once. The real test is after they have connected and it sends photos from one device to another.
Comment 8 dj_technohead 2013-11-05 13:31:21 UTC
I've just verified in the real app that GKSession is only created once.
Comment 9 dj_technohead 2013-11-09 11:51:54 UTC
Hi Rolf,
   I can replicate this bug every time, although the number of files it needs to transfer before it occurs varies. I've verified that it is not creating connections multiple times, just that the test app allows you to do so. So far, I m certain this is a real bug.

Comment 10 Rolf Bjarne Kvinge [MSFT] 2013-11-11 19:04:18 UTC
OK, looks like I can reproduce it now.

I didn't have enough pictures on my device before.
Comment 11 Rolf Bjarne Kvinge [MSFT] 2013-11-12 19:14:34 UTC
This is a race condition in our code.
Comment 12 Rolf Bjarne Kvinge [MSFT] 2013-11-13 07:48:28 UTC

monotouch/master: d5be24e1c01d49d20c8a6405c40bd47a9843d895
Comment 13 dj_technohead 2013-11-13 08:59:25 UTC
Thanks Rolf.
Comment 15 dj_technohead 2013-11-13 23:39:20 UTC
Hi Rolf, 
  the fix appears to be working. 

Comment 16 PJ 2013-12-11 18:45:57 UTC
This fix is planned to be released with Xamarin.iOS 7.0.6, which should hit the beta channel before December 23rd.
Comment 17 Mohit Kheterpal 2013-12-16 09:48:51 UTC
As per comment 15, this issue is fixed.

Hence closing this issue.