开发者社区 问答 正文

请问,我这段代码问题出现在哪里,内存等不到释放?

如题,在ios中使用opengl渲染的过程中,数组内存得不到释放,而我每次移动模型,会反复调用下述方法,导致内存越来越大,程序崩溃。求指点下。。

@autoreleasepool {
        
       
    if (sumFloat == nil) {
        sumFloat = [[NSMutableArray alloc]init];
    }
    
    
    NSString *fileName = [[NSBundle mainBundle]pathForResource:@"dangong" ofType:@"stl"];

    
    
    if (sumFloat.count == 0) {
        NSLog(@"当期没数据");
        NSString *asOrbar = [NSString stringWithContentsOfFile:fileName encoding:NSUTF8StringEncoding error:nil];
        
        if (asOrbar == nil) {
            //        NSLog(@"二进制");
            //编译二进制文件
            [sumFloat addObjectsFromArray: [STLParser stlParserWithBinary:fileName]];
        }
        else
        {
            //        NSLog(@"ascii");
            //编译ascii文件
            [sumFloat addObjectsFromArray:[STLParser stlParserWithAscii:fileName]];
        }
             
    }
    
            
    
                
               float verxxx[999999] = {0.0};//创建数组,保存顶点数据,提供给opengl画图
                int i=0;
                for (NSString *tem in sumFloat) {
                    verxxx[i] = [tem floatValue]/60;
                    i++;
                }
                
                
                //每次数据内存都得不到释放
            
    glVertexAttribPointer(_positionSlot, 3, GL_FLOAT, GL_FALSE, 0, verxxx);
    glEnableVertexAttribArray(_positionSlot);
    
    

    
    
    GLuint buffer;
    glGenBuffers(1, &buffer);
    glBindBuffer(GL_ARRAY_BUFFER, buffer);
    glBufferData(GL_ARRAY_BUFFER, sizeof(verxxx), verxxx, GL_STATIC_DRAW);
    
    //    3、将缓冲区的数据复制进通用顶点属性中
    glEnableVertexAttribArray(GLKVertexAttribPosition);
    glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, 4*8, (char *)NULL + 0);  //读顶点坐标
    
    
    //4、继续复制其他数据
    //    在前面预定义的顶点数据数组中,还包含了法线和纹理坐标,所以参照上面的方法,将剩余的数据分别复制进通用顶点属性中。
    glEnableVertexAttribArray(GLKVertexAttribNormal);
    glVertexAttribPointer(GLKVertexAttribNormal, 3, GL_FLOAT, GL_FALSE, 4*8, (char *)NULL +12); //读法线
    
    
    glEnableVertexAttribArray(GLKVertexAttribTexCoord0);
    glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, 4*8, (char *)NULL + 24);  //读纹理


    glDrawArrays(GL_TRIANGLES, 0, sumFloat.count/7);
//    NSLog(@"-----%lu",sizeof(verxxx)/32);
    
         
            }

展开
收起
a123456678 2016-06-12 11:05:43 2429 分享 版权
1 条回答
写回答
取消 提交回答
  • 补充,你代码里面sumFloat这个可变数组,在创建以后,没有执行过移除Object的操作。
    因为看不到你怎么写的这个属性的property, 但基本可以肯定`sumFloat`不是一个局部变量,所以在你重复执行这段代码的时候,sumFloat的Children越来越多,导致内存过大被系统杀掉。

         if (sumFloat.count == 0) {
                NSLog(@"当期没数据");
                NSString *asOrbar = [NSString stringWithContentsOfFile:fileName encoding:NSUTF8StringEncoding error:nil];
                
                if (asOrbar == nil) {
                    //        NSLog(@"二进制");
                    //编译二进制文件
                    [sumFloat addObjectsFromArray: [STLParser stlParserWithBinary:fileName]];
                }
                else
                {
                    //        NSLog(@"ascii");
                    //编译ascii文件
                    [sumFloat addObjectsFromArray:[STLParser stlParserWithAscii:fileName]];
                }
                
            }
    2019-07-17 19:33:30
    赞同 展开评论
问答分类:
问答标签:
问答地址: