iOS上有内存限制会导致应用崩溃吗?

时间:2013-12-16 12:06:38

标签: ios objective-c

我的应用程序在启动后立即崩溃。我认为这与我在这个init函数中定义两个大数组有关:

- (AppIndex*)init {
    if (self = [super init]) {
        float rgba[] = { 1.0f, 1.0f, 1.0f, 1.0f };
        range = [Text make:@"50 mi" font:[UIFont fontWithName:@"GillSans" size:30] color:rgba width:1000];
        [range centerOnX:420 Y:10 width:121 height:40];
        mode = [Text make:@"Relevant" font:[UIFont fontWithName:@"GillSans" size:30] color:rgba width:1000];
        [mode centerOnX:100 Y:10 width:121 height:40];
        NSString* wow = @"WOW";
        Vertex vertices[] = {
            // Bottom bar
            {{640, 0}, {250, 166, 104, 127}},
            {{640, 60}, {250, 166, 104, 127}},
            {{0, 60}, {250, 166, 104, 127}},
            {{0, 0}, {250, 166, 104, 127}},

            // Top bar
            {{640, [OpenGLView screenHeight] - 60}, {250, 166, 104, 127}},
            {{640, [OpenGLView screenHeight]}, {250, 166, 104, 127}},
            {{0, [OpenGLView screenHeight]}, {250, 166, 104, 127}},
            {{0, [OpenGLView screenHeight] - 60}, {250, 166, 104, 127}},

            // Background
            {{640, 0}, {255, 255, 255, 0}, {0, 0}},
            {{640, [OpenGLView screenHeight]}, {255, 255, 255, 0}, {0, [OpenGLView screenHeight] / 16.0f}},
            {{0, [OpenGLView screenHeight]}, {255, 255, 255, 0}, {[OpenGLView screenWidth] / 16.0f, [OpenGLView screenHeight] / 16.0f}},
            {{0, 0}, {255, 255, 255, 0}, {[OpenGLView screenWidth] / 16.0f, 0}},

            // Icons
            {{50, 7}, {255, 255, 255, 0}, {C(294), C(45)}},
            {{50, 52}, {255, 255, 255, 0}, {C(294), 0}},
            {{7, 52}, {255, 255, 255, 0}, {C(337), 0}},
            {{7, 7}, {255, 255, 255, 0}, {C(337), C(45)}},

            {{259, 8}, {255, 255, 255, 0}, {C(121), C(40)}},
            {{380, 8}, {255, 255, 255, 0}, {C(242), C(40)}},
            {{259, 48}, {255, 255, 255, 0}, {C(121), 0}},
            {{380, 48}, {255, 255, 255, 0}, {C(242), 0}},

            {{585, 11}, {255, 255, 255, 0}, {C(244), C(40)}},
            {{633, 11}, {255, 255, 255, 0}, {C(292), C(40)}},
            {{585, 51}, {255, 255, 255, 0}, {C(244), 0}},
            {{633, 51}, {255, 255, 255, 0}, {C(292), 0}},

            {{100, 10}, {255, 255, 255, 0}, {0, C(40)}},
            {{221, 10}, {255, 255, 255, 0}, {C(121), C(40)}},
            {{100, 50}, {255, 255, 255, 0}, {0, 0}},
            {{221, 50}, {255, 255, 255, 0}, {C(121), 0}},

            {{420, 10}, {255, 255, 255, 0}, {0, C(40)}},
            {{541, 10}, {255, 255, 255, 0}, {C(121), C(40)}},
            {{420, 50}, {255, 255, 255, 0}, {0, 0}},
            {{541, 50}, {255, 255, 255, 0}, {C(121), 0}},

            {{[range X], [range Y]}, {255, 255, 255, 0}, {0, [range TY]}},
            {{[range X2], [range Y]}, {255, 255, 255, 0}, {[range TX], [range TY]}},
            {{[range X], [range Y2]}, {255, 255, 255, 0}, {0, 0}},
            {{[range X2], [range Y2]}, {255, 255, 255, 0}, {[range TX], 0}},

            {{[mode X], [mode Y]}, {255, 255, 255, 0}, {0, [mode TY]}},
            {{[mode X2], [mode Y]}, {255, 255, 255, 0}, {[mode TX], [mode TY]}},
            {{[mode X], [mode Y2]}, {255, 255, 255, 0}, {0, 0}},
            {{[mode X2], [mode Y2]}, {255, 255, 255, 0}, {[mode TX], 0}}
        };
        GLuint vertexSize = 40;
        for (size_t i = 0; i < vertexSize; i++) {
            vertices[i].position[0] = X(vertices[i].position[0]);
            vertices[i].position[1] = Y(vertices[i].position[1], [OpenGLView screenHeight]);
        }
        GLushort indices[] = {
            0, 1, 2,
            2, 3, 0,

            4, 5, 6,
            6, 7, 4,

            8, 9, 10,
            10, 11, 8,

            12, 13, 14,
            14, 15, 12,

            16, 17, 19,
            16, 19, 18,

            20, 21, 23,
            20, 23, 22,

            24, 25, 27,
            24, 27, 26,

            28, 29, 31,
            28, 31, 30,

            32, 33, 35,
            32, 35, 34,

            36, 37, 39,
            36, 39, 38
        };
        GLuint indexSize = 60;
        icons = [OpenGLView setupTexture:@"icons.png"];
        background = [OpenGLView setupTexture:@"tile.png"];
        [self vboWithVertices:vertices count:vertexSize indices:indices count:indexSize];
    }
    return self;
}

- (void)vboWithVertices:(Vertex[])vertices count:(GLuint)countV indices:(GLushort[])indices count:(GLuint)countI {
    glGenBuffers(1, &vertexBuffer);
    glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
    NSLog(@"%u", sizeof(Vertex));
    glBufferData(GL_ARRAY_BUFFER, countV * sizeof(Vertex), vertices, GL_STATIC_DRAW);
    glGenBuffers(1, &indexBuffer);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, countI * sizeof(GLushort), indices, GL_STATIC_DRAW);
}

这有几个方面。 rangemode是正在加载的两个纹理。 iconsbackground也是如此。那些巨大的数组也会在最后复制到VBO。

这可以达到我可以在我的应用中添加内容的某个时刻。现在,如果我向同一个函数添加一行,它将崩溃。就像这样:

NSString* wow = @"Wow";

如果我在函数外部添加数组(可能是因为某些内存已被释放),它不会崩溃。

我的应用会因EXC_BAD_ACCESS而崩溃。如果我注释掉两个大型数组,我的应用程序工作,所以我认为这是一个内存问题。但即便使用的内存也不多。当应用程序崩溃Xcode表示它运行&lt; 1ms,内存高6.4MB,低6.4MB。

无论如何,这些数组只是占位符,我会更有条理。这有补救措施吗?有人可以解释为什么会崩溃吗?

1 个答案:

答案 0 :(得分:1)

数组数据全部在堆栈AFAICS上......不要把它放在堆栈上