ios - Sending release after [[NSString alloc] init] causes EXIT_BAD_ACCESS -
i think there did not understand in memory management in xcode , when or not release objects avoid memory leaks. have been reading presentation, since there no audio dont understand sides: http://www.slideshare.net/owengoss/finding-and-fixing-memory-leaks-in-ios-apps-5251292
here simple code of app issue:
// implement viewdidload additional setup after loading view, typically 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 try run
[mybundlename release]; [mybundleversion release]; [mybundlebuild release]; [myiosname release]; [myiosversion release];
then application crashes with
[session started @ 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 free software, covered gnu general public license, , welcome change and/or distribute copies of under conditions. type "show copying" see conditions. there absolutely no warranty gdb. type "show warranty" details. gdb configured "x86_64-apple-darwin". sharedlibrary apply-load-rules attaching process 26707. pending breakpoint 1 - ""homevc.m":49" resolved (gdb) continue current language: auto; objective-c [switching process 26707] [switching process 26707] program received signal: “exc_bad_access”. (gdb)
i thought doing right thing releasing intermediate variables used make code easier understand.
what problem here?
nb: face similar problems in other portion of code, simplest example causes me problems, there notion did not understand.
when assign variables items [uidevice currentdevice]
, [nsbundle mainbundle]
, objects come autoreleased. since they're autoreleased, ios automatically handles memory management of objects you. that's why app crashes when try manually release
them.
don't initialize nsstring
s new objects (i.e. [[nsstring alloc] init]
). initialize them 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 of them. way method never has ownership of these objects; happen passed use.
Comments
Post a Comment