trouble with object lifetime...

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

trouble with object lifetime...

David Jeske
I'm having trouble with MonoTouch segfaults. It seems to be related to programming patterns where I expect iOS objects to "own" objects I created in MonoTouch, such as the following code in a view init method. I am using SGen and the "reference counting extension - preview" because they seemed like they would make this better, but they didn't. 

{
   UIButton reRollButton = new UIButton(new RectangleF(20,0,Bounds.Width-40 ,40));
   reRollButton.SetTitle("Re-Roll",UIControlState.Normal);
   reRollButton.AddTarget(delegate(object sender, EventArgs e) {
        this.SetupNewRandomWords();
   } ,UIControlEvent.TouchUpInside);
   reRollButton.SetBackgroundImage(controller.glassButtonBackground,UIControlState.Normal);
   slView.AddSubview(reRollButton);
}


If I make "reRollButton" a class instance variable, I don't see a crash. As a result, it seems like the problem is that the garbage collector is not honoring the AddRef that happens inside AddSubview. Any Idea why that is? This seems to me like a pretty fundamental problem in Objective-C / C# objective lifetime compatibility. Am I mis-understanding something? How do I get object-lifetime to work correctly? 

_______________________________________________
MonoTouch mailing list
[hidden email]
http://lists.ximian.com/mailman/listinfo/monotouch
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: trouble with object lifetime...

Rolf Bjarne Kvinge
Hi,

This is due to an optimization, we do not make instances of non-inherited framework types track the life-time of their corresponding native/ObjC object.

Your example will work correctly if you use a class derived from UIButton:

class MyButton : UIButton { ... }

Best regards,
Rolf

On Tue, Jan 22, 2013 at 7:35 PM, David Jeske <[hidden email]> wrote:
I'm having trouble with MonoTouch segfaults. It seems to be related to programming patterns where I expect iOS objects to "own" objects I created in MonoTouch, such as the following code in a view init method. I am using SGen and the "reference counting extension - preview" because they seemed like they would make this better, but they didn't. 

{
   UIButton reRollButton = new UIButton(new RectangleF(20,0,Bounds.Width-40 ,40));
   reRollButton.SetTitle("Re-Roll",UIControlState.Normal);
   reRollButton.AddTarget(delegate(object sender, EventArgs e) {
        this.SetupNewRandomWords();
   } ,UIControlEvent.TouchUpInside);
   reRollButton.SetBackgroundImage(controller.glassButtonBackground,UIControlState.Normal);
   slView.AddSubview(reRollButton);
}


If I make "reRollButton" a class instance variable, I don't see a crash. As a result, it seems like the problem is that the garbage collector is not honoring the AddRef that happens inside AddSubview. Any Idea why that is? This seems to me like a pretty fundamental problem in Objective-C / C# objective lifetime compatibility. Am I mis-understanding something? How do I get object-lifetime to work correctly? 

_______________________________________________
MonoTouch mailing list
[hidden email]
http://lists.ximian.com/mailman/listinfo/monotouch



_______________________________________________
MonoTouch mailing list
[hidden email]
http://lists.ximian.com/mailman/listinfo/monotouch
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: trouble with object lifetime...

David Jeske
On Wed, Jan 23, 2013 at 1:37 AM, Rolf Bjarne Kvinge <[hidden email]> wrote:
This is due to an optimization, we do not make instances of non-inherited framework types track the life-time of their corresponding native/ObjC object.

Thanks for the reply. Can you explain a bit further? 

For example, I don't understand why Mono/C# tracking the lifetime of the native object is related to the native UIView tracking the lifetime of the native UIButton. I expect my "UIView.AddSubview(UIButton)" to cause the native UIView to hold a reference to the native UIButton alive, even if the C# wrapper object was collected.

...or is this a problem with my "target" delegate being collected, and not the UIButton itself?

This problem is one that I run into frequently when using Xamarin. Because my UI is instantiated in code, my classes are filled with superfluous instance variables holding views and delegates simply to avoid them being prematurely collected. Subclassing every one of those would be even more cumbersome than making them instance variables. I encourage you to reconsider this optimization, as it's a constant re-occuring "surprise" when these objects being collected cause segfaults. 

I don't know much about the details of how the Xamarin C# mirrors work, but I would think there would be a way to make the API put a mirror in place anytime a native object is returned without breaking the native-to-native, or native-to-C#-delegate lifetime tracking.



_______________________________________________
MonoTouch mailing list
[hidden email]
http://lists.ximian.com/mailman/listinfo/monotouch
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: trouble with object lifetime...

David Jeske
I'm still having trouble with this mysterious "optimization" regarding non-inherited UIKit framework types and object lifetimes. Can someone explain exactly how this optimization works, or point me to the relevant code?


_______________________________________________
MonoTouch mailing list
[hidden email]
http://lists.ximian.com/mailman/listinfo/monotouch
Loading...