简体   繁体   中英

Are there memory restrictions on iOS that cause an app to crash?

My app crashes right after it starts. I think it has something to do with me defining two large arrays in this init function:

- (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);
}

This does a couple of things. range and mode are two textures being loaded. So are icons and background . Those huge arrays are also copied to a VBO at the end.

This worked up to a certain point where I could add things to my app. Now if I add one line to the same function, it will crash. Like even this:

NSString* wow = @"Wow";

It won't crash if I add lines outside the function with the arrays (presumably because some memory has been freed).

My app will crash with EXC_BAD_ACCESS . If I comment out the two large arrays, my app works so I think it is a memory issue. But not that much memory is even used. When the app crashes Xcode says that it ran for < 1ms and the memory has a high of 6.4MB and a low of 6.4MB.

Anyways, these arrays are just placeholders, I will get more organized. Is there a remedy for this? And can someone explain why it is crashing?

数组数据全部在堆栈上AFAICS ...不要将其放在堆栈上

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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