From 9c23d8b54fb46a50d95f51a65b4d46871a26d8ce Mon Sep 17 00:00:00 2001 From: brainstormi Date: Tue, 22 Dec 2015 21:29:10 +0100 Subject: [PATCH] SDL2 resource destructors fix --- c_src/sdl_gl.c | 10 ++++++++-- c_src/sdl_renderer.c | 8 ++++++-- c_src/sdl_surface.c | 6 +++++- c_src/sdl_texture.c | 6 +++++- c_src/sdl_window.c | 6 +++++- 5 files changed, 29 insertions(+), 7 deletions(-) diff --git a/c_src/sdl_gl.c b/c_src/sdl_gl.c index febd49f..bd390ba 100644 --- a/c_src/sdl_gl.c +++ b/c_src/sdl_gl.c @@ -14,10 +14,16 @@ #include "esdl2.h" + +NIF_CAST_HANDLER(thread_destroy_GLContext) +{ + SDL_GL_DeleteContext(NIF_RES_GET(GLContext, args[0])); + enif_release_resource(NIF_RES_DEP(GLContext, args[0])); +} + void dtor_GLContext(ErlNifEnv* env, void* obj) { - SDL_GL_DeleteContext(NIF_RES_GET(GLContext, obj)); - enif_release_resource(NIF_RES_DEP(GLContext, obj)); + nif_thread_cast(env,thread_destroy_GLContext,1,obj); } // gl_create_context diff --git a/c_src/sdl_renderer.c b/c_src/sdl_renderer.c index 2b80cdd..78f917f 100644 --- a/c_src/sdl_renderer.c +++ b/c_src/sdl_renderer.c @@ -14,10 +14,14 @@ #include "esdl2.h" +NIF_CAST_HANDLER(thread_destroy_renderer) +{ + SDL_DestroyRenderer(NIF_RES_GET(Renderer, args[0])); + enif_release_resource(NIF_RES_DEP(Renderer, args[0])); +} void dtor_Renderer(ErlNifEnv* env, void* obj) { - SDL_DestroyRenderer(NIF_RES_GET(Renderer, obj)); - enif_release_resource(NIF_RES_DEP(Renderer, obj)); + nif_thread_cast(env,thread_destroy_renderer,1,obj); } #define RENDERER_FLAGS(F) \ diff --git a/c_src/sdl_surface.c b/c_src/sdl_surface.c index 8f674ba..3175dce 100644 --- a/c_src/sdl_surface.c +++ b/c_src/sdl_surface.c @@ -15,9 +15,13 @@ #include "esdl2.h" #include "SDL_image.h" +NIF_CAST_HANDLER(thread_destroy_surface) +{ + SDL_FreeSurface(NIF_RES_GET(Surface, args[0])); +} void dtor_Surface(ErlNifEnv* env, void* obj) { - SDL_FreeSurface(NIF_RES_GET(Surface, obj)); + nif_thread_cast(env,thread_destroy_surface,1,obj); } // img_load diff --git a/c_src/sdl_texture.c b/c_src/sdl_texture.c index 8606f37..ad22287 100644 --- a/c_src/sdl_texture.c +++ b/c_src/sdl_texture.c @@ -17,9 +17,13 @@ NIF_ATOM_TO_ENUM_FUNCTION_DECL(atom_to_blend_mode, SDL_BlendMode) NIF_ENUM_TO_ATOM_FUNCTION_DECL(blend_mode_to_atom, SDL_BlendMode) +NIF_CAST_HANDLER(thread_destroy_texture) +{ + SDL_DestroyTexture(NIF_RES_GET(Texture, args[0])); +} void dtor_Texture(ErlNifEnv* env, void* obj) { - SDL_DestroyTexture(NIF_RES_GET(Texture, obj)); + nif_thread_cast(env,thread_destroy_texture,1,obj); } // create_texture_from_surface diff --git a/c_src/sdl_window.c b/c_src/sdl_window.c index 96b3b3e..e2db870 100644 --- a/c_src/sdl_window.c +++ b/c_src/sdl_window.c @@ -16,9 +16,13 @@ NIF_ATOM_TO_ENUM_FUNCTION_DECL(atom_to_bool, SDL_bool) +NIF_CAST_HANDLER(thread_destroy_window) +{ + SDL_DestroyWindow(NIF_RES_GET(Window, args[0])); +} void dtor_Window(ErlNifEnv* env, void* obj) { - SDL_DestroyWindow(NIF_RES_GET(Window, obj)); + nif_thread_cast(env,thread_destroy_window,1,obj); } #define WINDOW_FLAGS(F) \