在[[NSString alloc] init]之后发送释放导致EXIT_BAD_ACCESS

时间:2010-12-02 07:18:42

标签: ios objective-c memory-management memory-leaks

我认为在xcode的内存管理中有一些我不理解的东西以及何时发布或不发布对象以避免内存泄漏。我一直在阅读这个演讲,但由于没有音频,我不理解所有方面:http://www.slideshare.net/OwenGoss/finding-and-fixing-memory-leaks-in-ios-apps-5251292

以下是我的应用程序的一个非常简单的问题:

// 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];

}

如果我尝试运行此

 [myBundleName release];
 [myBundleVersion release];
 [myBundleBuild release];
 [myIosName release];
 [myIosVersion release];

然后应用程序崩溃

[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) 

我认为我通过发布用于使代码更容易理解的中间变量来做正确的事情。

这里有什么问题?

注意:我在我的代码的其他部分遇到类似的问题,但这是导致我出现问题的最简单的例子,可能有一个我不理解的概念。

1 个答案:

答案 0 :(得分:4)

当您使用[UIDevice currentDevice][NSBundle mainBundle]中的项目分配变量时,这些对象会自动释放。由于它们是自动释放的,因此iOS会自动为您处理这些对象的内存管理。这就是当您尝试手动release时,您的应用崩溃的原因。

请勿将NSString初始化为新对象(即[[NSString alloc] init])。立即用方便的方法初始化它们:

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];

不要在其中任何一个上致电release。这样,您的方法永远不会拥有这些对象;他们恰好被传递给它使用。

相关问题