Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development > iPhone SDK Development - Advanced Discussion

Reply
 
LinkBack Thread Tools Display Modes
Old 06-19-2011, 01:03 PM   #1 (permalink)
Registered Member
 
Join Date: Mar 2011
Posts: 76
ae262 is on a distinguished road
Default No leaks but memory problem

Hi,

My app works fine on the simulator but it crashes on a device after playing with it for some time. I get an error message regarding memory a few times at Level 1 and then sometime at level 2 before crashing.

Received memory warning. Level=1

I used instruments to investigate and found that there were no leaks in my program but the memory allocation keeps going up. I am using multiple view controllers and as I move from one view controller, I never see the memory allocation go down. I tested by adding an empty view controller with only one back button and when I went back and forth between the main view and this empty view it crashed after a certain number of clicks.

I am attaching a picture of the analysis from instruments.

I have tried many things. I am using UIImage view and I have replaced the imageNamed method to withContentOfFile method but that didn't solve the problem.

Very frustrated with this memory issue. What is the next step in troubleshooting this?

Many thanks.
N
Attached Images
File Type: jpg Screen shot 2011-06-18 at 7.57.45 PM.jpg (20.8 KB, 26 views)
ae262 is offline   Reply With Quote
Old 06-20-2011, 01:39 AM   #2 (permalink)
Nuisance Developer
 
Join Date: Jul 2009
Location: Italy
Posts: 4,691
dany_dev is on a distinguished road
Default

the image attachments doesn't work on the forum, use imageshack.us

However i think there are leaks
__________________
dany_dev is offline   Reply With Quote
Old 06-22-2011, 07:44 PM   #3 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,002
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by ae262 View Post
Hi,

My app works fine on the simulator but it crashes on a device after playing with it for some time. I get an error message regarding memory a few times at Level 1 and then sometime at level 2 before crashing.

Received memory warning. Level=1

I used instruments to investigate and found that there were no leaks in my program but the memory allocation keeps going up. I am using multiple view controllers and as I move from one view controller, I never see the memory allocation go down. I tested by adding an empty view controller with only one back button and when I went back and forth between the main view and this empty view it crashed after a certain number of clicks.

I am attaching a picture of the analysis from instruments.

I have tried many things. I am using UIImage view and I have replaced the imageNamed method to withContentOfFile method but that didn't solve the problem.

Very frustrated with this memory issue. What is the next step in troubleshooting this?

Many thanks.
N
Have you cleaned your code and run build and analyze on it?

The memory profile you posted (I could see it for once!) does suggest a memory leak to me.

Note that you can do things like add pointers to objects to an NSArray that prevents them from being released. That doesn't meet the strict definition of a leak, but it does mean that you don't release things when you are done with them. I would suggest looking at the allocations tool and looking at the types of objects that you are creating, by number and by size. That should let you figure out what sorts of objects are being created and filling up memory. Then if the automated tools can't find anything, you'll need to wade through your code yourself. Do small tests where you exercise a bit of your app, then close it and go back to your starting point.

One question: Does the memory use climb on the simulator like it does on the device? If so, you can still use the simulator to track down the problem. Run on the simulator with the allocs instrument and watch total allocations. Do simple things like opening a new view controller and then closing it, then forcing a memory warning. After a memory warning, your app should revert it's previous memory use.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 10-20-2011, 10:24 PM   #4 (permalink)
Registered Member
 
Join Date: Jan 2009
Posts: 113
jonusx is on a distinguished road
Default

Use the allocations instrument to track down where memory is being left hanging.
__________________
What do you have to share? Share and view pics Oneshare

Play Magic: The Gathering? Try The Sylvan Archives!

Warhammer 40k your thing? W40k Manager is your thing.

Like karaoke? Try iSing Karaoke Locator
jonusx is offline   Reply With Quote
Old 02-18-2012, 03:07 PM   #5 (permalink)
Registered Member
 
Join Date: Dec 2010
Posts: 30
denin is on a distinguished road
Default

Hello,

I have a tab bar controller with 4 tabs. All the tabs have navigation controllers. My application is full of views. I am having exact the same behavior in the allocations. It is accumulating and building up and I didn’t know that this suggests memory leak. I am now trying to fix the issues in my application.

I would like to ask you guys some questions. I hope you can help me with them. I know this is bit too long but I think you can answer them quickly.

1. First of all I would like to learn how the allocation graph should look like. I assume it should be moving up and down rather than building up?? Is this right??

2. Duncan, you said we can trace the leaking variables by checking them out from the allocation tool. I have names like Malloc 16 Bytes, Malloc 32 Bytes, CFString (is this NSString?), CFBasicHash(value-store), CFUtilities, CFArray, _NSArrayI and many more. How can we use these to determine the leaking variables?

3.
Code:
NSString *path = [[NSBundle mainBundle] pathForResource:@"File" ofType:@"plist"];
NSLog(@"path1 %d", [path retainCount]);
NSArray *array = [[NSArray alloc] initWithContentsOfFile:path];
path = nil;
//[path release];
NSLog(@"path2 %d", [path retainCount]);
Initially I tried to release "path" which I knew was wrong. When I Build and Analyze it gave error:

"Incorrect decrement of the reference count of an object that is not owned at this point by the caller
1. Method returns an Objective-C object with a +0 retain count (non-owning reference)
2 Incorrect decrement of the reference count of an object that is not owned at this point by the caller"


However, when I check its retain count right after its declaration and initialization path has a retain count of 1. Then I tried path = nil; and checked the retain count, it returned 0. I should always have the retain count to be 0 when I am done with variable?? Is it right to use nil here?

4. I want to ask kind of the same question here.
Code:
	NSArray *array = [[NSArray alloc] initWithContentsOfFile:path];
	NSLog(@"array1 %d", [array retainCount]);

	self.content = array;
	NSLog(@"qi array2 %d", [array retainCount]);
	NSLog(@"content1 %d", [content retainCount]);

	[array release];
	NSLog(@"qi array3 %d", [array retainCount]);
	NSLog(@"content2 %d", [content retainCount]);

        array=nil;
	NSLog(@"qi array4 %d", [array retainCount]);
	NSLog(@"content1 %d", [content retainCount]);
After the declaration and initialization of array the retain count is 1. After self.content = array both array and content have retain count of 2. After [array release] array retain count is 1 and so is self.content's.After array = nil; array returns 0 and content returns 1. Is this how it should be??

5. In a navigation controller, should I load the views in -tableView: didSelectRowAtIndexPath according to the selected row. OR should I load every view to an array of views in the viewDidLoad and the extract the desired one in -tableView: didSelectRowAtIndexPath.

6.
Code:
	UIViewController *childViewController = [self.controllers objectAtIndex:indexPath.row];
	NSLog(@"child1:%d",[childViewController retainCount]);

	[self.navigationController pushViewController:childViewController animated:YES];
	NSLog(@"child2:%d",[childViewController retainCount]);

	childViewController = nil;
	NSLog(@"child3:%d",[childViewController retainCount]);
child1, child2 and child3 are 1,5 and 0 respectively. Why is the retainCount 5 after the pushViewController method? Is this something usual or is there mistake I am missing? Is it right to use nil??

My concern is setting it to "nil" does not call the dealloc method by "release" does. When the retain count is 5, setting it to nil causes a leak? Logically the release should be called on the object 5 times to release all the instance variables?? Is this right??

I will strongly appreciate if anyone please gives me a hand.

7. The FirstLevelViewControllers of the navigation controllers of the tabs are never released. right?
denin is offline   Reply With Quote
Old 02-21-2012, 04:34 PM   #6 (permalink)
Registered Member
 
Join Date: Dec 2010
Posts: 30
denin is on a distinguished road
Default

Would anyone please help? I am confused and kind of stuck.

Thanks
denin is offline   Reply With Quote
Old 02-21-2012, 05:36 PM   #7 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,002
Duncan C has a spectacular aura about
Default

From the questions you're asking, it's pretty clear you don't understand how to handle memory management in Cocoa. You need to stop and read the memory management programming guide in the Xcode docs. Do a search on "Memory Management Policy", and read the article with that title that comes up.

Quote:
Originally Posted by denin View Post
Hello,

I have a tab bar controller with 4 tabs. All the tabs have navigation controllers. My application is full of views. I am having exact the same behavior in the allocations. It is accumulating and building up and I didn’t know that this suggests memory leak. I am now trying to fix the issues in my application.

I would like to ask you guys some questions. I hope you can help me with them. I know this is bit too long but I think you can answer them quickly.

1. First of all I would like to learn how the allocation graph should look like. I assume it should be moving up and down rather than building up?? Is this right??
Right. Unless you are doing something that adds more content to your program (like recording more of your voice into a voice recorder, or adding text to a word processor) your app's memory should bounce around, but not increase steadily.

Quote:

2. Duncan, you said we can trace the leaking variables by checking them out from the allocation tool. I have names like Malloc 16 Bytes, Malloc 32 Bytes, CFString (is this NSString?), CFBasicHash(value-store), CFUtilities, CFArray, _NSArrayI and many more. How can we use these to determine the leaking variables?
I'm not an expert at using instruments. I'm ok at using it, but don't know how to describe it. I'd suggest watching the videos from Apple's developer conference on the subject. I think they covered it in 2010?
Quote:

3.
Code:
NSString *path = [[NSBundle mainBundle] pathForResource:@"File" ofType:@"plist"];
NSLog(@"path1 %d", [path retainCount]);
NSArray *array = [[NSArray alloc] initWithContentsOfFile:path];
path = nil;
//[path release];
NSLog(@"path2 %d", [path retainCount]);
Initially I tried to release "path" which I knew was wrong. When I Build and Analyze it gave error:

"Incorrect decrement of the reference count of an object that is not owned at this point by the caller
1. Method returns an Objective-C object with a +0 retain count (non-owning reference)
2 Incorrect decrement of the reference count of an object that is not owned at this point by the caller"

Read the docs I referenced. You should only release objects that you create using a method whose name begins with “alloc”, “new”, “copy”, or “mutableCopy. All other methods return objects that you do not own, and should not release.
Quote:

However, when I check its retain count right after its declaration and initialization path has a retain count of 1.
You can't get meaningful information from the retain count on objects. If an object is autoreleased, it could have a positive retain count, but still be released in the near future. Learn the memory management rules and follow them consistently.
Quote:

Then I tried path = nil; and checked the retain count, it returned 0. I should always have the retain count to be 0 when I am done with variable?? Is it right to use nil here?
Think about that for a second.

If you set path to nil, how can you check the retain count after? You don't have a pointer to the path any more.

The code

Code:
path = nil;
int retainCount = [path retaincount];
is the same as
Code:
[nil retaincount];
It doesn't do anything useful.
Quote:

4. I want to ask kind of the same question here.
Code:
	NSArray *array = [[NSArray alloc] initWithContentsOfFile:path];
	NSLog(@"array1 %d", [array retainCount]);

	self.content = array;
	NSLog(@"qi array2 %d", [array retainCount]);
	NSLog(@"content1 %d", [content retainCount]);

	[array release];
	NSLog(@"qi array3 %d", [array retainCount]);
	NSLog(@"content2 %d", [content retainCount]);

        array=nil;
	NSLog(@"qi array4 %d", [array retainCount]);
	NSLog(@"content1 %d", [content retainCount]);
After the declaration and initialization of array the retain count is 1. After self.content = array both array and content have retain count of 2. After [array release] array retain count is 1 and so is self.content's.After array = nil; array returns 0 and content returns 1. Is this how it should be??

5. In a navigation controller, should I load the views in -tableView: didSelectRowAtIndexPath according to the selected row. OR should I load every view to an array of views in the viewDidLoad and the extract the desired one in -tableView: didSelectRowAtIndexPath.

6.
Code:
	UIViewController *childViewController = [self.controllers objectAtIndex:indexPath.row];
	NSLog(@"child1:%d",[childViewController retainCount]);

	[self.navigationController pushViewController:childViewController animated:YES];
	NSLog(@"child2:%d",[childViewController retainCount]);

	childViewController = nil;
	NSLog(@"child3:%d",[childViewController retainCount]);
child1, child2 and child3 are 1,5 and 0 respectively. Why is the retainCount 5 after the pushViewController method? Is this something usual or is there mistake I am missing? Is it right to use nil??

My concern is setting it to "nil" does not call the dealloc method by "release" does. When the retain count is 5, setting it to nil causes a leak? Logically the release should be called on the object 5 times to release all the instance variables?? Is this right??

I will strongly appreciate if anyone please gives me a hand.

7. The FirstLevelViewControllers of the navigation controllers of the tabs are never released. right?

Setting a variable to nil has no effect on it's retain count. It just sets your local pointer to nil. Then, when you try to check the retain count, you check a nil's retain count, which always returns zero.

As I say, don't try to read retain counts. It will only confuse you. Learn the memory management rules and follow them, and you'll be okay.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 02-24-2012, 12:57 PM   #8 (permalink)
Registered Member
 
Join Date: Dec 2010
Posts: 30
denin is on a distinguished road
Default

Thanks very much Duncan, I went through it and corrected the leaks in my code. It was useful indeed.

Here:

https://developer.apple.com/library/...MemoryMgmt.pdf

in case anyone needs it.

Thanks
denin is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 388
17 members and 371 guests
Absentia, AyClass, Diligent, dre, givensur, jbro, jPuzzle, momolgtm, Newbie123, Paul10, Punkjumper, revg, sacha1996, skrew88, taylor202, tomtom100
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,643
Threads: 94,110
Posts: 402,858
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Diligent
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 04:41 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0