Reputation: 2277
I think there is something i did not understand in the memory management in xcode and when to or not to release objects to avoid memory leaks. I have been reading this presentation, but since there is no audio i dont understand all the sides: http://www.slideshare.net/OwenGoss/finding-and-fixing-memory-leaks-in-ios-apps-5251292
Here is a very simple code of my app that is the issue:
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
NSString *myBundleName = [[NSString alloc] init];
NSString *myBundleVersion = [[NSString alloc] init];
NSString *myBundleBuild = [[NSString alloc] init];
NSString *myIosName = [[NSString alloc] init];
NSString *myIosVersion = [[NSString alloc] init];
myBundleName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleName"];
myBundleVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
myBundleBuild = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"];
myIosName = [[UIDevice currentDevice] systemName ];
myIosVersion = [[UIDevice currentDevice] systemVersion];
self.versionBuildLabel.text = [NSString stringWithFormat:@"%@ version %@ build %@ on %@ %@", myBundleName, myBundleVersion, myBundleBuild, myIosName, myIosVersion];
[myBundleName release];
[myBundleVersion release];
[myBundleBuild release];
[myIosName release];
[myIosVersion release];
}
If I try to run this
[myBundleName release];
[myBundleVersion release];
[myBundleBuild release];
[myIosName release];
[myIosVersion release];
Then the application crashes with
[Session started at 2010-12-02 14:08:47 +0700.]
GNU gdb 6.3.50-20050815 (Apple version gdb-1472) (Wed Jul 21 10:53:12 UTC 2010)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "x86_64-apple-darwin".
sharedlibrary apply-load-rules all
Attaching to process 26707.
Pending breakpoint 1 - ""HomeVC.m":49" resolved
(gdb) continue
Current language: auto; currently objective-c
[Switching to process 26707]
[Switching to process 26707]
Program received signal: “EXC_BAD_ACCESS”.
(gdb)
I thought i was doing the right thing by releasing intermediate variables used to make my code easier to understand.
What is the problem here?
NB: I face similar problems in other portion of my code, but this is the simplest example that causes me problems, for which there is probably a notion i did not understand.
Upvotes: 0
Views: 1520
Reputation: 723809
When you assign your variables with items from [UIDevice currentDevice]
and [NSBundle mainBundle]
, those objects come autoreleased. Since they're autoreleased, iOS automatically handles memory management of those objects for you. That's why your app crashes when you try to manually release
them.
Don't initialize your NSString
s to new objects (i.e. [[NSString alloc] init]
). Initialize them with the convenience methods right away:
NSString *myBundleName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleName"];
NSString *myBundleVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
NSString *myBundleBuild = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"];
NSString *myIosName = [[UIDevice currentDevice] systemName];
NSString *myIosVersion = [[UIDevice currentDevice] systemVersion];
And don't call release
on any of them. This way your method never has ownership of these objects; they just happen to be passed to it for use.
Upvotes: 4