Bug 1661 - Selector invoked from objective-c on a managed object that has been GC'ed
Summary: Selector invoked from objective-c on a managed object that has been GC'ed
Status: RESOLVED UPSTREAM
Alias: None
Product: iOS
Classification: Xamarin
Component: XI runtime ()
Version: 5.0
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2011-10-24 13:44 UTC by Quoc Nguyen
Modified: 2011-10-27 03:17 UTC (History)
2 users (show)

Tags:
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:
Status:
RESOLVED UPSTREAM

Description Quoc Nguyen 2011-10-24 13:44:43 UTC
This is a bug to question why the underlying object is being garbage collected when I do indeed have references to it. The only line of code in the below stack trace that I wrote is the "Main" method.

	public class Application {
		static void Main(string[] args) {
			UIApplication.Main(args, null, "DouglasFactorsAppDelegate");
		}
	}

Unhandled Exception: System.Exception: Selector invoked from objective-c on a managed object that has been GC'ed ---> System.MissingMethodException: No constructor found for MonoTouch.UIKit.UIScrollView+_UIScrollViewDelegate::.ctor(System.IntPtr)
  at System.Activator.CreateInstance (System.Type type, BindingFlags bindingAttr, System.Reflection.Binder binder, System.Object[] args, System.Globalization.CultureInfo culture, System.Object[] activationAttributes) [0x00180] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System/Activator.cs:300 
  at System.Activator.CreateInstance (System.Type type, System.Object[] args, System.Object[] activationAttributes) [0x00000] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System/Activator.cs:234 
  at System.Activator.CreateInstance (System.Type type, System.Object[] args) [0x00000] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System/Activator.cs:229 
  at MonoTouch.ObjCRuntime.Runtime.ConstructNSObject (IntPtr ptr, IntPtr klass) [0x0000d] in /Developer/MonoTouch/Source/monotouch/src/ObjCRuntime/Runtime.cs:197 
  --- End of inner exception stack trace ---
  at MonoTouch.ObjCRuntime.Runtime.ConstructNSObject (IntPtr ptr, IntPtr klass) [0x0002e] in /Developer/MonoTouch/Source/monotouch/src/ObjCRuntime/Runtime.cs:200 
  at MonoTouch.ObjCRuntime.Runtime.GetNSObject (IntPtr ptr) [0x0001f] in /Developer/MonoTouch/Source/monotouch/src/ObjCRuntime/Runtime.cs:244 
  at MonoTouch.ObjCRuntime.Runtime.GetNSObjectWrapped (IntPtr ptr) [0x00000] in /Developer/MonoTouch/Source/monotouch/src/ObjCRuntime/Runtime.cs:261 
  at (wrapper native-to-managed) MonoTouch.ObjCRuntime.Runtime:GetNSObjectWrapped (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) [0x00042] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:29 
  at DouglasFactors.Application.Main (System.String[] args) [0x00000] in /Users/qnguyen/Projects/Maps/DouglasFactors/Main.cs:13 
[ERROR] FATAL UNHANDLED EXCEPTION: System.Exception: Selector invoked from objective-c on a managed object that has been GC'ed ---> System.MissingMethodException: No constructor found for MonoTouch.UIKit.UIScrollView+_UIScrollViewDelegate::.ctor(System.IntPtr)
  at System.Activator.CreateInstance (System.Type type, BindingFlags bindingAttr, System.Reflection.Binder binder, System.Object[] args, System.Globalization.CultureInfo culture, System.Object[] activationAttributes) [0x00180] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System/Activator.cs:300 
  at System.Activator.CreateInstance (System.Type type, System.Object[] args, System.Object[] activationAttributes) [0x00000] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System/Activator.cs:234 
  at System.Activator.CreateInstance (System.Type type, System.Object[] args) [0x00000] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System/Activator.cs:229 
  at MonoTouch.ObjCRuntime.Runtime.ConstructNSObject (IntPtr ptr, IntPtr klass) [0x0000d] in /Developer/MonoTouch/Source/monotouch/src/ObjCRuntime/Runtime.cs:197 
  --- End of inner exception stack trace ---
  at MonoTouch.ObjCRuntime.Runtime.ConstructNSObject (IntPtr ptr, IntPtr klass) [0x0002e] in /Developer/MonoTouch/Source/monotouch/src/ObjCRuntime/Runtime.cs:200 
  at MonoTouch.ObjCRuntime.Runtime.GetNSObject (IntPtr ptr) [0x0001f] in /Developer/MonoTouch/Source/monotouch/src/ObjCRuntime/Runtime.cs:244 
  at MonoTouch.ObjCRuntime.Runtime.GetNSObjectWrapped (IntPtr ptr) [0x00000] in /Developer/MonoTouch/Source/monotouch/src/ObjCRuntime/Runtime.cs:261 
  at (wrapper native-to-managed) MonoTouch.ObjCRuntime.Runtime:GetNSObjectWrapped (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) [0x00042] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:29 
  at DouglasFactors.Application.Main (System.String[] args) [0x00000] in /Users/qnguyen/Projects/Maps/DouglasFactors/Main.cs:13
Comment 1 Rolf Bjarne Kvinge [MSFT] 2011-10-24 17:26:15 UTC
Can you upload your project so we can have a look at it?
Comment 2 Quoc Nguyen 2011-10-26 12:31:16 UTC
Rolf, I cannot upload my project and we were unable to create an isolated
reproduction of the issue because it involves using enough memory for the
GC to kick in but we did track down the issue.  The problem is with the
disposal of the UIScrollView.  It is our thoughts that it is up to
Monotouch to do proper disposal but here's what we had to do:

public class OurView : UIView {
  private UIScrollView scroll;

  public OurView() {
    scroll = new UIScrollView();
    scroll.Scrolled += Scrolled;
  }

  protected void Scrolled(object sender, EventArgs e) {
    // do some animations, UIView.Animate( ....
  }

  protected override void Dispose(bool disposing) {
    scroll.Delegate = null;
    scroll.Layer.RemoveAllAnimations();
    scroll.RemoveFromSuperview();
    scroll.Dispose();
    base.Dispose(disposing);
  }
}
Comment 3 Rolf Bjarne Kvinge [MSFT] 2011-10-26 16:42:23 UTC
If you're running on the simulator the GC will run automatically every second, so using much memory should not be a requirement for reproducing this bug.

In general you must ensure that managed objects native code has access to are reachable for the GC. You don't say what you set scroll.Delegate to, but from the stack trace my guess is that that object is freed by the GC because only native code knows about it, and when then native code tries to call that object you end up with the exception you provided.

In any case it looks like your fix is correct for this case (you're right that MonoTouch should probably be handling this a bit better, we're working on this and some cases will be fixed in a future release).
Comment 4 Quoc Nguyen 2011-10-26 18:29:06 UTC
Rolf, we never set the scroll.Delegate to anything, that seems to be a behind the scenes artifact that we are cleaning up despite no explicit assignment on our part.
Comment 5 Rolf Bjarne Kvinge [MSFT] 2011-10-27 03:17:41 UTC
You're right of course, scroll.Delegate is set when you attach to the Scrolled event.