EXC_BAD_ACCESS when opening modal view - iphone

I'm having a problem with my iPhone App, I'm getting EXC_BAD_ACCESS, I had some memory leaks, but these are now fixed, so I'm not sure whats going on. I realise that I haven't provide a lot of information, but I really don't know whats happening.
The initial screen opens up where I have a number of buttons. Tapping on the first button, which runs the following code and opens up a modal view:
-(IBAction)newWorkoutButton
{
newWorkoutViewController .loadedFromRootViewController = #"YES";
[self presentModalViewController:newWorkoutViewController animated:YES];
}
The screen freezes and the is in the code below:
#import <UIKit/UIKit.h>
#import <objc/runtime.h>
#import <CoreLocation/CoreLocation.h>
int main(int argc, char *argv[])
{
Method getDistanceFrom = class_getInstanceMethod([CLLocation class], #selector(getDistanceFrom:));
class_addMethod([CLLocation class], #selector(distanceFromLocation:), method_getImplementation(getDistanceFrom), method_getTypeEncoding(getDistanceFrom));
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, nil, nil); // ERROR HAPPENING HERE
[pool release];
return retVal;
}

Like Aleks suggested you can try to find the zombie like this:
I find this alternative more convenient:
Click the "Run Button Dropdown"
From the list choose Profile
The program "Instruments" should open where you can also choose Zombies
Now you can interact with your app and try to cause the error
As soon as the error happens you should get a hint on when your object was released and therefore deallocated.
(source: dimzzy.com)

Related

UIApplication delegate method is not called

I have a problem integrating Facebook SDK 3.2 with my app, my app goes to facebook app for approval and login, but when
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation;
needs to be called when facebook authorization is done, it simply skips it.
It works great when I force webView facebook login via modal viewcontroller, but when I want to use installed facebook app/safari it just doesn't work.
Any suggestions?
Thank you.
Make sure you put UIApplicationDelegate in interface declaration.
#interface AppDelegate : NSObject <UIApplicationDelegate>
Also verify its right App delegate class
int main(int argc, char *argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, nil, #"AppDelegate");
[pool release];
return retVal;
}
Also add fb Id in plist.

"EXC_BAD_ACCESS" when switch too fast between an tableView and a mapView

I have a tableView with an button to push a mapView. The push and back actions generally work fine. If I switch quickly between these two views, "EXC_BAD_ACCESS" error will appear.
MapViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
self.mapView.delegate = self;
UIButton *btnL = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40.0, 40.0)];
[btnL setImage:[UIImage imageNamed:#"back.png"] forState:UIControlStateNormal];
[btnL addTarget:self.navigationController action:#selector(popViewControllerAnimated:) forControlEvents:UIControlEventTouchDown];
self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:btnL] autorelease];
[btnL release];
self.whereAmIAnnotation = [[[WhereAmIAnnotation alloc] init] autorelease];
if (!self.mapView || !self.whereAmIAnnotation) {
NSLog(#"mapview : %#", self.mapView);
NSLog(#"whereAmIAnnotation : %#",self.whereAmIAnnotation);
// will never enter in to here
}
[self.mapView addAnnotation:self.whereAmIAnnotation];
}
If I comment [self.mapView addAnnotation:self.whereAmIAnnotation]; , there is no "EXC_BAD_ACCESS" anymore.
Any answers and comments will be appreciated. Thanks in advance!
Edit 2
main.m
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
int main(int argc, char *argv[])
{
#autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
// "EXC_BAD_ACCESS" error message shows here
}
}
Edit 3:
the whereAmIAnnotation is declared here:
MapViewController.m
#interface AddrCoordinateAdjustMapViewController()
#property (retain) WhereAmIAnnotation *whereAmIAnnotation;
#end
#implementation MapViewController
#synthesize whereAmIAnnotation;
Edit 4:
Error message is as following:
2012-07-30 15:56:19.735 myApp[13584:707] *** -[MapViewController respondsToSelector:]: message sent to deallocated instance 0x10195e80
It usually crashes when i switch back to the tableView while the annotationView is dropping.
Have you removed yourself as delegate of the mapview?
self.mapview.delegate = nil;
try removing it at viewWilldisappear or whenever you consider necessary as you might need it later if you have pushed a view controller and will return to the view later.
In a nutshell, remove it when your view cannot respond to the delegate which is why you got the EXC_BAD_ACCSS. it sent a message to your view but it was already released from memory.
Do you release and set to nil all your IBOutlets in the viewDidUnload? So at least you should do:
- (void)viewDidUnload {
self.mapView = nil;
[super viewDidUnload];
}
Generally in ViewDidUnload you should release and set to nil, any view objects that are created from a Nib file or allocated in your ViewDidLoad method
I would suggest to check whether self.whereAmIAnnotation isnt already released by at the time you add it to mapView. That might be one reason for the BAD_ACCESS you receive.
I had similar problem and my solution was to remove all overlays from the map before popping controller from UINavigationController. I was using OpenStreetMap.

Implement auto log-off feature in Iphone

I am making an application which logs in using some username and password. Now when i am logged in succesfully I want my application to be logged out automatically if no interaction with the application found for 10-12 minutes.
Can anybody guide me how can i achieve this ??
Help with some code will be really appreciated.
Thanks
The Thing you want to implement is called SESSION MANAGEMENT.You have to subclass the UIApplication.
#interface MyUIApp : UIApplication {
}
In this class you have to reset the timer each time. Also you have to check if the application is responded or not with this.If the idleTimer is Exceeded then push the viewController to your login view.
- (UIResponder *)nextResponder {
[self resetIdleTimer];
return [super nextResponder];
}
Also you have to change the main class file with this:-
#import <UIKit/UIKit.h>
int main(int argc, char *argv[]) {
NSString* appClass = #"MyUIApp";
NSString* delegateClass = nil;
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, appClass, delegateClass);
[pool release];
return retVal;
}

Is this release call causing instruments to crash?

I have a class called CategoryViewController and its viewDidLoad method calls this method:
- (void)reset {
Category * c = [[Category alloc] initWithId:0 title:#"Categories"];
[self setCategory:c];
[c release]; // <--- line of code I am interested in
self.title = #"Categories";
[self fillCategory];
}
In most situations category here would be null, but sometimes reset needs to be called after category has been assigned. Without the line I marked in my code, the program builds and debugs just fine and I can pull up instruments and check my leaks. The only leak I can find is a category initialized from this function (because without the release, I get a leak when calling this function on a CategoryViewController that has already been initialized).
If I try to run this method as is WITH the release on c, Instruments, XCode, and the Simulator all begin to act strangely, crashing and freezing, giving me random SIGABRTs and SIGKILLs. I can build and debug with the line of code in there, but Instruments won't even start my app. Can anyone give me a hint as to what's going on here?
EDIT: More code
#implementation Category
#synthesize title, articleCount, seeAlso, categoryId, articles, subcategories;
- (id)initWithId:(NSInteger)cid title:(NSString*)t{
self.title = t;
self.categoryId = cid;
[self setArticles:[[NSMutableArray alloc] init]];
[self setSubcategories:[[NSMutableArray alloc] init]];
[self setSeeAlso:[[NSMutableArray alloc] init]];
self.articleCount = 0;
return self;
}
It's funny how these things seem to be resolved so easily after you take the time to post them online. After posting the Category init code I realized I wasn't properly releasing the allocations I made. My leaks as well as my crashes appear to be gone after proper memory management like so:
- (id)initWithId:(NSInteger)cid title:(NSString*)t{
self.title = t;
self.categoryId = cid;
NSMutableArray * m = [[NSMutableArray alloc] init];
[self setArticles:m];
[m release];
m = [[NSMutableArray alloc] init];
[self setSubcategories:m];
[m release];
m = [[NSMutableArray alloc] init];
[self setSeeAlso:m];
[m release];
self.articleCount = 0;
return self;
}
Assuming your property is declared like below, I don't see any problem with the line you highlighted, nor with the rest of your code:
#property (retain) Category *c;
Just shooting in the dark, but last time Xcode, Instruments and the Simulator did crazy stuff, I had an infinite recursion going on.
Are you sure your method fillCategory doesn't somehow call your viewDidLoad or reset method?
What happens when you set a breakpoint on the first line of your reset method, and follow your code step-by-step?

iPhone - Preventing Sleep while using applicationMusicPlayer in MPMusicPlayerController

I am trying to disable the automated sleeping of iPhone for certain period in my app.
Used [[UIApplication sharedApplication] setIdleTimerDisabled:YES] which works fine as long as I play no music.
But when I play music the Idle Timer seems to get reactivated.
I have tried all kinds of tricks from NSTimer firing silent sounds every 10 second etc but nothing works.
Would welcome any suggestion or thoughts on making this happen.
The way to fix this is to first subclass UIApplication, override the setIdleTimerDisabled method and make it do nothing. Then, add a couple of your own methods that you'll call from your application instead of using the normal setter. By doing this you will ignore all messages that might change the idle timer aside from the custom method calls you make yourself. Here's how you do it:
Edit your main.m file to use a custom UIApplication subclass:
#import <UIKit/UIKit.h>
int main(int argc, char *argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSString* appClass = #"CustomUIApplicationSubclass";
NSString* delegateClass = nil;
int retVal = UIApplicationMain(argc, argv, appClass, delegateClass);
[pool release];
return retVal;
}
Then define your UIApplication subclass:
#interface CustomUIApplicationSubclass : UIApplication {
}
- (void)disableIdleTimer;
- (void)enableIdleTimer;
#end
#implementation CustomUIApplicationSubclass
- (void)setIdleTimerDisabled:(BOOL)disabled
{
// do nothing! take that stupid ipod controller!
}
- (void)enableIdleTimer
{
[super setIdleTimerDisabled:NO];
}
- (void)disableIdleTimer
{
[super setIdleTimerDisabled:YES];
}
#end
This will force the iPod controller to use your custom UIApplication instance which does no longer does anything when the normal setIdleTimerDisabled method is called.