BAD_ACCESS & NSInvalidArgumentException in working with UIPickerView - ios

I'm beginner in working with UIKit. When I tired to work with UIPickerView in iOS 7 SDK I faced to a hard-to-solve problem.
We know that UIPickerView needs two resources to work perfectly: dataSource & delegate. So I wrote a class named "KMPickerProtocols". I adopted it to UIPickerViewDataSource & UIPickerViewDelegate Protocols and then I added some extra setter methods to it.
KMPickerProtocols is adopted to all of essential methods of dataSource and delegate Protocols + the necessary optional method for setting title of each row (pickerView:titleForRow:forComponent). all these methods get things done without any problem (in normal situations).
Finally I set the delegate and dataSource properties of my UIPickerView (named _accountPicker) manually with this piece of code:
NSArray *delegateAgent = [[KMTwitterDelegate new] run ];
_accountPicker.dataSource = [delegateAgent objectAtIndex:0];
_accountPicker.delegate = [delegateAgent objectAtIndex:1] ;
(the run method is designed to set some properties including height of each row and ...)
Now, when I run my app it will show my twitterAccounts stored in system(accounts framework) in the form of UIPickerView. but there is a problem: As soon as I scroll the picker view or tap on any row, the program will crash and I get BAD_ACCESS (code=2 , address = 0x1) in this line of code:
#autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([KMAppDelegate class]));
}
and also there are sometimes that I get signal SIGABRT in above line. in these situations Log Says:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSMallocBlock pickerView:titleForRow:forComponent:]: unrecognized selector sent to instance 0x8c75ad0'
I will appreciate if you help me to deal with this problem. I really don't know where the bug is. but I guess this screen shots will be helpful to find that:
The problem in above screenshot is that UIPicker called the "pickerView:titleForRow:forComponent" method for row 0, three times instead of one time. I just don't know whether that is this because myFault or because the typical behaviour of UIPicker.

You are sending the message pickerView:titleForRow:forComponent: to an object of type NSMallocBlock. This is a message automatically sent from the picker to its datasource, so my assumption is that you are assigning the datasource:
_accountPicker.dataSource = [delegateAgent objectAtIndex:0];
The datasource you assign does not conform the protocol UIPickerViewDataSource
Checkout what returns [delegateAgent objectAtIndex:0]; and make sure it is what you expect it to be.

Related

PFObject as MKAnnotation causes NSInternalInconsistencyException

I have a subclass of PFObject that conforms to MKAnnotation, which is working fine for adding annotations to a map view, and starting to drag annotations, but as soon as I stop dragging, the app crashes with the following exception:
NSInternalInconsistencyException', reason: 'Getter should take no arguments!
I've tracked the crash down to [PFObject forwardInvocation], which leads to [PFObjectSubclassingController _forwardGetterInvocation:forProperty:withObject:], but I can't figure out what's calling the forwardInvocation.
Anyone have any ideas?
Problem found... I needed to declare a setCoordinate:(CLLocationCoordinate2D)newCoordinate method in the custom class. Whoops.

[Not A Type release]: message sent to deallocated instance in UITableView

I get a crash when quickly scrolling through a UITableView.
The crash is :
*** -[Not A Type release]: message sent to deallocated instance 0x1aded1c0
And the exception breakpoint indicates this as the source:
- (void)updateInfo:(Item*)item{
if (!item) {
return;
}
Program *prg = (Program *)item;
self.titleLabel.text = prg.title; // crash here
self.descriptionLabel.text = prg.item_description;
}
This method is inside the custom table view cell subclass and it is called when data from the internet is ready to be displayed in the cell.
I have never seen this kind of crash before...
What is the best way to fix it?
Reason:
As you scroll the tableview, the cells on top get released and when data comes from internet for those cell, they have already been released. So, change your logic. You generally receive this error when you try to access and update an object which has already been released. As you are saying that this method is inside your custom cell, so it clearly indicates that the cell is already released. That's why your app gets crashed when you try to update any view of that cell.
Solution:
Either you can reload your whole tableview or reload those specific cells when you receive data.
Where are you calling the method "updateInfo"? You should call this in cellForRowAtIndexPath. You should not store a reference to a cell and then call this.
When you get the data from your service you just call self.tableView reloadData (or perhaps reloadRowsAtIndexPaths if you know which row needs update and dont want a performance hit)

iOS - Getting a weird error: unrecognized selector sent to instance on a UITableView

Introduction
In my current application I have a UITableView which holds custom cell objects. The custom UIViewCellObjects are simply subclassed from the standard UITableViewCell class. The custom cells holds information about running background uploads, and updates them with things like percentage done and so on.
The custom cell objects listens to NSNotifications from upload processes running in the background, and when they get a relevant notification, they simply update their own view controls with the new information (such as upload percentage).
Now when an upload process is done, I re-order the array of active upload objects and reload the tableview like this:
-(void) uploadFinished: (NSNotification*)notification
{
NSDictionary *userInfo = [notification userInfo];
NSNumber *uploadID = [userInfo valueForKey:#"uploadID"];
if (uploadID.integerValue == uploadActivity.uploadID)
{
[[ApplicationActivities getSharedActivities] markUploadAsFinished:uploadActivity];
[parentTable reloadData];
[self setUploadComplete];
}
}
Now this method takes place in the tableviewcell objects, and as you can see they call their owning UITableView to reload the data right after the array is sorted. The markUploadAsFinished method simply re-orders the array so any newly finished upload is put at the top, so it will appear this way in the UITableView.
The Problem
Now the problem I'm having is that when this method is called, I sometimes get the following error:
'NSInvalidArgumentException', reason: '-[CALayer tableView:numberOfRowsInSection:]: unrecognized selector sent to instance
I do not get it all the time, sometimes the entire process runs fine and finished uploads appear in the start of the UItableview, and at other seemingly random times it fails. I don't really have a clue what's going on here.
The custom cells are loaded from a .NIB file like this:
UploadCell *cell = [activeUploadsTable dequeueReusableCellWithIdentifier:#"UploadProgressCell"];
if (cell == nil)
{
[[NSBundle mainBundle] loadNibNamed:#"UploadCellView" owner:self options:nil];
cell = customCell;
}
Is there anyone who might have a clue about what's going on here?
EDIT
First of all, I have tracked down this error to appear right at the line where:
reloadData
is called inside of the custom cell objects.
Furthermore, it seems that the instance it sends methods to can change. I just got this error too:
'NSInvalidArgumentException', reason: '-[UIScrollViewPanGestureRecognizer tableView:numberOfRowsInSection:]: unrecognized selector sent to instance
I really have no idea what's going on here.
'-[CALayer tableView:numberOfRowsInSection:]: unrecognized selector sent to instance
You've got a bad pointer. It looks like your table's data source is being released while the table still exists. The table doesn't retain its data source because that could create a retain cycle. If you don't take care to keep the data source around while the table is using it, the table will can up with a pointer to an object that no longer exists. In this case, it looks like a CALayer object is subsequently being created at the same address. When the table later sends its "data source" a message to get the number of rows, that message is delivered to the layer, which (obviously) doesn't have a -tableView:numberOfRowsInSection: method, and the error results.
According to me you running the upload process method in background i think other thread than main thread. And according to my knowledge when you deal with UIKIT objects you have ruin on main thread.
But these problem not occur every time because some time you switch to other thread to main thread so its working fine and some time not. so that problem occur

Popovers in UITableView

I would like to display popovers with UITableView's as content (this works) on some button presses and then get the selected item as string as buttontitle or some textview text. I've found a few example on how to do this with protocols but still get an error.
My code:
In popoverViewController.h
#protocol popoverViewControllerDelegate <NSObject>
-(void)getRowText:(NSString *)string;
#end
I declare an id delegate2 variable and set its property to:
#property(nonatomic,assign) id<popoverViewControllerDelegate> delegate2;
In the popoverViewController.m file I synthesize the variable, and in didSelectRowAtIndexPath method I have this, and this line seems to cause the error I`m having:
[self.delegate2 getRowText:[someArray objectAtIndex:indexPath.row];
In mainViewController.m I add the popoverViewControllerDelegate to the ViewControllers protocol and have its header file imported. And then have some code in the -(void)getRowText: method which doesnt get called.
UIPopovers and such are set up as they work as needed, problem arises when I press a row in the tableview. I get the
Terminating app due to uncaught
exception
'NSInvalidArgumentException', reason:
'* -[UIPopoverViewController
getRowText:]: unrecognized selector
sent to instance 0x57ca80'
Could anyone give some advice with this?
Finally found the error and cant believe how silly I/it was.
I had a viewController.delegate2 = self. line with period instead of a semicolon, I wonder why it compiled tho.

Writing an own iPad photogallery - dealloc error

i want to write my own photogallery like the original "Photos.app" from apple.
I´ve created a UITabbarcontroller in the AppDelegate and then an "ImageViewController" and a "VideoViewController".
In the "ImageViewController" i´ve added an UIScrollView and then made an instance of my own "PhotoGallery" with different properties like imagePerRow, images, paddings etc.
For the "PhotoGallery" i´ve created a new objective-c class as a subclass of "NSObject", where i´m positioning all the different images as UIButtons.
Then i´ve added another function which describes the arrangement for all the images when the device orientation has changed. And the dealloc-function. Thats all.
This class works great, also the rearrangement when the device orientation has changed. The problem is, if i simulate a memory warning in the ios-simulator, the first time the PhotoGallery gets correctly dealloc but if i simulate a warning again, i get a error-message: "[PhotoGallery release]: message sent to deallocated instance ".
I thought its because of the subclass as NSObject, right?
Then i´ve tested it as a UIView. With the same error. So know i don´t know what to do anymore. Hope you understand what´s the problem and you would give me some hints on that..
Think about calling the init-function again? How? Need "drawRect"? I´ve no idea.
Thanks for your time and help,
G.
You're probably not setting the property which holds a reference to the PhotoGallery to nil.
ie. You're keeping a reference to a deallocated instance, and attempting to call release on it.
bad example:
- (void) didReceiveMemoryWarning
{
[photoGallery release];
}
safe(r) example:
- (void) didReceiveMemoryWarning
{
[photoGallery release];
photoGallery = nil;
// or combine both actions if your property attributes are set up to accommodate it:
// self.photoGallery = nil;
}
In the bad example, photoGallery still holds a reference to a now-deallocated instance, and the second memory warning will attempt to send a message to it.
In the safe(r) example, photoGallery is nil, and sending a message to nil is safe.

Resources