Application crashes when go to previous ViewController - iphone

Hi My iPhone application is crashing when i tap on back button. I really don't know why this is happening. you can see in picture where my application is crashing.
I have already checked that my button is connected only with one action. So I am failed to know exactly what is the problem.
If my question silly then plz forgive me, I am new to iPhone development So please help
Infect i am using printers with device and i think there is some problem
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor colorWithRed:0.8 green:0.8 blue:1 alpha:1] CGColor],(id)[[UIColor colorWithRed:0.05 green:0.05 blue:0.38 alpha:255] CGColor], nil];
//creating the iEpsonCom objects
m_deviceParams = [[DeviceParameters alloc] init];
m_device = [[Device alloc] init];
//registering the callback
[m_device registerCallback:self withSelector:#selector(callbackMethod:)];
//creating a thread for regularly checking the connection state
m_Thread = [[ThreadClass alloc] init];
[m_Thread registerCallback:self withSelector:#selector(connectionStateThreadCallbackMethod:)];
[m_Thread setMilliseconds:300]; //check the connection state every 300 ms
[m_Thread start]; //start the thread

It might be that your previous view controller is lost so first check, is the controller that you wish to pop to still in the view controller stack?
NSArray *arrView = [self.navigationController viewControllers];
NSLog(#"arrView %#",arrView);
If you can't see the view controller you want to pop to in that stack, it's likely that you either need to retain the controller. It might also be possible that you have released it too much, for example in the dealloc method.

Related

Xcode Instruments Leak coming from UIDeviceRGBColor

I have a leak coming from UIDeviceRGBColor. The responsible frame is +[UIColor allocWithZone:]. I am not using ARC.
The leak is coming from the method below.
- (void) lAction
{
MKCoordinateRegion mapRegion;
mapRegion.center = mapView.userLocation.coordinate;
mapRegion.span.latitudeDelta = 0.05;
mapRegion.span.longitudeDelta = 0.05;
[mapView setRegion:mapRegion animated: YES];
SettingsViewController *settingsViewController = [[SettingsViewController alloc]
initWithNibName:#"SettingsViewController" bundle:nil];
The leak is coming from this next line:
[self presentModalViewController: settingsViewController animated:YES];
Then the method finishes like this:
self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:40.0/255.0
green:43.0/255.0 blue:46.0/255.0 alpha:1.0];
}
Anyone know how to fix this? Thank you all!
Try:
SettingsViewController *settingsViewController = [[[SettingsViewController alloc]
initWithNibName:#"SettingsViewController" bundle:nil] autorelease];
In order to satisfy the commenter, the explanation is simply that if you're not using ARC, whenever you call alloc, the retain count of the returned object is set to 1. You are responsible for releasing it. One easy way to do that is call autorelease on it which will automatically release it at the end of the main run loop (unless you're managing your own autorelease pool, but I won't get into that). You do want to make sure that as long as you need to use an object your code has something retaining it, in this case when you call
[self presentModalViewController: settingsViewController animated:YES];
an extra retain is called on the settingViewController, so you don't have to worry about it getting dealloced when your method finishes.
I find memory management in Objective-C pretty easy, but it does require extra code and everyone uses ARC these days. If you're just making a few small changes to an existing code base, there is no need to switch to ARC, but if you're going to continue working with the code base for some time, it will be more time efficient to switch. It is pretty straightforward to do, as Xcode will do most of the work for you. (See here: http://developer.apple.com/library/ios/#releasenotes/ObjectiveC/RN-TransitioningToARC/Introduction/Introduction.html).

How to update a progressbar during instantiation of timeconsuming ui elements?

I would like to update a progressbar while i am instantiating some ui elements that takes some times. I first create my view during the viewLoad method and add my progress bar there. Once my view appeared in the viewDidAppear method I am making several uikit objects instantiation but i would like to update the progress bar in the mean time. I am not sure how to proceed since everything should happen in the main thread as it is ui elements.
Here is part of my code:
-(void) viewDidAppear:(BOOL)animated
{
// precompute the source and destination view screenshots for the custom segue
self.sourceScreenshotView = [[UIImageView alloc] initWithImage:[self.view pw_imageSnapshot]];
[self.progressBar setProgress:.3];
SCLViewController *rvc = [[SCLViewController alloc] init];
UIView *destinationView = rvc.view;
destinationView.frame = CGRectMake(0, 0, kWidthLandscape, kHeightLandscape);
self.destinationScreenshotView = [[UIImageView alloc] initWithImage:[destinationView pw_imageSnapshot]];
[self.progressBar setProgress:.5];
}
In the above code I just need to create two screenshots of views to use them later on. The problem is that i only see the last update (.5) when setting the progress to the progress bar. What is the proper way to do this update?
You can use the performSelectorInBackground:withObject: method in order to instantiate your heavy views. That method (the one that instantiates your views) will have to set your progress bar progress in the main thread.
So your code would look something like this:
- (void)viewDidAppear:(BOOL)animated
{
[self performSelectorInBackground:#selector(instantiateHeavyViews) withObject:nil];
}
- (void)instantiateHeavyViews
{
self.sourceScreenshotView = [[UIImageView alloc] initWithImage:[self.view pw_imageSnapshot]];
[self performSelectorOnMainThread:#selector(updateMyProgressView:) withObject:[NSNumber numberWithFloat:0.3f] waitUntilDone:YES];
SCLViewController *rvc = [[SCLViewController alloc] init];
UIView *destinationView = rvc.view;
destinationView.frame = CGRectMake(0, 0, kWidthLandscape, kHeightLandscape);
self.destinationScreenshotView = [[UIImageView alloc] initWithImage:[destinationView pw_imageSnapshot]];
[self performSelectorOnMainThread:#selector(updateMyProgressView:) withObject:[NSNumber numberWithFloat:0.5f] waitUntilDone:YES];
}
- (void)updateMyProgressView:(NSNumber *)progress
{
[self.progressBar setProgress:[progress floatValue]];
}
Edit: of course, it won't animate your progress bar (I don't know if that is what you wanted). If you want it to move on while your views are being created, you should use a delegate to be notified of the progress, and this can be a bit harder. This way you would be able to update the progress bar every time the delegate is notified.

How do you manage memory when adding a UIImageView to another windows view?

Since I think I scared everyone away with my other recent post ( Memory management when adding a UIImageView to another viewController's view from another viewController's view ) and I'm running out of time, I'd just like to ask this in simpler terms. If you want to know I'm doing with more detail, refer to the other post.
How do you manage memory, and releasing, when adding a UIImageView to another windows view?
i.e. I'm creating a UIImageView in one view, and adding it as a subview in another windows view. The other window is being used by a TV.
Right now I have:
mapImageViewEx = [[UIImageView alloc] init];
// I think you can ignore these two things below
CGPoint p = mapScrollView.contentOffset;
mapImageViewEx.frame = CGRectMake((p.x*-1), (p.y*-1), mapImageView.frame.size.width, mapImageView.frame.size.height);
NSString *mapExFileLocation = [[NSBundle mainBundle] pathForResource:[map_List objectAtIndex:mapNum] ofType:#"png"];
NSData *mapExIMGData = [NSData dataWithContentsOfFile:mapExFileLocation];
mapImageViewEx.image = [UIImage imageWithData:mapExIMGData];
// finds the other window's view controller's view
UIView *containerExViewP = (UIView*)[del.switchExVC.view viewWithTag:9000];
// adds it
[containerExViewP addSubview:mapImageViewEx];
Problem I'm having is that it keeps stacking up memory use every time I exit the view. Releasing the image in the iPad screen view's dealloc does nothing.
change
mapImageViewEx = [[UIImageView alloc] init];
to
mapImageViewEx = [[[UIImageView alloc] init] autorelease];
autorelease means that the reference count will be decremented at some unknown point in the future.
the addSubview will increment the count, so you don't have to worry about it.
As long as the parent view is properly disposed of, mapImageViewEx will be released.

Implementing my own navigation controller?

I have a tab bar app. Under one of the tabs i want a uisegmentedControl in the top navigation view, that controls what view is currently displayed. This is dead easy if i just exchange the view, but i want to do it in a more organized and generic way, by using one uiviewcontroller for each view and exchanging them in the most optimzed way.
i guess step one would be to know exactly what a tabbar controller sends to a navigation controller/view controller when a tab is changed, and work it out from there.
Can any one point me in the right direction?
Some time ago I stumbled upon SegmentsController which I found in this blog entry from red artisan.
I used it in conjunction with a UITabBarController, but without knowing I did it wrong. Not wrong as in "it crashs" or "it doesn't do what i want" but wrong in the sense that I have to forward each UIViewController call (like viewDidAppear, receivedMemoryWarning etc) to the child viewControllers. The app with the wrong code is still in the app store and I never received a complain about it.
But I played around a while and figured out how to use it right. It's a bit of a hassle but imho it's absolutely worth it.
I'll show you the correct version that I have right now, I'm creating the UITabBarController in Interface Builder so I have to change the tab in code. Which introduces another piece of mess, and maybe there is room for improvements. But right now I'm satisfied with this solution.
NSMutableArray *items = [self.tabBarController.viewControllers mutableCopy]; // tabs from tabbar configured in IB
// The two child vc that will appear in the segment control
SomeViewController_iPhone *tvcs = [[[SomeViewController_iPhone alloc] initWithNibName:#"SomeView_iPhone" bundle:nil] autorelease];
SomeOtherViewController_iPhone *tvct = [[[SomeOtherViewController_iPhone alloc] initWithNibName:#"SomeOtherView_iPhone" bundle:nil] autorelease];
NSArray *viewControllers1 = [NSArray arrayWithObjects:tvcs, tvct, nil];
// the nav controller acts as a wrapper around the child viewcontrollers
UINavigationController *navController1 = [[[UINavigationController alloc] init] autorelease];
navController1.tabBarItem.title = NSLocalizedString(#"FirstTab", nil);
navController1.tabBarItem.image = [UIImage imageNamed:#"tabImage1.png"];
navController1.navigationBar.tintColor = [UIColor navBarTintColor];
firstTabSegmentsController = [[SegmentsController alloc] initWithNavigationController:navController1 viewControllers:viewControllers1];
// uses a NSArray category that basically creates a NSArray that has the title properties of the vc in viewControllers1
firstTabSegmentedController = [[UISegmentedControl alloc] initWithItems:[viewControllers1 arrayByPerformingSelector:#selector(title)]];
firstTabSegmentedController.frame = CGRectMake(0, 0, 222, 30);
firstTabSegmentedController.segmentedControlStyle = UISegmentedControlStyleBar;
firstTabSegmentedController.selectedSegmentIndex = 0;
[firstTabSegmentsController indexDidChangeForSegmentedControl:firstTabSegmentedController];
[firstTabSegmentedController addTarget:firstTabSegmentsController action:#selector(indexDidChangeForSegmentedControl:) forControlEvents:UIControlEventValueChanged];
// replace first tab from interface builder with this
[items replaceObjectAtIndex:0 withObject:navController1];
as you see it needs a bit of setup, but in my opinion this solution is better than anything else I've tried throughout the time. I hope I de-NDAed the code correctly.
Edit: Uploaded a sample project: BeautifulColors.zip
Just exchanging the views and keeping up with the current view's viewController is the best way to implement a UISegmentedControl in this regard.
Note: by exchanging the views i mean adding a subview to the current view and removing the old one.
You might be interested in the method below, which is implemented by the UITabBarControllerDelegate
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController;

Application crashes on adding custom banner or label in adwhirl

What steps will reproduce the problem?
1.√ [adWhirlView replaceBannerViewWith:replacement];
What is the expected output? What do you see instead?
Exec Bad Access on replaceBanner with UILabel as specfied in example, it also crashes when I add my custom banner View
What version of the product are you using? On what operating system?
Mac OX 10.6.3 and AdWhirlSDK_iPhone_2.6.2.zip
Please provide any additional information below.
Whenever I integrate my custom add in Adwhirl so my application crashes [adWhirlView replaceBannerViewWith:replacement]; please let me know how to overcome this issue.
- (void)performEvent:(AdWhirlView *)adWhirlView {
// replace banner content
UILabel *replacement = [[UILabel alloc] initWithFrame:kAdWhirlViewDefaultFrame];
replacement.backgroundColor = [UIColor blackColor];
replacement.textColor = [UIColor whiteColor];
replacement.textAlignment = UITextAlignmentCenter;
replacement.text = [NSString stringWithFormat:#"Event performed, view %x", adWhirlView];
[adWhirlView replaceBannerViewWith:replacement];
[replacement release];
}
http://code.google.com/p/adwhirl/wiki/CustomEvents
EXC_BAD_ACCESS it represents that you have removed some objects and after that you trying to access it. OR you have created an autorelease objects and you are accessing the object afterwards. In both the cases the EXC_BAD_ACCESS is shown when you try to access an object which is not pointing to anything