仪器指向内存泄漏!
嗨,我有这个代码用于初始化我的类。
- (id)initWithSize:(int)size {
self = [super init];
if( self != nil )
{
[self setMyVector:[[NSMutableArray alloc] initWithCapacity:size]];
for ( int i = 0; i < size; i++ )
{
[myVector addObject: [[NSMutableArray alloc] initWithCapacity:size]];
}
}
return self;
}
我在 Instruments 中遇到了这个泄漏!
类别:CFArray(存储双端队列)
事件类型:Malloc
有人知道我需要什么来解决它吗? 谢谢!
Hi, i'm have this code for initialize my class.
- (id)initWithSize:(int)size {
self = [super init];
if( self != nil )
{
[self setMyVector:[[NSMutableArray alloc] initWithCapacity:size]];
for ( int i = 0; i < size; i++ )
{
[myVector addObject: [[NSMutableArray alloc] initWithCapacity:size]];
}
}
return self;
}
I'm get this leak in Instruments !
Category: CFArray (store-deque)
Event Type: Malloc
Anyone know what i need to resolve it ?
Thanks !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
泄漏发生在您的 for 循环中,也可能发生在之前。
首先,这一行:
[self setMyVector:[[NSMutableArray alloc] initWithCapacity:size]];
如果
setMyVector
保留传递的数组(根据约定,它可能应该),那么你就泄露了数组。首先分配并初始化数组。它的保留计数为+1。然后将其设置到 myVector 实例变量中,这意味着它的保留计数为 +2。当您将其他内容放入 myVector 中,或者在dealloc
方法中释放 myVector 变量时,您将减少保留计数,这意味着它将有 +1 的保留计数。换句话说,它不会被释放,并且您已经泄漏了数组。另一个明确的泄漏是在 for() 循环内,其中有:
[myVector addObject: [[NSMutableArray alloc] initWithCapacity:size]];
再次,您创建了一个保留计数为+1,然后将其添加到 myVector 数组中,该数组将再次保留它 (+2)。但是,您不再拥有指向该数组的指针,因此您已经泄漏了它。
这两个泄漏都可以通过使用 [NSMutableArray arrayWithCapacity:size] 来解决,而不是使用 alloc/init 方法。这将创建一个自动释放的数组,这将解决您的内存泄漏问题。
如果您不知道什么是 autoreleasing,那么您可能需要考虑创建数组并将其存储到局部变量中,将其添加到 myVector (或将其设置到 myVector 中),然后立即释放它。
The leak is in your for loop, and possibly also one before.
First, this line:
[self setMyVector:[[NSMutableArray alloc] initWithCapacity:size]];
If
setMyVector
retains the passed array (which, according to convention, it probably should), then you've leaked the array. First you alloc and init the array. It has a retain count of +1. Then you set it into the myVector instance variable, which means it has a retain count of +2. When you put something else into myVector, or you release the myVector variable in yourdealloc
method, you're going to decrement the retain count, which means it'll have a retain count of +1. In other words, it won't be deallocated, and you've leaked the array.The other, definite leak is inside your for() loop, where you have:
[myVector addObject: [[NSMutableArray alloc] initWithCapacity:size]];
Again, you create an array with a retain count of +1, then you add it to the myVector array, which will retain it again (+2). However, you no longer have a pointer to the array, so you've leaked it.
Both of these leaks can be solved by using
[NSMutableArray arrayWithCapacity:size]
instead of the alloc/init approach. This will create an autoreleased array, which will solve your memory leak.If you don't know what autoreleasing is, then you may want to consider creating the array and storing it into a local variable, adding it to myVector (or setting it into myVector), and then immediately releasing it afterwards.