简体   繁体   English

重新分配指针会导致内存泄漏?

[英]will reassign a pointer cause memory leak?

I have the following code and wondering what will happen when iOS is recollecting the allocated memories. 我有以下代码,想知道当iOS重新收集分配的内存时会发生什么。 Will the memory p1 pointed to get autoreleased afterwards, although it's pointing to a different memory now? 尽管现在指向另一个内存,但p1指向的内存会在以后自动释放吗? And also will the memory p2 pointed to get autoreleased as well, as p1 is semantically meant to point to autoreleased memory at the beginning? 并且p2指向的内存也将自动释放,因为p1的语义是在开始时指向自动释放的内存?

NSMutableArray *p1 = [NSMutableArray arrayWithCapacity:10];
NSMutableArray *p2 = [[NSMutableArray alloc] init];

// what will happen to the memory p1 and p2 point to
// after the following assignment, and at later stage?
p1 = p2;

Nothing will be leaked, p1 is an autoreleased object, in that scenario, after you assign p2 to p1, both of them simply point to the same object. 没有泄漏,p1是一个自动释放的对象,在这种情况下,将p2分配给p1后,它们都只是指向同一对象。 Original object that p1 was pointing to, is managed by autorelease pool and will be released when the pool is drained. p1指向的原始对象由自动释放池管理,并且在该池耗尽时将被释放。 ARC or not, there is no leak assuming that you release p2 later. 不管是否为ARC,假设以后再发布p2都不会泄漏。

Under ARC, you won't need to do anything Both objects will be released. 在ARC下,您无需执行任何操作。两个对象都将被释放。 Outside of ARC: 在ARC之外:

NSMutableArray *p1 = [NSMutableArray arrayWithCapacity:10];

returns an autoreleaed value. 返回一个自动释放的值。 You don't need to release this value. 您不需要释放此值。 However, this: 但是,这个:

NSMutableArray *p2 = [[NSMutableArray alloc] init];

has a manual retain count of 1. You will need to release the value of p2 (which is then assigned to p1) when you are done with it. 手动保留计数为1。完成后,您将需要释放p2的值(然后将其分配给p1)。

NSMutableArray *p1 = [NSMutableArray arrayWithCapacity:10];
NSMutableArray *p2 = [[NSMutableArray alloc] init];

// what will happen to the memory p1 and p2 point to
// after the following assignment, and at later stage?
p1 = p2;

" iOS is recollecting the allocated memories" - iOS never had Garbage Collection feature. “ iOS正在收集分配的内存”-iOS从未具有垃圾回收功能。

p1 is pointer pointing to autoreleased object. p1是指向自动释放对象的指针。

p2 is pointer pointing to non-autoreleased object (you or ARC has to release it later to avoid memory leak) p2是指向非自动释放对象的指针(您或ARC必须稍后释放它以避免内存泄漏)

After p1 = p2, p1 points to non-autoreleased object that p2 is pointing to. 在p1 = p2之后,p1指向p2指向的非自动释放对象。 Original object p1 was pointing to is orphaned at this point. 此时指向原始对象p1是孤立的。

If you are using ARC feature, you don't have to do anything. 如果使用的是ARC功能,则无需执行任何操作。 Under Manual memory management environment you do [p2 release]; 在“手动内存管理”环境下,执行[p2 release]; or [p1 release]; [p1 release]; to balance releasing of allocated object that you own. 平衡释放您拥有的已分配对象。

By the way, if you ever have questions like this in the future, it's easily tested by (a) subclassing the class in question; 顺便说一句,如果您将来有这样的问题,可以通过(a)将该问题的子类化而轻松地进行测试。 and (b) adding your own dealloc that logs the deallocation. (b)添加您自己的记录下解除分配的dealloc For example: 例如:

@interface TestNSMutableArray : NSMutableArray
@end

@implementation TestNSMutableArray
- (void)dealloc
{
    [super dealloc]; // only needed for non-ARC
    NSLog("%s", __FUNCTION__);
}
@end

Then try your code using TestNSMutableArray rather than NSMutableArray . 然后使用TestNSMutableArray而不是NSMutableArray尝试代码。 If you see your dealloc , you're good. 如果看到您的dealloc ,那就很好。 If not, you're leaking. 如果没有,那是在泄漏。

Clearly you can also use Instruments, but when I was first getting my hands around Objective-C memory handling, I found this to be a simple, though useful, diagnostic technique. 显然,您也可以使用Instruments,但是当我第一次接触Objective-C内存处理时,我发现这是一种简单但有用的诊断技术。

First, this depends on if you are using ARC. 首先,这取决于您是否使用ARC。

Using ARC: There will be no memory leaks. 使用ARC:不会有内存泄漏。 The OS will release the memory for p1 and the array will now point to the same location as p2. 操作系统将释放p1的内存,并且阵列现在将指向与p2相同的位置。 Then, the OS will release the memory for both arrays when p1 and p2 go away (such as when the class is deallocated). 然后,当p1和p2消失时(例如,当释放该类时),操作系统将释放两个阵列的内存。

No ARC: p1 will be leaked. 没有ARC:p1将泄漏。 To fix the leak you will need 要修复泄漏,您将需要

[p1 release];

before assigning the value of p2 to p1. 在将p2的值分配给p1之前。 If you do not call release on both of these arrays before the class is deallocated, the memory from p2 will be leaked also. 如果在释放该类之前不对这两个数组都调用release,则p2的内存也会泄漏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM