简体   繁体   English

使用C ++,SDL,OpenGL和GLEW的无法解释的内存错误

[英]Unexplained memory error using C++, SDL, OpenGL and GLEW

This is my entire application (stripped down to the essentials): 这是我的整个应用程序(精简了要点):

#include <stdio.h>
#include <GL/glew.h>
#include <SDL.h>
#include <SDL_opengl.h>
#include <gl/glu.h>

int main( int argc, char* args[] )
{
    SDL_Init(SDL_INIT_VIDEO);
    SDL_Window* gWindow = SDL_CreateWindow( "title", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN );
    SDL_GLContext gContext = SDL_GL_CreateContext( gWindow );
    glewInit();

    GLfloat *floatArray = new GLfloat(108);
    floatArray[107] = 0.0f;

    GLuint glBuffer;
    glGenBuffers(1, &glBuffer);

    return 0;
}

This and the required libraries are all compiled in Visual Studio 2012. 该库和所需的库都在Visual Studio 2012中编译。

This code initialises all the required systems for an OpenGL context, in the right order and nothing is missing as far as I can see (cleaning up is only needed after the point it breaks so I took that out). 这段代码以正确的顺序初始化了OpenGL上下文所需的所有系统,并且据我所知没有丢失任何东西(仅在中断点之后才需要清理,因此我将其清除了)。

It breaks on the line glGenBuffers(1, &glBuffer); 它在行glGenBuffers(1, &glBuffer); , with the error "SOFT323_vs11_2012.exe has triggered a breakpoint." ,错误为“ SOFT323_vs11_2012.exe触发了断点”。

Setting an element beyond floatArray[9] is required to break it. 需要设置超出floatArray[9]的元素才能破坏它。

How do I approach this problem? 我该如何解决这个问题? Is it a glitch with one of the libraries or am I using them incorrectly? 是其中一个库出现故障还是我使用不正确?

This allocates a single GLfloat , initialized with the value 108.0f . 这将分配一个GLfloat ,其初始值为108.0f

GLfloat *floatArray = new GLfloat(108);

You wanted to allocate an array : 您想分配一个数组

GLfloat *floatArray = new GLfloat[108];

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

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