简体   繁体   English

在自动释放池上调用释放时,应用程序崩溃-计时问题? 苹果手机

[英]App Crashing When Calling Release on Autorelease Pool - Timing Issue ? iPhone

I have the following method in my app : 我的应用程式中有以下方法:

- (void) loadModel {
// Runs in a seperate thread so we need to create an additional NSAutoreleasePool
pool = [[NSAutoreleasePool alloc] init];

NSData *getData = [activeModelInfo getFileData];

if (getData) {
    if ([activeModel loadFromFileData:daeData]) {
        [activeModel reset];
        [mainViewController performSelectorOnMainThread:@selector(showModelView) withObject:nil waitUntilDone:NO];
    }
    else {

    }
}
else {

}

[pool release];

}

The loadFromFileData method calls the following code which loads data. loadFromFileData方法调用以下代码来加载数据。 :

- (void) loadVerticesFromSource:(NSString*)verticesSourceId meshNode:(TBXMLElement*)meshNode intoMesh: (Mesh3D*) mesh {

NSLog(@"Getting Vertices for Source %@",verticesSourceId);

FloatArray *floatArray =  [self getFloatArrayFromSource: verticesSourceId meshNode: meshNode];

if (floatArray) {
[daeFloatArray addObject:floatArray];
}

if (floatArray) {
    if ([floatArray.array count] % 3 != 0) {
        NSLog(@"Float array length not divisible by 3!");
    }
    else {
        mesh->verticesCount = [floatArray.array count] / 3;
        mesh->vertices = malloc(sizeof(Vector3D) * mesh->verticesCount);
        for (int i=0; i<mesh->verticesCount; i++) {
            mesh->vertices[i].x = [[floatArray.array objectAtIndex:(i*3)] floatValue];
            mesh->vertices[i].y = [[floatArray.array objectAtIndex:(i*3)+1] floatValue];
            mesh->vertices[i].z = [[floatArray.array objectAtIndex:(i*3)+2] floatValue];

            // update extents information
            if (!extents.pointDefined || mesh->vertices[i].x < extents.minX) extents.minX = mesh->vertices[i].x;
            else if (!extents.pointDefined || mesh->vertices[i].x > extents.maxX) extents.maxX = mesh->vertices[i].x;
            if (!extents.pointDefined || mesh->vertices[i].y < extents.minY) extents.minY = mesh->vertices[i].y;
            else if (!extents.pointDefined || mesh->vertices[i].y > extents.maxY) extents.maxY = mesh->vertices[i].y;
            if (!extents.pointDefined || mesh->vertices[i].z < extents.minZ) extents.minZ = mesh->vertices[i].z;
            else if (!extents.pointDefined || mesh->vertices[i].z > extents.maxZ) extents.maxZ = mesh->vertices[i].z;
            if (!extents.pointDefined) extents.pointDefined = YES;

            [pointerStorageArray addObject:[NSValue valueWithPointer:mesh->vertices]];
        }
    }
}
}

Since this method is called several times while the data loads, each time mallocing memory for the mesh->vertices struct, I have created a pointerStorage array where I store the pointer to the malloced memory. 由于在数据加载时多次调用此方法,每次mallocing内存为mesh-> vertices结构,我创建了一个pointerStorage数组,我将指针存储到malloced内存。

The app then displays a 3D object using OpenGL ES. 然后,该应用程序使用OpenGL ES显示3D对象。 When the user presses a Main Menu button, I then free up the pointerStorageArray as follows : 当用户按下Main Menu(主菜单)按钮时,我按如下方式释放了pointerStorageArray:

- (void) freeUpMallocedMemory

for (NSValue * value in pointerStorageArray) {

    free(value);

}

The problem is that the app then crashes during this process. 问题在于该应用程序随后在此过程中崩溃。 All I get is the EXC_BAD_ACCESSS error message and a pointer to the following line of code in the loadModel method above : 我得到的是EXC_BAD_ACCESSS错误消息和上面loadModel方法中以下代码行的指针:

[pool release];

Nothing in the stack trace. 堆栈跟踪中没有任何内容。 I have also tried turning on NSZombie, NSAutoreleaseTrackFreedObjectCheck and NSDebugEnabled, but I still don't get any additional information. 我也尝试过打开NSZombie,NSAutoreleaseTrackFreedObjectCheck和NSDebugEnabled,但是我仍然没有得到任何其他信息。

The odd thing is that if I put a delay on the button of 2 seconds (ie only trigger the freeUpMallocedMemory method after 2 seconds), the app no longer crashes and works fine. 奇怪的是,如果我在2秒的按钮上放置一个延迟(即仅在2秒后触发freeUpMallocedMemory方法),该应用程序不再崩溃并正常工作。

Can anyone suggest what might be causing this - really confused and have already spent a few days troubleshooting. 任何人都可以建议可能导致这种情况 - 真的很困惑,已经花了几天时间进行故障排除。

Thank you ! 谢谢 !

您过度释放了... pointerStorageArray中的值是通过一种便捷方法创建的,因此不需要释放,只需将其从数组中删除即可处理它们。

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

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