ARC Memory Management: random memory warnings - ios

I keep getting random crashes saying:
Received memory warning.
(lldb)
Now after a bit of reading I have found that this is probably due to memory management, resources been fully used and none free. I thought in ARC we dont need to free up memory and release things (it wont even let us release) I thought it did it all by itself.
I have seen from some articles & threads that a possible problem is way that you define #properties so some I have:
FirstViewController
#property (strong) FilterViewController *filterViewController;
#property (weak, nonatomic) IBOutlet MKMapView *mapView;
#property(nonatomic, retain) IBOutlet UILabel *sliderValue;
#property(nonatomic, retain) NSString *passedData;
#property int selectedTime;
FilterViewController
#property (nonatomic, retain) IBOutlet UIDatePicker *datePicker;
#property (strong, nonatomic) IBOutlet UILabel *stepperValueLabel;
#property (strong) FirstViewController *firstViewController;

Your problem is Retain cycle. firstViewController object is retains filterViewController, and filterViewController object is retains firstViewController
#property (strong) FirstViewController *firstViewController; in FilterViewController
#property (strong) FilterViewController *filterViewController; in FirstViewController

Related

What made the difference

What made the difference
#property (strong, nonatomic) and #property (nonatomic, strong) in ios.
i will define iboutlet for example uilabel like this
#property (strong, nonatomic) IBOutlet UILabel *label1;
and i see many time in this site
#property (nonatomic, strong) NSString* str;
What is the difference between the two property.
There are no differences in the logic. They represent the same thing but with different order.
Usually in IBOutlets you have #property (weak, nonatomic) because it is auto generated when you ctrl+drag from interface builder.
However, most people prefer the second form because the "nonatomic" is used in most of the cases in ios and therefore it is easily ignored.
There is no difference. But in apple sample codes and most frequently we use :
#property (nonatomic, strong)

abandoned memory problems with the heap

I am trying to learn a bit about the instruments panel in Xcode. I am using ARC. When checking allocations I have been coming up with some abandoned memory it issues. The picture below depicts code from my prepareForSegue method. Every time I tap to perform the segue, memory is allocated and never released. So if I go back and forth between the two viewControllers, the memory keeps climbing and eventually terminates the app. I used the tools to narrow down the problems in the code but am unsure where to go from here. I have seen several examples online about how to identify the problem but not about how to resolve it. So my question is, what is going on here that is causing the app to not release the memory?
properties for viewController 1:
#property (nonatomic, retain) NSMutableArray *recAlbumsArray;
#property (nonatomic, retain) NSMutableArray *titlesArray;
#property (nonatomic, retain) NSMutableArray *timersArray;
#property (nonatomic, retain) NSMutableArray *albumsArray;
#property (nonatomic, retain) NSMutableArray *imagesArray;
#property (nonatomic, retain) NSMutableArray *objsIdArray;
#property (nonatomic, retain) NSArray *imagesDataArray;
#property (nonatomic, weak) NSArray *recAlbumData;
#property (nonatomic, weak) NSArray *albumData;
#property (nonatomic, weak) NSString *albumTitle;
#property (nonatomic, weak) NSString *objId;
properties for viewController 2:
#property (nonatomic, retain) NSMutableArray *album;
#property (nonatomic, weak) NSString *title;
#property (nonatomic, weak) NSString *albumRef;
#property (nonatomic, weak) NSString *objId;
I have tried setting all the properties to nil in a viewDidDisappear method and also tried removing the 1st viewController from the viewController array hierarchy but nothing changed.

How to fix the warning of "Autosynthesized property 'myVar' will use synthesized instance variable '_myVar', not existing instance variable 'myVar' "?

I declare my .h file like this:
#import <UIKit/UIKit.h>
#interface NavigationTripViewController : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource>{
NSArray *questionTitleTrip;
NSArray *questionDescTrip;
NSMutableArray *answerTrip;
NSMutableArray *pickerChoices;
int questionInt;
int totalInt;
IBOutlet UILabel *questionNum;
IBOutlet UILabel *questionTotalNum;
IBOutlet UILabel *recordType;
IBOutlet UITextView *questionDes;
IBOutlet UIView *answerView;
IBOutlet UIButton *preButton;
IBOutlet UIButton *nextButton;
UITextField *text;
UIPickerView *picker;
}
#property (retain, nonatomic) NSArray *questionTitleTrip;
#property (retain, nonatomic) NSArray *questionDescTrip;
#property (retain, nonatomic) NSMutableArray *answerTrip;
#property (retain, nonatomic) NSMutableArray *pickerChoices;
#property (retain, nonatomic) IBOutlet UILabel *questionNum;
#property (retain, nonatomic) IBOutlet UILabel *questionTotalNum;
#property (retain, nonatomic) IBOutlet UILabel *recordType;
#property (retain, nonatomic) IBOutlet UITextView *questionDes;
#property (retain, nonatomic) IBOutlet UIView *answerView;
#property (retain, nonatomic) IBOutlet UIButton *preButton;
#property (retain, nonatomic) IBOutlet UIButton *nextButton;
#property (retain, nonatomic) UITextField *text;
#property (retain, nonatomic) UIPickerView *picker;
-(IBAction)clickPre;
-(IBAction)clickNext;
#end
And my .m file here like this:
#import "NavigationTripViewController.h"
#import <QuartzCore/QuartzCore.h>
#interface NavigationTripViewController ()
#end
#implementation NavigationTripViewController
#synthesize questionTitleTrip,questionDescTrip,answerTripl,pickerChoices,questionNum,questionTotalNum,recordType,questionDes,answerView,preButton,nextButton,text,picker;
All my variables in the #synthesize receive the warnings:
Autosynthesized property 'myVar' will use synthesized instance variable '_myVar', not existing instance variable 'myVar'
Also, the variables and class names used in viewDidLoad don't display in colors, just show in black color. In other view controllers, there are no warnings like this. How to fix these problems?
Edit:
Basically for all intents and purposes you should be building on a new enough XCode to use the new behavior, in that solution you typically will just remove the ivar from the #interface block in the .h file... If for some reason you do need to access an ivar directly you can now declare it in the #implementation block... and use #synthesize var or #synthesize var=_var
OGPost:
to make that go away you can go all new school and drop the iVar, or you can go all old school and add an #synthesize someIvar in your #implementation block.

Message Sent to Deallocated Instance when adding storyboard view as a subview

Okay, I have a basic understanding of what is happening here, but am having trouble fixing it. I am hoping someone can walk me through what I'm doing wrong here...
I have a nifty app that works great and was built with the storyboard and custom UIViewControllers to handle all my code. I was doing really well, until I needed to handle my push notifications by dropping me in a specific view and loading some data. I made a lot of headway today and just got stuck in a bad way. I am now getting an objc_sendmsg error and I know it has to do with my memory management. I've never initialized a view in this way, so I'm wondering if that's what's causing it. Basically, I can load a view, but I can never push any buttons or get anywhere after that.
Here's the code:
AppDelegate.m
UIStoryboard* storyBoard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UIViewController *detailVC = [storyBoard instantiateViewControllerWithIdentifier:#"Leads_Calls_SB"];
[self.window addSubview:detailVC.view];
[[NSNotificationCenter defaultCenter] postNotificationName:#"callReceived"
object:nil
userInfo:userInfo];
[self.window makeKeyAndVisible];
Leads_CallsDetailViewController.h
#property (strong, nonatomic) IBOutlet UILabel *cName;
#property (strong, nonatomic) IBOutlet UILabel *cStart;
#property (strong, nonatomic) IBOutlet UILabel *cNumber;
#property (strong, nonatomic) IBOutlet UILabel *cStart2;
#property (strong, nonatomic) IBOutlet UILabel *cEnd;
#property (strong, nonatomic) IBOutlet UILabel *cDuration;
#property (strong, nonatomic) IBOutlet UILabel *cStatus;
#property (strong, nonatomic) IBOutlet UILabel *cProvider;
#property (strong, nonatomic) IBOutlet UILabel *cLineType;
#property (strong, nonatomic) IBOutlet UILabel *cCity;
#property (strong, nonatomic) IBOutlet UIView *innerView;
#property (strong, nonatomic, retain) IBOutlet UIButton *backStyle;
#property (strong, nonatomic) IBOutlet UIScrollView *scrollView;
#property (strong, nonatomic, retain) NSString *cNotifID;
#property (strong, nonatomic, retain) NSString *cNameText;
#property (strong, nonatomic, retain) NSString *cNumberText;
#property (strong, nonatomic, retain) NSString *cStartText;
#property (strong, nonatomic, retain) NSString *cEndText;
#property (strong, nonatomic, retain) NSString *cCallStatusText;
#property (strong, nonatomic, retain) NSString *cLatitudeText;
#property (strong, nonatomic, retain) NSString *cLongitudeText;
#property (strong, nonatomic, retain) NSString *cCityText;
#property (strong, nonatomic, retain) NSString *cLineTypeText;
#property (strong, nonatomic, retain) NSString *cProviderNameText;
- (IBAction)back:(id)sender;
#property (strong, nonatomic) IBOutlet MKMapView *map;
- (IBAction)forward_lead:(id)sender;
- (IBAction)call_lead:(id)sender;
- (IBAction)add_lead:(id)sender;
#property (strong, nonatomic) IBOutlet UIButton *bottomMessage;
#property (strong, nonatomic) IBOutlet UIButton *bottomCalls;
#property (strong, nonatomic) IBOutlet UIButton *bottomReports;
#property (strong, nonatomic) IBOutlet UIButton *bottomHome;
.m
- (IBAction)back:(id)sender {
if (self.cNotifID != nil)
{
[self.view removeFromSuperview];
}
else {
[self.navigationController popViewControllerAnimated:YES];
}
}
I'm not sure what I'm doing wrong, but no matter what happens if I hit any button on that page or try to dismiss the view, it screams at me and gets angry...I've tried everything I can think of to figure this out.
Thanks guys!
The issue is that you're creating your view controller, grabbing its view, but the letting the controller fall out of scope (and presumably using ARC where it's getting released on you).
In my original answer, I thought the goal was simply to consider different ways of presenting the standard initial view controller. But that is not the case. The question is how to present a new scene when some event takes place (in my example, I'm doing it upon openURL, but you could presumably do this in response to notifications and the like).
Anyway, one approach to solving this is to perform presentViewController. So you could do something like:
// determine the current controller (in case you've already done some modal segues)
UIWindow *window = [[UIApplication sharedApplication] keyWindow];
UIViewController *currentController = window.rootViewController;
while (currentController.presentedViewController)
currentController = currentController.presentedViewController;
// load the controller for the new scene
UIStoryboard* storyBoard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UIViewController *newController = [storyBoard instantiateViewControllerWithIdentifier:#"Leads_Calls_SB"];
// perform a modal transition to the new scene
[currentController presentViewController:newController animated:NO completion:nil];

IOS sythesize - when to declare an internal variable?

I'm following along with Apple's "Hello" tutorial on iOS, and I'd like to know why it's necessary to declare the "userName" variable (to be accessed in code later). Isn't it generated by the synthesize statement?
#interface HelloWorldViewController : UIViewController {
NSString *userName;
}
- (IBAction)changeGreeting:(id)sender;
#property (weak, nonatomic) IBOutlet UILabel *label;
#property (weak, nonatomic) IBOutlet UITextField *testField;
#property (nonatomic, copy) NSString *userName;
And here's the implementation of synthesize:
#synthesize label=_label;
#synthesize testField=_testField;
#synthesize userName=_userName;
It wasn't always generated by the property/synthesize. That's a relatively new addition to Objective-C. The tutorial was probably written before this was the case.

Resources