clean up allocated memory properly when raising Ruby exceptions upon object creation

This commit is contained in:
Josh Holtrop 2014-07-25 16:29:19 -04:00
parent 9203a94a71
commit 8d8c9adcb3
3 changed files with 23 additions and 17 deletions

View File

@ -24,14 +24,15 @@ static void GLProgram_free(void * ptr)
static VALUE GLProgram_new(int argc, VALUE * argv, VALUE klass)
{
GLProgram * glprogram = new GLProgram();
glprogram->id = glCreateProgram();
if (glprogram->id == 0u)
GLuint id = glCreateProgram();
if (id == 0u)
{
rb_raise(rb_eRuntimeError, "Error allocating GL program object");
}
GLProgram * glprogram = new GLProgram();
glprogram->id = id;
VALUE rv = Data_Wrap_Struct(klass, NULL, GLProgram_free, glprogram);
rb_obj_call_init(rv, argc, argv);

View File

@ -79,7 +79,6 @@ static void GLShader_free(void * ptr)
static VALUE GLShader_new(VALUE klass, VALUE type, VALUE source)
{
GLShader * glshader = new GLShader();
GLenum shader_type;
if (type == ID2SYM(rb_intern("vertex")))
@ -99,9 +98,12 @@ static VALUE GLShader_new(VALUE klass, VALUE type, VALUE source)
type_cstr);
}
glshader->id = create_shader(shader_type,
RSTRING_PTR(source),
RSTRING_LEN(source));
GLuint id = create_shader(shader_type,
RSTRING_PTR(source),
RSTRING_LEN(source));
GLShader * glshader = new GLShader();
glshader->id = id;
return Data_Wrap_Struct(klass, NULL, GLShader_free, glshader);
}

View File

@ -72,19 +72,22 @@ static VALUE Window_new(VALUE klass)
}
Init_SDL();
Window * window = new Window();
window->sdl_window = SDL_CreateWindow("jes",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
WIDTH,
HEIGHT,
SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
if (window->sdl_window == NULL)
SDL_Window * sdl_window =
SDL_CreateWindow("jes",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
WIDTH,
HEIGHT,
SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
if (sdl_window == NULL)
{
free(window);
rb_raise(rb_eRuntimeError, "Failed to create SDL window");
}
Window * window = new Window();
window->sdl_window = sdl_window;
(void)SDL_GL_CreateContext(window->sdl_window);
Init_OpenGL();