Cannot allocate a decent amount of memory glBufferData()

Started by
11 comments, last by AbandonedAccount 10 years, 2 months ago

Hello,

I have an issue with glBufferData() and memory. I was testing how much memory I could allocate to a single vbo. I noticed it was smaller than 10 KB (10 000 bytes) and bigger than 1 KB (1000 bytes). The problem is that I have approximately 1700 MB of memory on my GPU and 8 GB of RAM on my computer which is way more than 10 KB...

Thank you,

Thecheeselover

Hide yo cheese! Hide yo wife!

Advertisement

Is it returning GL_OUT_OF_MEMORY or GL_INVALID_OPERATION or what? Considering you want to aim for buffers over a megabyte in size, it's definitely odd to hear you can't allocate 10k. Without knowing anything about your code and such, I have no clue what to tell you though.

Is it returning GL_OUT_OF_MEMORY or GL_INVALID_OPERATION or what? Considering you want to aim for buffers over a megabyte in size, it's definitely odd to hear you can't allocate 10k. Without knowing anything about your code and such, I have no clue what to tell you though.

gDEBugger shows me this:

Exception
Event Properties
Reason: Access violation
Address: 0x52f19f89
Details: The thread tried to read from or write to a virtual address to which it does not have access.

Hide yo cheese! Hide yo wife!

Post your code. That's a segfault, not an out-of-memory exception.

Post your code. That's a segfault, not an out-of-memory exception.


void MinecraftTest1::InitBuffers()
{
    glGenBuffers(1, &vbo);
    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertexData), vertexData, GL_STATIC_DRAW);
    // glBufferData(GL_ARRAY_BUFFER, 10000, vertexData, GL_STATIC_DRAW);

    glGenBuffers(1, &ibo);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(vertexIndices), vertexIndices, GL_STATIC_DRAW);
}

Hide yo cheese! Hide yo wife!

If the commented out line is the one that crashes, that's because you're copying past the end of vertexData. glBufferData (using text replacement with your code) is basically:

boundBuffer[GL_ARRAY_BUFFER] = malloc(10000);

memcpy(boudBuffer[GL_ARRAY_BUFFER], vertexData, 10000);

And assuming vertexData is less than 10000 bytes long, the memcpy part of that crashes after trying to copy past the end of vertexData.

What is vertexData?

sizeof(vertexData) is probably not what you think it is; you're probably allocating a 4-byte VBO.

What is vertexData?

sizeof(vertexData) is probably not what you think it is; you're probably allocating a 4-byte VBO.

Here it is:


GLfloat vertexData[NUM_VERTS * ELEM_PER_POS + NUM_VERTS * ELEM_PER_COLOR + NUM_VERTS * ELEM_PER_NORM] = {
    -1.0f, -1.0f, -1.0f, // Positions
    -1.0f, 1.0f, -1.0f,  // Back
    1.0f, 1.0f, -1.0f,
    1.0f, -1.0f, -1.0f,

    -1.0f, -1.0f, 1.0f,  // Front
    -1.0f, 1.0f, 1.0f,
    1.0f, 1.0f, 1.0f,
    1.0f, -1.0f, 1.0f,

    1.0f, 1.0f, 1.0f, 1.0f, // Colors
    1.0f, 1.0f, 1.0f, 1.0f,
    1.0f, 1.0f, 1.0f, 1.0f,
    1.0f, 1.0f, 1.0f, 1.0f,

    1.0f, 1.0f, 1.0f, 1.0f,
    1.0f, 1.0f, 1.0f, 1.0f,
    1.0f, 1.0f, 1.0f, 1.0f,
    1.0f, 1.0f, 1.0f, 1.0f,

    -1.0f, -1.0f, -1.0f, // Normals
    -1.0f, 1.0f, -1.0f,  // Back
    1.0f, 1.0f, -1.0f,
    1.0f, -1.0f, -1.0f,

    -1.0f, -1.0f, 1.0f,  // Front
    -1.0f, 1.0f, 1.0f,
    1.0f, 1.0f, 1.0f,
    1.0f, -1.0f, 1.0f};

Hide yo cheese! Hide yo wife!

If the commented out line is the one that crashes, that's because you're copying past the end of vertexData. glBufferData (using text replacement with your code) is basically:

boundBuffer[GL_ARRAY_BUFFER] = malloc(10000);

memcpy(boudBuffer[GL_ARRAY_BUFFER], vertexData, 10000);

And assuming vertexData is less than 10000 bytes long, the memcpy part of that crashes after trying to copy past the end of vertexData.

Possible, but why allocating 1000 bytes doesn't crash?

Hide yo cheese! Hide yo wife!

As Hodgeman has already pointed out and as your code clearly shows, you are allocating way to less memory. sizeof(vertexData) returns the size of the datatype of vertexData, which is GLfloat, which is 4 byte. You need sizeof(float)*numVertices*numElementsPerVertex in order allocate a correctly sized buffer.

Though I'm no expert on the details, the reason why having less bytes not crash is that behaviour is somewhat undefined if violating memory boundaries. For example, a fellow colleque of mine had a fixed-size texture cache with an array of size 23, but she did in fact store 25 textures without any problem whatsoever. I at some point deleted a dynamic array without [] and didn't get a crash until like a week later in some unrelated code piece. I also once deleted memory twice and got some strange DirectX11-related error from the GPU that didn't have anything to do with the issue itself. The thing is, when you are writing over the boundaries of like an array, you are writing to some random region of memory, where eigther nothing can be allocated yet, or you could overwrite something from another array, etc... if you are lucky, the debugger catches on to it, if not, such random things as in your case happen. Bottom line: Memory is stupid, always triple-check when dealing with raw memory management.

This topic is closed to new replies.

Advertisement