使用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的内存泄露
1030 0
|
jenkins Unix 持续交付
个人记录jenkins编译ios过程 xcode是9.4.1
个人记录jenkins编译ios过程 xcode是9.4.1
364 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‘
867 4
|
iOS开发 开发者
解决xcode doesn‘t support iphone’s ios 14.6 (18f72)
解决xcode doesn‘t support iphone’s ios 14.6 (18f72)
871 3
|
iOS开发
mac不通过Xcode直接打开IOS模拟器
mac不通过Xcode直接打开IOS模拟器
882 24
|
iOS开发
技术好文:xcode动态图,ios实现动态图,iosgif,暂停和继续播放
技术好文:xcode动态图,ios实现动态图,iosgif,暂停和继续播放
267 24
|
缓存 iOS开发
如何在Xcode删除某个版本的IOS模拟器
如何在Xcode删除某个版本的IOS模拟器
1687 1
|
iOS开发
iOS Xcode 意外退出 打不开工程
iOS Xcode 意外退出 打不开工程
370 0