iOS Exception was thrown: unrecognized selector sent to instance - ios

I have a problem. I am calling a method in another class. I call it before and it works perfect but I call back in another method near the end of the class and I have this error:
-(void)methodOne:(NSString*)myString
{
mySecondClasss *second = [[mySecondClasss init] autorelease];
[second doSomething:myString];
/*
more code
*/
}
-(void)methodTwo:(NSString*)myString
{
mySecondClasss *second = [[mySecondClasss init] autorelease];
[second doSomething:myString];
/*
more code
*/
}
In the second I'm getting this error:
Exception was thrown: -[mySecondClasss doSomething:]: unrecognized selector sent to instance. I don't understand why works once but not the second time. Any of you can give me some pointers of how can I fix this?
I'll really appreciate your help.

Since IOS 5 you have ARC (Automatic Reference Counting) which is automatically releasing the object correctly. Anyway you are not allocating the memory for the mySecondClasss class. Not sure why it even worked in the first method.
Instead of using
mySecondClasss *second = [[mySecondClasss init] autorelease];
Try using
mySecondClasss *second = [[mySecondClasss alloc] init];

Are you sure that you are sending a string to the function? The error implies that the function is not getting a string.
For example:
[self methodOne:#"Properly formatted string"];

Related

ios application crashed with "message send to deallocated instance"

My app crashed and the code is the following:
else if(...)
{
CGDetailView *detailView = [[CGDetailView alloc] init];
ContactGroup *contactGroup = [[ContactGroup alloc] init];
[contactGroup setObjectStatus:NewObject];
[detailView setContactGroup:contactGroup];
[detailView newContactGroup:YES];
[contactGroup release];
UIBarButtonItem *temporaryItem=[[UIBarButtonItem alloc] init];
temporaryItem.title = NSLocalizedString(#"back", #"");
self.navigationItem.backBarButtonItem = temporaryItem;
[temporaryItem release];
[[self navigationController] pushViewController:detailView animated:YES];
[detailView release];
}
The error is "message sent to deallocated instance" and it regards the object of type
CGDetailView. I use the alloc-init-release pattern, and I don't really understand why the
app crashed. It usually works.
iOS 7.1 and device is iPhone5 if that helps.
Just try to get the point where your memory is released for this CGDetailView's object and you still calling a method on that object.
You can do it by enabling Zombie Object from edit scheme, it will tell you the point where your app is crashing.

How XCode knows about name of function at compile time?

I have written simple code to practice selector in Objective C, which is working fine
NSMutableArray *array = [NSMutableArray arrayWithObjects:#"A",#"B", nil];
NSLog(#"Before adding %#",array);
SEL message = #selector(addObject:); //I will change this line
if([array respondsToSelector:message])
{
[array performSelector:message withObject:#"C"];
}
NSLog(#"After adding %#",array);
But as soon as I change selector line to,
SEL message = #selector(addobject:);// Just changed name of selector
XCode starts giving warning :
Undeclared selector 'addobject:'
Now, question is how XCode knows at compile time about name of method is correct or not. Is there always list of selector generates internally for whatever object I am creating? In this case for NSMutableArray
All Xcode knows is that there is no class, either in the system code or in your program, that declares a selector called addobject. You can prove this by creating a custom class that declares an addobject method, and the warning should go away, but of course the program will crash with a unrecognized selector sent to object error message.

Nonatomatic assign and memory leak issue

I have one property say
#property(nonatomic,assign) NSString *str;
Now I am having something like
self.str = [[NSString alloc] init];
self.str = #"test";
NSLog(#"%#",str);
[self.str release];
When I run I can see a leak "Potential leak of memory".
Why its showing me leak ?
Please guide me I am leaning phase of iOS
In other words (I'm just expanding on Anoop's answer here), you have two strings, not one.
self.str = [[NSString alloc] init];
self.str = #"test";
The thing on the right side of the first line is a string: [[NSString alloc] init]. But in the second line you throw it away, replacing it with a different string, namely #"test". Now there is NO REFERENCE pointing to the first string. Thus it leaks, since it can never be released nor can anything else ever be done to or for it.
The situation of the string created in the first line is, after the second line, like the situation of "thing1" in the second panel of this diagram:
No one is pointing to it, so its memory cannot be managed, and it lives forever in isolation (leak).
self.str = [[NSString alloc] init]; //1st
One string is allocated not used.
self.str = #"test"; // 2nd
Another constant string "test" is allocated but released.
So first one is a leak.
When you use factory method or create object using alloc,new,retain,copy,mutableCopy your object has +1 retain count every time. You own object in this case. You are responsible for releasing it. So you need to release object after you finish using object which cause -1 retain count to object.
It is not necessary to alloc you string again as you are already creating its property.
And it has its getter setter + you are allocating it again so gives you a leak.

Memory Management issues with LIExposeController in iOS

I am using "LIExposeController" to create new UIViewControllers. This brings up some memory warnings that I am not able to fix.
- (UIViewController *)newViewControllerForExposeController:(LIExposeController *)exposeController {
UIViewController *viewcontroller = [[[MyViewController alloc] init] autorelease];
return viewcontroller; // warning here saying object with a + 0 retain count returned to caller where a + 1 (owning) retain count is expected
}
- (void)shouldAddViewControllerForExposeController:(LIExposeController *)exposeController {
[exposeController addNewViewController:[self newViewControllerForExposeController:exposeController]
animated:YES];
} // warning here saying potential leak of an object
LIExposeController *exposeController = [[[LIExposeController alloc] init] autorelease];
exposeController.viewControllers = [NSMutableArray arrayWithObjects:
[self newViewControllerForExposeController:exposeController],
nil]; // warning here saying potential leak of an object
A method starting with new is expected to produce an object with retain count +1 and not an autoreleased/retain count +0 object. ARC and the Static Analyzer both follow these conventions as rules, and you should fix your code to meet them or rename your method to not clash with the conventions.

Under ARC, keep getting EXC_BAD_ACCESS after using ARC, because of using Block?

Issue:
I keep getting EXC_BAD_ACCESS. And after I open NSZombieEnabled, I saw this [FeatureCommentListViewController respondsToSelector:]: message sent to deallocated instance 0x7c1dc30
Before I changed my project to ARC, there is no such error, but after I changed to ARC, this error appeared.
I declare a ViewController in a Block and push it into navigation Controller. Will this reason case it's lifetime shorter?
UIBlockButton is from this post
UIBlockButton *lbGood3 = [[UIBlockButton alloc] initWithFrame:CGRectMake(0, 0, First_Button_Width, [self getGoodRow2Height:productDetail]) ];
[lbGood3 handleControlEvent:UIControlEventTouchUpInside withBlock:^ {
NSLog(#"%#", Label.text);
ProductDetail *productDetail = [productDetailDict objectForKey:#"product"];
NSString *dp_id = [NSString stringWithFormat:#"%#-%#",productDetail.url_crc,productDetail.site_id];
FeatureCommentListViewController *cmtListController = [[FeatureCommentListViewController alloc] initWithNibName:#"FeatureCommentListViewController" bundle:nil];
cmtListController.title = Label.text;
cmtListController.isReviewed=isReviewed;
cmtListController.productDetail=productDetail;
cmtListController.dp_id=dp_id;
cmtListController.feature_name = #"&feature_good_id=2";
[self.navigationController pushViewController:cmtListController animated:YES];
}];
Should I declare the controller as a member of this viewController or just declare out of the block?
I solved this by alloc the FeatureCommentListViewController in the viewDidLoad function and use it in block.
1st. Im wondering why do you push a view controller in a block but not in the main thread? Isn't it important to give a quick response to the touch action?
2nd.[self.navigationController pushViewController:cmtListController animated:YES]; is in your block. Whenever you left the current navigationController what will the self.navigationController represent?
3rd. If you declare the viewController out of the block you can add __block in front of it as mentioned by Hermann Klecker.

Resources