Call AddRef() on the device so it doesn't accidentally get released from underneath the caller.
authorSam Lantinga <slouken@libsdl.org>
Sat, 28 Sep 2013 14:07:17 -0700
changeset 7766 84b154b1ed71
parent 7765 09937804892a
child 7767 0d425d6327d8
Call AddRef() on the device so it doesn't accidentally get released from underneath the caller.
include/SDL_system.h
src/render/direct3d/SDL_render_d3d.c
--- a/include/SDL_system.h	Sat Sep 28 14:07:14 2013 -0700
+++ b/include/SDL_system.h	Sat Sep 28 14:07:17 2013 -0700
@@ -49,7 +49,9 @@
 */
 extern DECLSPEC int SDLCALL SDL_Direct3D9GetAdapterIndex( int displayIndex );
 
-/* Returns the D3D device associated with a renderer, or NULL if it's not a D3D renderer. */
+/* Returns the D3D device associated with a renderer, or NULL if it's not a D3D renderer.
+   Once you are done using the device, you should release it to avoid a resource leak.
+ */
 typedef struct IDirect3DDevice9 IDirect3DDevice9;
 extern DECLSPEC IDirect3DDevice9* SDLCALL SDL_RenderGetD3D9Device(SDL_Renderer * renderer);
 
--- a/src/render/direct3d/SDL_render_d3d.c	Sat Sep 28 14:07:14 2013 -0700
+++ b/src/render/direct3d/SDL_render_d3d.c	Sat Sep 28 14:07:17 2013 -0700
@@ -1887,13 +1887,19 @@
 SDL_RenderGetD3D9Device(SDL_Renderer * renderer)
 {
 	D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
+	IDirect3DDevice9 *device;
 
 	// Make sure that this is a D3D renderer
 	if (renderer->DestroyRenderer != D3D_DestroyRenderer) {
 		SDL_SetError("Renderer is not a D3D renderer");
 		return NULL;
 	}
-	return data->device;
+
+	device = data->device;
+	if (device) {
+		IDirect3DDevice9_AddRef( device );
+	}
+	return device;
 }
 
 #endif /* SDL_VIDEO_RENDER_D3D && !SDL_RENDER_DISABLED */