简体   繁体   English

如何发布C样式数组?

[英]How do I release C style arrays?

I have a QuantumClone class which has an array of CGPoint s. 我有一个QuantumClone类,它有一个CGPoint数组。 The single QuantumPilot object creates a QuantumClone at the beginning of each level. 单个QuantumPilot对象在每个级别的开头创建一个QuantumClone During the next level the QuantumPilot records its velocities to its QuantumClone . 在下一个级别, QuantumPilot将其速度记录到QuantumClone At the beginning of a new level the game loop runs this code 在新级别的开始,游戏循环运行此代码

QuantumClone *c = [[self.pilot clone] copy];
c.bulletDelegate = self;
c.weapon = self.pilot.weapon;
[self.clones addObject:c];

But eventually the game will be reset and each QuantumClone object in the clones NSMutableArray will be removed. 但最终游戏将被重置,克隆NSMutableArray中的每个QuantumClone对象都将被删除。

Am I leaking memory by assigning values to the CGPoint pastVelocities[4551] ? 我是否通过为CGPoint pastVelocities[4551]分配值来泄漏记忆?

How do I reset these? 我该如何重置这些? I can't release them since they are not Objective-C objects. 我不能释放它们,因为它们不是Objective-C对象。 Do I need to call C functions to release this memory? 我是否需要调用C函数来释放这个内存?

@interface QuantumClone : QuantumPilot <NSCopying> {
  CGPoint pastVelocities[4551];
}

- (id)copyWithZone:(NSZone *)zone {
    QuantumClone *c = [[[QuantumClone alloc] init] autorelease];
    c.weapon = self.weapon;
    for (NSInteger i = 0; i < 4551; i++) {
        [c recordVelocity:pastVelocities[i] firing:pastFireTimings[i]];
    }
    [c recordLatestIndex:timeIndex];
    return c;
}

- (void)recordVelocity:(CGPoint)vel firing:(BOOL)firing {
    CGPoint p = pastVelocities[timeIndex];
    p.x = vel.x;
    p.y = vel.y;
    pastVelocities[timeIndex] = p;
    bool fired = firing;
    pastFireTimings[timeIndex] = fired;
    timeIndex++;
}

@interface QuantumPilot : CCNode {}
....
@property (nonatomic, retain) QuantumClone *clone;

- (void)copyDeltas {
    [self.clone recordVelocity:ccp(self.vel.x, -self.vel.y) firing:self.firing];
}

- (void)createClone {
    self.clone = [[[QuantumClone alloc] init] autorelease];
    self.clone.active = YES;
    self.clone.weapon = self.weapon;
}

Am I leaking memory by assigning values to the CGPoint pastVelocities[4551] ? 我是否通过为CGPoint pastVelocities[4551]分配值来泄漏记忆?

Short answer : No. 简答 :不。

Long answer : The array in your code is a big chunk of contiguous memory where all CGRect s live in, and it has automatic storage, which means it will be allocated and deallocated automatically (when it goes out of scope). 答案很长 :代码中的数组是所有CGRect所在的大块连续内存,并且它具有自动存储功能,这意味着它将自动分配和释放(当它超出范围时)。 In other words, when its parent object is destroyed, the array will be gone along with those 4551 objects. 换句话说,当它的父对象被销毁时,该数组将与这些4551对象一起消失。

You can verify its size by printing the result of sizeof(pastVelocities) . 您可以通过打印sizeof(pastVelocities)的结果来验证其大小。 Dividing the result by sizeof(CGRect) will tell you how many objects of this type can be stored in it. 将结果除以sizeof(CGRect)将告诉您可以在其中存储多少个此类型的对象。

A deallocation must be married to an explicit allocation. 解除分配必须与明确的分配结合。 You only need to release memory that is allocated dynamically (explicitly), for example, using the alloc function family (malloc, calloc, realloc, etc). 您只需要释放动态(显式)分配的内存,例如,使用alloc函数系列(malloc,calloc,realloc等)。

How do I reset these? 我该如何重置这些?

memset(pastVelocities, 0, sizeof(pastVelocities));

This will reset the entire array. 这将重置整个阵列。

jweyrich beat me to it, but I'll go ahead and post this in case it helps ;) jweyrich打败了我,但是我会继续发布这个以防它有帮助;)

–– -

You aren't leaking. 你没有泄漏。 The runtime allocates enough memory to hold all ivars. 运行时分配足够的内存来容纳所有ivars。 In this case, each QuantumClone instance will use ~18k (~36k on 64-bit) more memory than a QuantumPilot, since you have told the runtime that it needs to allocate enough ivar storage for 4551 CGPoints. 在这种情况下,每个QuantumClone实例将使用~14k(64位上约36k)比QuantumPilot更多的内存,因为您告诉运行时它需要为4551 CGPoints分配足够的ivar存储。

If pastVelocities were a CGFloat * rather than a CGFloat[4551] , you would need to manually allocate the memory via malloc and then call free in -dealloc . 如果pastVelocities是CGFloat *而不是CGFloat[4551] ,则需要通过malloc手动分配内存,然后在-dealloc调用free However, by declaring it as a fixed size C-array, the runtime handles it (but at the expense of making every QuantumClone a rather huge object). 但是,通过将其声明为固定大小的C数组,运行时会处理它(但代价是使每个QuantumClone成为一个相当大的对象)。

That said, this whole approach seems fragile. 也就是说,这整个方法似乎很脆弱。 Why 4551? 为什么选择4551? Why C arrays? 为何选择C阵列? There is nothing wrong with using C arrays for performance, but I strongly suspect this is premature optimization. 使用C数组来提高性能没有任何问题,但我强烈怀疑这是不成熟的优化。

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

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