检查内存泄漏
您好,请告诉我我的代码中是否存在内存泄漏..
我正在解决我的应用程序中的内存泄漏问题,但在这里感到困惑,它会造成内存泄漏吗?
NSMutableArray *dataArray=[[NSMutableArray alloc]init];
if(condition)
{
[dataArray addObject:[appDelegate selectFromDatabase:x]];//returning an autoreleased array
}
else
{
dataArray=[appDelegate selectFromDatabase:a];
}
//use dataArray
[dataArray release];
Hi please tell me if there is any memory leak in my code or not..
I am solving my memory leaks in my app but confused here will it create a memory leak?
NSMutableArray *dataArray=[[NSMutableArray alloc]init];
if(condition)
{
[dataArray addObject:[appDelegate selectFromDatabase:x]];//returning an autoreleased array
}
else
{
dataArray=[appDelegate selectFromDatabase:a];
}
//use dataArray
[dataArray release];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
此代码会泄漏内存。您重新分配 dataArray 而不释放旧值。通过生成的属性进行赋值,旧值将首先被释放。
您还可以重组代码以避免创建数组,除非您确实需要它:
This code will leak memory. You reassign dataArray without releasing the old value. Do the assignment through a generated property and the old value will be released first.
You could also restructure your code to avoid creating the array unless you really need it:
您可以使用 Instruments 来帮助您搜索内存泄漏。
http://www.cimgf。 com/2008/04/02/cocoa-tutorial-fixing-memory-leaks-with-instruments/
You can use Instruments to help you search for memory leaks.
http://www.cimgf.com/2008/04/02/cocoa-tutorial-fixing-memory-leaks-with-instruments/