Really weird "[UIAccessibilityBundle setWhatever:]: unrecognized selector sent to instance 0xnnnnnnn" - iphone

So, I'm puzzled by a really weird crash occurring in my app.
Background: I have a class CustomButton overriding UIButton. Briefly, the interface looks like:
#interface CustomButton : UIButton
#property (nonatomic, getter = isLoading) BOOL loading;
#end
Implementation:
#implementation CustomButton
#synthesize loading = loading_;
- (id)init {
if ((self = [UIButton buttonWithType:UIButtonTypeCustom])) {
[self setTitle:NSLocalizedString(#"My Button", nil) forState:UIControlStateNormal];
[[self titleLabel] setShadowColor:[UIColor blackColor]];
[[self titleLabel] setShadowOffset:CGSizeMake(0, -1)];
[self setContentEdgeInsets:UIEdgeInsetsMake(0, 77, 0, 30)];
UIImage *backgroundImage = [[UIImage imageNamed:#"back.png"] stretchableImageWithLeftCapWidth:77 topCapHeight:0];
UIImage *highlightedBackgroundImage = [[UIImage imageNamed:#"back_highlighted.png"] stretchableImageWithLeftCapWidth:77 topCapHeight:0];
[self setBackgroundImage:backgroundImage forState:UIControlStateNormal];
[self setBackgroundImage:highlightedBackgroundImage forState:UIControlStateHighlighted];
[self sizeToFit];
}
return self;
}
- (void)setLoading:(BOOL)loading {
if (loading_ != loading) {
// Do fancy things
}
}
#end
Problem: I'm trying to use the button in a class, so I have property and so on:
...
#property(nonatomic, strong) CustomButton *customButton;
...
#syntesize customButton = customButton_;
...
- (void)aMethod {
self.customButton.loading = YES;
}
...
The button, of course, is initialized.
Then, upon executing the statement self.bookButton.loading = YES, I get this error
2012-08-28 12:54:25.091 MyApp[9465:15803] -[UIAccessibilityBundle
setLoading:]: unrecognized selector sent to instance 0x8650cc0
2012-08-28 12:54:25.091 MyApp[9465:15803] *** Terminating app due
to uncaught exception 'NSInvalidArgumentException', reason:
'-[UIAccessibilityBundle setLoading:]: unrecognized selector sent to
instance 0x8650cc0'
*** First throw call stack: (0x1ead022 0x203ecd6 0x1eaecbd 0x1e13ed0 0x1e13cb2 0x26bf5 0x257ce 0xfa938f 0xfa96a4 0xfa99a7 0xfb8aea
0x11600be 0x11603c7 0x1160436 0xf10e49 0xf10f34 0xc5bcaac 0xe04b54
0x272e509 0x1de4803 0x1de3d84 0x1de3c9b 0x27887d8 0x278888a 0xee0626
0x14cce 0x27b5 0x1) terminate called throwing an exception
I honestly have no idea about what to look for. It's either something really really stupid, or something really subdle.
I've been searching on google/stackoverflow already of course, but I didn't manage to get an answer.
Usually that error occurs, as far as I know, when the object doesn't "understand" the sent message, but this shouldn't be the case. I don't even get a warning from the compiler.
If you have even just an idea of what I could look for or you want more details, let me know, thanks.
EDIT: forgot to specify that I am using ARC.
UPDATE: In the answers below Phillip Mills said that it might have been a premature memory release issue. It's a good point, but Instrument didn't find any zombie. Moreover, I've put a log right before the crashy statement, where I try print information about the CustomButton object and few things related to a UIButton, like the title. Everything looks ok. The title is the one I've set, the frame is there and so on... so I believe the object (an actual UIButton) exists and it's also the expected one. The only thing is the "loading" property that messes everything up.
SOLUTION: the init method was the source of the problem. Instead of initializing an instance of the subclass of the button, it was initializing an instance of UIButton. So, conequentely, no "loading" properties could be found. Substituting init with initWithFrame and using the standard procedure to override the initializer, everything works great.
Still a mistery, though, the reason why I got UIAccessibilityBundle as the origin of the error and not a UIButton.

Are you casting anything to (CustomButton *) in your app? Double-check the casts - you might be incorrectly casting something that's not a CustomButton and storing the result in the customButton property.

The button is almost certainly being released prematurely and having its address re-used for a UIAccessibilityBundle object. Try turning on zombies (or run Instruments to look for zombies) and you should see better information if the memory management problem isn't obvious.

Try
#interface CustomButton : UIButton
#property (nonatomic, assign) BOOL loading;
#end
and
#implementation CustomButton
#synthesize loading = _loading;
- (id)init {
....
}
- (void)setLoading:(BOOL)loading {
if (self.loading != loading) {
// Do fancy things
}
}
#end
For all other instances of loading, use self.loading.
Without seeing the other parts of your code, this is my best guess.

Related

objective-c: Delegate object argument getting overwritten when i create multiple instances of custom class

EDIT: I apologize for wasting time, the erorr had nothing to do with what I'm taking about but rather some logic in my code that made me believe this was the cause. I'm awarding Kevin with the correct answer since using his idea to pass the whole AuthorSelectionView, and his note on correcting the NSNumer mistake. Sorry about that.
I've been trying to figure this out for hours, and even left it alone for a day, and still can not figure it out...
My situation is as follows:
I've created a custom class that implements 'UIView' and made this class into a protocol as follows:
custom UIView h file
#protocol AuthorSelectionViewDelegate <NSObject>
-(void)AuthorSelected:(NSNumber *)sender;
#end
#import <UIKit/UIKit.h>
#interface AuthorSelectionView : UIView
#property (nonatomic,assign) id<AuthorSelectionViewDelegate> delegate;
#property (strong,retain) NSNumber *authorID;
- (id)initWithFrame:(CGRect)frame withImage:(UIImage *)img withLabel:(NSString *)lbl withID:(int)authorID ;
#end
the implementation...
- (id)initWithFrame:(CGRect)frame withImage:(UIImage *)img withLabel:(NSString *)lbl withID:(int)authorID
{
self = [super initWithFrame:frame];
if (self) {
self.authorID = [[NSNumber alloc] initWithInt:authorID]; //used to distinguish multiple instances of this class in a view.
...
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, FRAMEWIDTH, FRAMEHEIGHT)];
[button addTarget:self action:#selector(CUSTOMBUTTONCLICK) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:button];
}
return self;
}
- (void) CUSTOMBUTTONCLICK
{
[self.delegate performSelector:#selector(AuthorSelected:) withObject:self.authorID];
}
Now the method in my delegate object gets called just fine, but my major problem here is that something is going on with the object being pass through when i have multiple instances of the AuthorSelected class alloc'd.. (the NSNumber authorID). I'm getting some weird behavior with it. It seems almost random with the value being passed, but i'm detecting some pattern where the value passed through is coming up late..
thats confusing so ill try to explain:
I create two instances of the AuthorSelected view, one with authorID=1 and the other with authorID=2.
On the first press, lets say i press the first button, i'll get 1 as expected.
On the second press, if I press the 1st custom button, i'll get '1', but if i press the second i'll still get 1.
On the third go, either button will give me back '2'
I feel like this is some issue with pointers since that has always been a weak point for me, but any help would be greatly appreciated as I can not seem to figure this one out.
Thank you!
EDIT:
as requested here is how I create the AuthorSelectionView Objects...
AuthorSelectionView * asView01 = [[AuthorSelectionView alloc]
initWithFrame:CGRectMake(0, 0, FRAMEWIDTH, FRAMEHEIGHT)
withImage:userPic1
withLabel:randomUserName
withID:1];
asView01.delegate = self;
AuthorSelectionView * asView02 = [[AuthorSelectionView alloc]
initWithFrame:CGRectMake(0, 0, FRAMEWIDTH, FRAMEHEIGHT)
withImage:userPic2
withLabel:randomUserName2
withID:2];
asView02.delegate = self;
A detail that may be important:
As soon as i click on one of these custom views, my code is set to (for now) call the method that runs the above AuthorSelectionView alloc code, so that i can refresh the screen with the same layout, but with different userpic/userName. This is poor design, I know, but for now I just want the basic features to work, and will then worry about redrawing. I metion this tidbit, becuase I understand that objective-c 'layers' veiws on top of eachother like paint on a canvas, and had a thought that maybe when I click what I think may be my 2nd button, its really 'clicking' the layer beneath and pulling incorrect info.
Your description of the problem is a bit confusing, but this line in your init is very clearly wrong:
self.authorID = [self.authorID initWithInt:authorID];
In -init, your property self.authorID defaults to nil, so the expression [self.authorID initWithInt:authorID] is equivalent to [nil initWithInt:authorID], which evaluates back to nil. So you should actually be seeing nil in your action. You probably meant to say self.authorID = [NSNumber numberWithInt:authorID]
You're missing the alloc message, so this message:
self.authorID = [self.authorID initWithInt:authorID];
Is sent to a nil target, because self.authorID hasn't been allocated yet.
So first allocate it, then use the init method, or mix these two messages. A faster syntax allows to do it this way:
self.authorID= #(authorID);
EDIT
I don't see where you initialize the delegate, that method shouldn't even be called if you haven't initialized it. Show the code where you create the AuthorSelectionView objects and set the delegates.
instead of :
self.authorID = [self.authorID initWithInt:authorID];
put :
self.authorID = [NSNumber numberWithInt:authorID];
or
self.authorID = [[NSNumber alloc] initWithInt:authorID];
EDIT :
Don't you have errors or warnings in your code ? I can't see you returning self object in the init method ("return self;")

Objective c: setting an object attribute ; attribute not updating

Wow. I have had a total mental failure this morning stuck on this 101 problem.
In ViewController, I have this code. But after it executes, the value of [proposalInfo expanded] is still NO. Can somebody see what I'm doing wrong?
- (void)showFullProposal:(id) sender {
// update proposalinfo
ProposalInfo *proposalInfo = [self.proposalInfoArray objectAtIndex:index.section];
[proposalInfo setExpanded:YES];
}
The variables are declared as follows:
ViewController:
#interface ViewController()
#property (nonatomic, strong) NSMutableArray* proposalInfoArray;
#end
ProposalInfo.h:
#interface ProposalInfo : NSObject
#property (assign) BOOL expanded;
#end
ProposalInfo.m:
#synthesize expanded;
Please help!!
If you never alloc/init your proposalInfoArray array, you could experience behavior like this (i.e. get no error, but always get NO back because when you send a message to a nil object, you get nil back). If not precisely this, it's going to be something simple like that. Check proposalInfoArray and make sure it's not nil. Also check the proposalInfo object you got back, make sure it's not nil.
To illustrate your likely problem, this reproduces the behavior you describe (e.g. expanded looks like it's NO, regardless, but you still don't get any exception):
self.proposalInfoArray = nil; // This obviously won't work
[self.proposalInfoArray addObject:[[ProposalInfo alloc] init]];
ProposalInfo *proposalInfo = [self.proposalInfoArray objectAtIndex:0];
NSLog(#"before=%d", proposalInfo.expanded); // OK, IT'S "0"
proposalInfo.expanded = YES;
NSLog(#"after=%d", proposalInfo.expanded); // HEY, IT'S STILL "0" -- BAD!
Whereas this works properly:
self.proposalInfoArray = [[NSMutableArray alloc] init];
[self.proposalInfoArray addObject:[[ProposalInfo alloc] init]];
ProposalInfo * proposalInfo = [self.proposalInfoArray objectAtIndex:0];
NSLog(#"before=%d", proposalInfo.expanded); // OK, IT'S "0"
proposalInfo.expanded = YES;
NSLog(#"after=%d", proposalInfo.expanded); // HEY, IT'S NOW "1" -- GOOD!
In terms of how to identify these issues in the future, use NSAssert. We would have found this problem if we had the following line of code before the objectAtIndex line:
NSAssert(self.proposalInfoArray, #"proposalInfoArray must be initialized!");
or, after the objectForIndex:
NSAssert(proposalInfo, #"proposalInfo must not be nil!");
The nice thing about NSAssert statements is that you can put them in your code, and when you build for debugging, they help you find your program logic mistakes, but when you build your final release version, they're automatically omitted, making your code more efficient. So, use NSAssert liberally!
Imme, the following line seems to be strange:
ProposalInfo *proposalInfo = [self.proposalInfoArray objectAtIndex:index.section];
Actually, what do you have in your array, means in proposalInfoArray.
Have you checked your object?

UILabel category and setFont: crash

I created UILabel category which would do extra work when font property is changed.
I have chosen category over sublassing so I do not have to change class for all labels in my all XIB files. I just add this category declaration to the prefix header and category is visible in the whole project scope.
Implementation file:
//
// UILabel+XIBCustomFonts.m
#import "UILabel+XIBCustomFonts.h"
#implementation UILabel (XIBCustomFonts)
BOOL comes_from_nib = NO;
-(id)initWithCoder:(NSCoder *)aDecoder_{
self = [super initWithCoder:aDecoder_];
if (self) {
comes_from_nib = YES;
}
return self;
}
-(void)setFont:(UIFont *)font_{
[super setFont:font_];
NSLog(#"Setting from for label from XIB for name:%# size: %.1f - do font theme replacement if needed", self.font.fontName, self.font.pointSize);
}
#end
The surprising thing is the crash with following log:
-[UIButtonLabel setFont:]: unrecognized selector sent to instance 0x9ad1b70
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIButtonLabel setFont:]: unrecognized selector sent to instance 0x9ad1b70'
Where did UIButtonLabel came form?
It happens even if I do extra Class checking in setFont: setter:
if ([self class] != [UILabel class] || !comes_from_nib) {
[super setFont:font_];
return;
}
Is there any way to override setFont: setter in UILabel without subclassing?
You cannot call super methods in categories! Never!
This is not possible when you use a category to augment a class' functionality, you are not extending the class, you are actually wholly overriding the existing method, you lose the original method completely. That's why your error appears.

Static Analyzer showing wrong leak?? (XCode 4.0, iOS 4.3 and above)

Happy November to all,
Well I tried Xcode Build and analyze on my project, and it showed some unusual leaks, which I couldn't quite accept with my knowledge of Objective C.
So I decided to put up a test project and ask here..
MemoryTestController.h
#interface MemoryTestController : UIViewController{
UIImageView *tstImageView;
}
#property(nonatomic,retain) UIImageView *tstImageView;
#end
MemoryTestController.m
#implementation MemoryTestController
#synthesize tstImageView;
- (void)viewDidLoad{
[super viewDidLoad];
self.tstImageView = [[UIImageView alloc] //<==This object is leaking
initWithFrame:<SomeFrame>];
self.tstImageView.image = [UIImage imageNamed:#"SomeImage.png"];
[self.view addSubview:tstImageView];
[tstImageView release];
}
-(void)dealloc{
[tstImageView release];
[super dealloc];
}
#end
When I try Build and analyze, clang static analyzer say
Potential leak of an object at line xx
And the culprit line is
self.tstImageView = [[UIImageView alloc]initWithFrame:<SomeFrame>];
I think I am releasing once for every time I am allocing/retaining. Am I missing something, or Static analyzer has some bugs?
EDIT : Is there any leak there?
Well I run the above project using Leak tool in instrument..It didn't show any leak even though I tried many times..Whom should I believe? Static analyzer or Leak instrument?
your problem is how you release it:
- (void)viewDidLoad{
[super viewDidLoad];
self.tstImageView = [[UIImageView alloc] //<==This object is leaking
initWithFrame:<SomeFrame>];
self.tstImageView.image = [UIImage imageNamed:#"SomeImage.png"];
[self.view addSubview:tstImageView];
[tstImageView release]; // << here
}
you should do it this way:
- (void)viewDidLoad{
[super viewDidLoad];
UIImageView * imageView = [[UIImageView alloc] initWithFrame:<SomeFrame>];
imageView.image = [UIImage imageNamed:#"SomeImage.png"];
self.tstImageView = imageView;
[imageView release];
[self.view addSubview:self.tstImageView];
}
The checker is correct because it cannot assume that the variable is identical to the one you set. Therefore, the form you use in the OP could introduce a reference count imbalance because the ivar's value may not be what you assigned to it by the time you message release upon the ivar.
These cases are not likely for a UIImageView, and quite unlikely in the context of your program, but these examples should give you an idea as to why the checker assumes that object->ivar associations shall not be trusted:
Between creation of the image view and the message to release it via the ivar, you have:
self.tstImageView = [[UIImageView alloc] initWithFrame:<SomeFrame>];
self.tstImageView.image = [UIImage imageNamed:#"SomeImage.png"];
[self.view addSubview:tstImageView];
1) assignment of the image view via the setter
2) access of the image view via the getter
3) direct access of the ivar, when adding to self.view
the setter may have taken a copied or used a cached value. UIImageView is a bad example, but the checker does not know how types are generally passed around - even if it did, it would (at times) make unsafe assumptions.
the simplest example would be:
- (void)setName:(NSString *)inName {
NSString * prev = name;
if (inName == prev) return;
if (0 == [inName count]) name = #"";
else name = [inName copy];
[prev release];
}
the value held by the ivar could change in the meantime. not likely an issue in this case, but let's say that adding the image view as the subview could end up calling back and altering self in the process/effect of adding the subview, and replacing or removing the image view you passed. In that case, the variable view you passed would leak and the view it replaced it with would have a negative imbalance.
Neither of those are likely to happen in your example, but it does happen in real world programs, and the checker is correctly evaluating based on locality, not property (the checker can't assume much of what happens inside a method call). It also encourages one good idiomatic style in this case.
EDIT : Is there any leak there?
Well I run the above project using
Leak tool in instrument..It didn't shown any leak even though I tried
it many times..Whom should I believe? Static analyzer or Leak
instrument?
The static analyzer says there is a potential leak because it is unable to guarantee the reference/allocation it follows is correctly retained/released. You can guarantee that reference counting is correct and please the static analyzer by changing you program to look like I wrote it in my example.
The way you have written it has made it impossible for the analyzer to follow the reference.
If you have no leaks and no zombies, then there is not a leak. But the solution is easy to fix - and programs have a way of changing during development. It's much easier to use the form I posted so it is easier for the toolset and for you to verify the program is correct. The static analyzer is not always correct, but you should adjust your programs to please it because static analysis is very useful. The program I posted is also easier for a human to understand and confirm that it is correct.
when you declare a property with retain like this
#property(nonatomic,retain) UIImageView *tstImageView;
a setter is added that will incr the retainCount when you assign to the property. When you do as below the object you created has already a retainCount == 1
self.tstImageView = [[UIImageView alloc]
initWithFrame:<SomeFrame>];
so the tstImageView object has 2 in retainCount.
do instead
UIImageView* view = [[UIImageView alloc] initWithFrame:<SomeFrame>];
self.tstImageView = view;
[view release];
then, although unrelated to your leak when you release it write like this instead
self.tstImageView = nil;
since the setter will then will properly set the retainCount

iPhone Dev = maps and deselecting annotations

I am successfully drawing annotations on a map using an array of annotations. I can even click on the annotation and change it’s colour or image. My problem arises when the use selects the second annotation and I want to dynamically change the colour or image of the first one back to a non-selected colour/image. I can get the array of all the annotations and work through the array but once I try to set the colour or image ot the array I get a similar error.
for (MKAnnotationView *ann in map.selectedAnnotations){
if ([ann isMemberOfClass:[Place class]]) {
place = (Place *)ann;
if (currentPlaceID != place.placeID) {
UIImage *i = [UIImage imageNamed:#"pin.png"];
ann.image = i;
}
}
the above code works ok until I get to ann.image = i; then it errors. The errors I get are:-
-[Place setImage:]: unrecognized selector sent to instance 0x4514370 Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: '** -[Place setImage:]:
unrecognized selector sent to instance 0x4514370'
Please advise as I have been going around in circles on this one for 2 days now!!!!
Any ideas on how best to do this?
thanks in advance
Do you have a property on the class Place called image?
Something like... #property (nonatomic, retain) UIImage* image; and is it properly synthesized? #synthesize image;?
The error is pretty straight forward, some object is receiving a message that it doesn't respond to, namely 'setImage' which is invoked by the .image.
Here is your code:
1. for (MKAnnotationView *ann in map.selectedAnnotations) {
2. if ([ann isMemberOfClass:[Place class]]) {
3. place = (Place *)ann;
4. if (currentPlaceID != place.placeID) {
5. UIImage *i = [UIImage imageNamed:#"pin.png"];
6. ann.image = i;
7. }
8. }
9. }
What I can see:
ann is an MKAnnotationView (from map.selectedAnnotations)
you are typecasting your annotation to a place on line 3 (is this right? Does Place subclass MKAnnotationView?)
you are properly setting the image to the annotation
What this means:
If Place is indeed a subclass of MKAnnotationView, you hid the setImage (somehow) method
If Place is NOT a subclass of MKAnnotationView, you've added an invalid annotation to the annotations (sure) that you're trying to treat as an annotation.
I finally figured out how to do this. As usual it's not that hard once you know how. Just thought I would pass this on.
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
NSLog(#"here I am in set selected");
if (YES == selected)
{
NSLog(#"I am selected");
}
else
{
self.backgroundColor = [UIColor clearColor];
NSLog(#"not selected");
}
}