使用XCode Clang(静态分析器)发现内存泄露

简介: XCode中引入了静态分析器,用于发现普通编译错误以外的错误选择Build->Build and Analyze请看下面这段代码#import int main(int agrc, const char *argv[]){    NSAutoreleasePool *pool ...

XCode中引入了静态分析器,用于发现普通编译错误以外的错误

选择Build->Build and Analyze

请看下面这段代码

#import <Foundation/FOundation.h>

int main( int agrc,  const  char *argv[])
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    NSDate *date = [[NSDate alloc] init];
    NSLog( @" The time is: %@ ", date);
    
    [pool drain];
     return  0;
}

 

上面这段代码中, date对象在创建后没有被释放

所以, 我们应该加上[date release]

正确的代码

#import <Foundation/FOundation.h>

int main( int agrc,  const  char *argv[])
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    NSDate *date = [[NSDate alloc] init];
    NSLog( @" The time is: %@ ", date);
    [date release];
    [pool drain];
     return  0;
}

 

 

目录
相关文章
|
iOS开发
[✔️]xcode Instrucments排查app的内存泄露
[✔️]xcode Instrucments排查app的内存泄露
1234 0
|
iOS开发 C++
使用Xcode和Instruments调试解决iOS内存泄露
来源:http://blog.csdn.net/totogo2010/article/details/8233565 虽然iOS 5.0版本之后加入了ARC机制,由于相互引用关系比较复杂时,内存泄露还是可能存在。
1289 0
|
jenkins Unix 持续交付
个人记录jenkins编译ios过程 xcode是9.4.1
个人记录jenkins编译ios过程 xcode是9.4.1
438 2
|
Linux 数据安全/隐私保护 iOS开发
如何使用 Xcode 打包导出 IPA 文件并进行 iOS 应用内测,无需支付苹果开发者账号费用?
如何使用 Xcode 打包导出 IPA 文件并进行 iOS 应用内测,无需支付苹果开发者账号费用?
|
iOS开发 MacOS Perl
解决Xcode运行IOS报错:redefinition of module ‘Firebase‘和could not build module ‘CoreFoundation‘
解决Xcode运行IOS报错:redefinition of module ‘Firebase‘和could not build module ‘CoreFoundation‘
1073 4
|
iOS开发 开发者
解决xcode doesn‘t support iphone’s ios 14.6 (18f72)
解决xcode doesn‘t support iphone’s ios 14.6 (18f72)
1057 3
|
iOS开发
mac不通过Xcode直接打开IOS模拟器
mac不通过Xcode直接打开IOS模拟器
1256 24
|
iOS开发
iOS Xcode 意外退出 打不开工程
iOS Xcode 意外退出 打不开工程
480 0
|
iOS开发
技术好文:xcode动态图,ios实现动态图,iosgif,暂停和继续播放
技术好文:xcode动态图,ios实现动态图,iosgif,暂停和继续播放
358 24