set a breakpoint in malloc_error_break to debug

本文探讨了Objective-C中自动释放池的使用方法,并通过一个简单的示例代码展示了不当使用自动释放池导致程序崩溃的问题。此外,还介绍了如何通过设置环境变量来启用调试功能,以便更好地追踪和解决内存管理方面的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转自:http://year-2002.blog.163.com/blog/static/2493812320118795838457/


NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]; 
NSData* data = [NSData dataWithBytes:"asklaskdxjgr" length:12];
[data release];
[pool release];

 autorelease pool 自动释放 ,我们又手工释放,所以crash掉了;

Here"s what we do:

Click on the "Targets" tab, open "Executables" and select the app (In XCode 2.0, double-click the executable in the file tree and select the arguments tab to enter environment variables). In the executable settings, add the following environment variables and set their values to "YES" (without the quotes):

 

NSDebugEnabled
NSZombieEnabled
MallocStackLogging

You may also want the following environment variable set to YES:

MallocStackLoggingNoCompact
/////////////////////////////
if you run under gdb, you may enter:

(gdb) shell malloc_history 3939 0xa4e10


以下例子可以查看变量有没有设置成功:

 if( getenv("NSZombieEnabled") || getenv("NSAutoreleaseFreedObjectCheckEnabled") ) {
       NSLog(@"NSZombieEnabled/NSAutoreleaseFreedObjectCheckEnabled enabled!"); 

   }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值