memory full with views and crash - ipad

I have a problem with my application. I have 18 views but when you view every time step consumes more memory. After the application is crash.
while ([self.view.subviews count] > 0) {
[[self.view.subviews lastObject] removeFromSuperview];
}
[self presentViewController:[[proj_lletrac_14_ontroller alloc] init] animated:NO completion:nil];
[proj_lletrac_14_Controller release];
[proj_lletrac_13_Controller release];
This code is a small example
My english is bad, I speak spanish :)
Thanks

The problem is your passing in an allocated object and not releasing it. You need to have it in a variable so you can release it:
proj_lletrac_14_ontroller *viewController = [[proj_lletract_14_ontroller alloc] init];
[self presentViewController:viewController animated:NO completion:nil];
[viewController release];
So in steps your allocating an instance to a variable:
proj_lletrac_14_ontroller *viewController = [[proj_lletract_14_ontroller alloc] init];
Passing the instance to the presentViewController method:
[self presentViewController:viewController animated:NO completion:nil];
Now you don't need to worry about the instance so you release your allocated object:
[viewController release];
Simple memory management.
Never allocate an object when passing in to functions unless you stick it in an autorelease pool:
[self presentViewController:[[[proj_lletrac_14_ontroller alloc] init] autorelease] animated:NO completion:nil];
But this is bad memory management practice. You should release everything you know that you don't need again.

Related

Creating uiviewcontroller as weak programmatically

I am creating uiviewcontroller in my one of the singleton like this.
BrowserVC *vc = [BrowserVC loadFromNib];
vc.titleName = #"Password Management";
[vc setData:#{#"url":url.absoluteString}];
vc.successBlock = ^(NSString *urlStr){
if (![urlStr isEqualToString:url.absoluteString])
[vc dismissViewControllerAnimated:YES completion:nil];
};
Then, I got this usual error.
Capturing 'vc' strongly in this block is likely to lead a retain
cycle.
To solve that warning, I have used either __Weak or __unsafe_unretained. Now, problem come in. My vc is released immediately after I initiate. I can't even set titleName. How shall I do?
The pattern is this
BrowserVC *vc = [BrowserVC loadFromNib];
vc.titleName = #"Password Management";
[vc setData:#{#"url":url.absoluteString}];
__weak __typeof(vc) weakVC = vc;
vc.successBlock = ^(NSString *urlStr){
__typeof(vc) strongVC = weakVC;
if (strongVC && ![urlStr isEqualToString:url.absoluteString])
[strongVC dismissViewControllerAnimated:YES completion:nil];
};
You create the view controller as you normally would but then you create a weak reference to the view controller and pass that into the block

dismissViewControllerAnimated not being called on iPhone

I have the following code where I am trying to show a PDF preview. It words perfectly on an iPad however when I am trying to do it on a iPhone it dosnt work.
QLPreviewController* preview = [[QLPreviewController alloc] init];
preview.dataSource = self;
[self dismissViewControllerAnimated:YES completion:^{
[self presentViewController:preview animated:YES completion:nil];
}];
The thread on the iPhone never makes it to this line
[self presentViewController:preview animated:YES completion:nil];
but works fine on ipad.. I am not sure what to even look at. Any help would be appreaciated.
To access the instances/variables (that are declared outside of the block) inside a block, you need to declare those instances/variables like this:
__block type identifier = initial value (optional) e.g, in your case use
__block QLPreviewController* preview = [[QLPreviewController alloc] init];
Try to use
[self.presentingViewController presentViewController:preview animated:YES completion:nil];
instead of
[self presentViewController:preview animated:YES completion:nil];

dismissViewControllerAnimated prevents new UIViewController from showing

I am trying to replace one UIViewController with another, However I have ecountered a problem.
If I write this
[self dismissViewControllerAnimated:NO completion:nil];
//load currentProjectListViewController
currentProjectListViewController = [[CurrentProjectListViewController alloc] initWithNibName:#"CurrentProjectListViewController" bundle:nil];
[self presentViewController:currentProjectListViewController animated:NO completion:nil];
This almost works, however the view just blinks and nothing happens.. no new view is loaded or anything, I have put a break point inside currentProjectListViewController and the thread never makes it there.
however if I do this.
//load currentProjectListViewController
currentProjectListViewController = [[CurrentProjectListViewController alloc] initWithNibName:#"CurrentProjectListViewController" bundle:nil];
[self presentViewController:currentProjectListViewController animated:NO completion:nil];
currentProjectListViewController loads perfectly fine. however I am worried about whats hapening with the previous view? is it stuck there in memory? or is it gone?
my question is how can I dismiss it from memory as well as site without stopping my next view from appearing.
any help would be greatly appreciated.
No, the second way is the correct way as far as I see it. In the first method you're asking the VC to dismiss before a new VC is presented. This would be there is no view, which wouldn't happen.
Bu presenting a new VC, the old VC won't be retained in memory as the nature of the view is that it only uses memory when it is in view. I hope this make makes sense.
Try presenting the new view controller in the completion handler of the dismiss method call:
typeof(self) __weak weakSelf = self; //Need to have a weak reference to self to prevent retain cycle.
[self dismissViewControllerAnimated:NO completion:^{
currentProjectListViewController = [[CurrentProjectListViewController alloc] initWithNibName:#"CurrentProjectListViewController" bundle:nil];
[weakSelf presentViewController:currentProjectListViewController animated:NO completion:nil];
}];

iOS 6 force an orientation

In iOS5 you could use this snippet to force the orientation:
UIViewController *c = [[UIViewController alloc]init];
[self presentModalViewController:c animated:NO];
[self dismissModalViewControllerAnimated:NO];
[c release];
However this causes an EXC_BAD_ACCESS in iOS6. How can a certain orientation be forced in iOS6?
Just to complete the previous answer, you should do this:
UIViewController *viewController = [[UIViewController alloc] init];
[self presentViewController:viewController animated:NO completion:^{
[viewController dismissModalViewControllerAnimated:NO];
}];
And iOS 6 is no longer under NDA.
In case anyone still cares about this, here's the iOS6 code snippet (I placed it in my viewDidLoad routine):
UIViewController *viewController = [[UIViewController alloc] init];
viewController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentViewController:viewController animated:NO completion:^{
[self dismissViewControllerAnimated:NO completion:nil];
}];
At first presentModalViewController and dismissModalViewControllerAnimated are deprecated and probably iOS6 will not use these methods correctly. You should use similar methods with complition block instead.
The second thing is that [self dismissModalViewControllerAnimated:NO]; tries to dismiss self firstly. Is this correct in your case?
And last thing iOS6 is under NDA

Memory not released when popViewController Called

Below is the code in which i have some doubts:-
MyController *vc= [MyViewController alloc] initWithNibName:#"myController"
bundle:nil];
[self.navigationController pushViewController:vc animated:YES];
[vc release];
Then, i pop my controller by pressing the back button on nav bar.
The problem is memory is increased by 5mb(On Activity Monitor) for the first time.And when i pop the controller it doesnt get released.
And when i do the push and pop again and again the memory increases by small amount and decreased too.
UIView *myView=[UIView alloc]init];
self.vi=myView;
[myView release];
UIScrollView *mySv=[UIScrollView alloc]init];
self.sv=mySv;
[mySvrelease];
UIProgressView*myPv=[UIProgressViewalloc]init];
self.pv=myPv;
[myPvrelease];
UIWebView *myWv=[UIWebView alloc]init];
self.wv=myWv;
[myWv release];
-(void)dealloc
{
[wv relase];
[sv release]
[pv release];
[vi release];
[super dealloc];
}
wv,sv,pv,vi are MyViewControoler variables which have retain attribute.
I wrote this code to check the memory management concepts,but iam confused now seeing the activity monitor and instruments results.
I have verified that no object is getting leaked in my MyController class by using Instruments on it.
MyViewController have a content which does a leaks
Its not a memory leak. iOS cache the controllers you have visited recently. It will get deallocated by iOS itself when your application needs memory to execute some other tasks.
try this method in MyViewController.m file
- (void)dealloc
{
//release any object thats retained into the memory
[super dealloc];
}

Resources