Button events of custom UITableViewCell are crashing simulator

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Button events of custom UITableViewCell are crashing simulator

Yawar Khan
I have been working on monotouch for almost two months. I am facing a weird problem.
I had used "UITableViewCell" to make customized cell view and it is working perfectly in cases of pictures and labels enclosed.
Then I added a button in custom cell with attached event. 
It is appearing properly but when I clicked on those buttons it crashed the simulator, with following error

"Stacktrace:

  at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication.UIApplicationMain (int,string[],intptr,intptr) <IL 0x0009f, 0xffffffff>
  at MonoTouch.UIKit.UIApplication.Main (string[],string,string) [0x00038] in /Users/plasma/Source/iphone/monotouch/UIKit/UIApplication.cs:26
  at MonoTouch.UIKit.UIApplication.Main (string[]) [0x00000] in /Users/plasma/Source/iphone/monotouch/UIKit/UIApplication.cs:31
  at MezziPhone.Application.Main (string[]) [0x00063] in /Users/folio3/Desktop/iPhone/Main.cs:53
  at (wrapper runtime-invoke) <Module>.runtime_invoke_void_object (object,intptr,intptr,intptr) <IL 0x00050, 0xffffffff>"
.
<Native Stacktrace>
.
=================================================================
Got a SIGSEGV while executing native code. This usually indicates
a fatal error in the mono runtime or one of the native libraries 
used by your application.
=================================================================

What i did?
I have made a UITableViewController having a single table. 
In ViewDidLoad of above controller, I am setting it source. 
In GetCell I loaded a custom cell from nib.
Buttons events of above custom cell are actually crashing the simulator 

//SettingsController.xib.cs
public partial class SettingsController : UITableViewController
{
// All Constructors
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
TableView.Source = new SettingSource();
}
class SettingSource : UITableViewSource{
String PicCell = "piccell";
public SettingSource(){
}
public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
{
UITableViewCell cell = null;
SettingsPicture pictureCell = null;
cell = tableView.DequeueReusableCell (PicCell);
if (cell == null) {
pictureCell = new SettingsPicture(PicCell);
NSBundle.MainBundle.LoadNib("SettingsPicture", pictureCell, null);
cell = pictureCell.Cell;
cell.Tag = Environment.TickCount;
}
return cell;
}
}
}

//SettingsPicture.xib.cs
public partial class SettingsPicture : UITableViewCell
{
public SettingsPicture (IntPtr handle) : base(handle)
{
Initialize ();
}

[Export("initWithCoder:")]
public SettingsPicture (NSCoder coder) : base(coder)
{
Initialize ();
}

public SettingsPicture (string strIdentfier) : base(UITableViewCellStyle.Default, strIdentfier)
{
Initialize ();
}

void Initialize ()
{
AttachEvents();
}

public UITableViewCell Cell {
get {
return cell; 
}
}

public void AttachEvents (){
//btnTakePhoto.and btnSelectPhoto are outlets defined against buttons in sib file
btnTakePhoto.TouchUpInside += this.HandleBtnTakePhotoTouchUpInside;
btnSelectPhoto.TouchUpInside += this.HandleBtnSelectPhotoTouchUpInside;
}
void HandleBtnTakePhotoTouchUpInside (object sender, EventArgs e)
{
Console.WriteLine("HandleBtnTakePhotoTouchUpInside");
}

void HandleBtnSelectPhotoTouchUpInside (object sender, EventArgs e)
{
Console.WriteLine("HandleBtnSelectPhotoTouchUpInside");
}
}


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

Re: Button events of custom UITableViewCell are crashing simulator

Nic Wise
From memory this is your problem:

cell = tableView.DequeueReusableCell (PicCell);
                        if (cell == null) {
                                pictureCell = new SettingsPicture(PicCell);
                                NSBundle.MainBundle.LoadNib("SettingsPicture", pictureCell, null);
                                cell = pictureCell.Cell;
                                cell.Tag = Environment.TickCount;
                                }
                        return cell;

you make a new pictureCell, and assign the value to cell. But then the
GC gets a little too agressive and GC's pictureCell, which kills
pictureCell.Cell and takes the cell out with it.

I think. From memory. Most of the examples I've seen around keep a
list of the parent controller (pictureCell) at the tableview level, so
they dont go out of scope....

It's BAD, 'cos I'm not sure when it would free up, so you are likely
to leak memory left and right, but try this:

cell = tableView.DequeueReusableCell (PicCell);
                        if (cell == null) {
                                pictureCell = new SettingsPicture(PicCell);
                                NSBundle.MainBundle.LoadNib("SettingsPicture", pictureCell, null);
                                cell = pictureCell.Cell;
                                cell.Tag = Environment.TickCount;
    cellControllerList.Add(pictureCell);
                                }
                        return cell;

and :

class SettingSource : UITableViewSource{
                String PicCell = "piccell";

private List<SettingsPicture> cellControllerList = new List<SettingsPicture>();
 
                public SettingSource(){
                }
               
See if that fixes it :) If it does, maybe use a dictionary based on
the tag, not a list, so you don't re-make the controller each time.

On Wed, May 4, 2011 at 15:13, Yawar Khan <[hidden email]> wrote:

> I have been working on monotouch for almost two months. I am facing
> a weird problem.
> I had used "UITableViewCell" to make customized cell view and it is working
> perfectly in cases of pictures and labels enclosed.
> Then I added a button in custom cell with attached event.
> It is appearing properly but when I clicked on those buttons it crashed the
> simulator, with following error
> "Stacktrace:
>   at (wrapper managed-to-native)
> MonoTouch.UIKit.UIApplication.UIApplicationMain (int,string[],intptr,intptr)
> <IL 0x0009f, 0xffffffff>
>   at MonoTouch.UIKit.UIApplication.Main (string[],string,string) [0x00038]
> in /Users/plasma/Source/iphone/monotouch/UIKit/UIApplication.cs:26
>   at MonoTouch.UIKit.UIApplication.Main (string[]) [0x00000] in
> /Users/plasma/Source/iphone/monotouch/UIKit/UIApplication.cs:31
>   at MezziPhone.Application.Main (string[]) [0x00063] in
> /Users/folio3/Desktop/iPhone/Main.cs:53
>   at (wrapper runtime-invoke) <Module>.runtime_invoke_void_object
> (object,intptr,intptr,intptr) <IL 0x00050, 0xffffffff>"
> .
> <Native Stacktrace>
> .
> =================================================================
> Got a SIGSEGV while executing native code. This usually indicates
> a fatal error in the mono runtime or one of the native libraries
> used by your application.
> =================================================================
> What i did?
> I have made a UITableViewController having a single table.
> In ViewDidLoad of above controller, I am setting it source.
> In GetCell I loaded a custom cell from nib.
> Buttons events of above custom cell are actually crashing the simulator
> //SettingsController.xib.cs
> public partial class SettingsController : UITableViewController
> {
> // All Constructors
> public override void ViewDidLoad ()
> {
> base.ViewDidLoad ();
> TableView.Source = new SettingSource();
> }
> class SettingSource : UITableViewSource{
> String PicCell = "piccell";
> public SettingSource(){
> }
> public override UITableViewCell GetCell (UITableView tableView, NSIndexPath
> indexPath)
> {
> UITableViewCell cell = null;
> SettingsPicture pictureCell = null;
> cell = tableView.DequeueReusableCell (PicCell);
> if (cell == null) {
> pictureCell = new SettingsPicture(PicCell);
> NSBundle.MainBundle.LoadNib("SettingsPicture", pictureCell, null);
> cell = pictureCell.Cell;
> cell.Tag = Environment.TickCount;
> }
> return cell;
> }
> }
> }
> //SettingsPicture.xib.cs
> public partial class SettingsPicture : UITableViewCell
> {
> public SettingsPicture (IntPtr handle) : base(handle)
> {
> Initialize ();
> }
> [Export("initWithCoder:")]
> public SettingsPicture (NSCoder coder) : base(coder)
> {
> Initialize ();
> }
> public SettingsPicture (string strIdentfier) :
> base(UITableViewCellStyle.Default, strIdentfier)
> {
> Initialize ();
> }
> void Initialize ()
> {
> AttachEvents();
> }
> public UITableViewCell Cell {
> get {
> return cell;
> }
> }
> public void AttachEvents (){
> //btnTakePhoto.and btnSelectPhoto are outlets defined against buttons in sib
> file
> btnTakePhoto.TouchUpInside += this.HandleBtnTakePhotoTouchUpInside;
> btnSelectPhoto.TouchUpInside += this.HandleBtnSelectPhotoTouchUpInside;
> }
> void HandleBtnTakePhotoTouchUpInside (object sender, EventArgs e)
> {
> Console.WriteLine("HandleBtnTakePhotoTouchUpInside");
> }
> void HandleBtnSelectPhotoTouchUpInside (object sender, EventArgs e)
> {
> Console.WriteLine("HandleBtnSelectPhotoTouchUpInside");
> }
> }
>
> _______________________________________________
> MonoTouch mailing list
> [hidden email]
> http://lists.ximian.com/mailman/listinfo/monotouch
>
>



--
Nic Wise
t.  +44 7788 592 806 | @fastchicken | http://www.linkedin.com/in/nicwise
b. http://www.fastchicken.co.nz/ | http://www.flickr.com/photos/nicwise

mobileAgent (for FreeAgent): get your accounts in your pocket.
http://goo.gl/IuBU
Trip Wallet: Keep track of your budget on the go: http://goo.gl/ePhKa
London Bike App: Find the nearest Boris Bike, and get riding! http://goo.gl/Icp2
_______________________________________________
MonoTouch mailing list
[hidden email]
http://lists.ximian.com/mailman/listinfo/monotouch