From d6fec79334e5daf9d442b50860d786a28cf46b3d Mon Sep 17 00:00:00 2001
From: Tomaka17 <pierre.krieger1708@gmail.com>
Date: Fri, 22 Aug 2014 11:26:00 +0200
Subject: [PATCH] Cleanup temporary objects in case of error during creation on
 win32 See #10

---
 src/win32/init.rs | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/src/win32/init.rs b/src/win32/init.rs
index 21d9612b..9b6ec815 100644
--- a/src/win32/init.rs
+++ b/src/win32/init.rs
@@ -135,6 +135,7 @@ pub fn new_window(builder: WindowBuilder) -> Result<Window, String> {
                 if hdc.is_null() {
                     tx.send(Err(format!("GetDC function failed: {}",
                         os::error_string(os::errno() as uint))));
+                    unsafe { ffi::DestroyWindow(dummy_window); }
                     return;
                 }
                 hdc
@@ -162,6 +163,7 @@ pub fn new_window(builder: WindowBuilder) -> Result<Window, String> {
                 if pf_index == 0 {
                     tx.send(Err(format!("ChoosePixelFormat function failed: {}",
                         os::error_string(os::errno() as uint))));
+                    unsafe { ffi::DestroyWindow(dummy_window); }
                     return;
                 }
 
@@ -170,6 +172,7 @@ pub fn new_window(builder: WindowBuilder) -> Result<Window, String> {
                 {
                     tx.send(Err(format!("DescribePixelFormat function failed: {}",
                         os::error_string(os::errno() as uint))));
+                    unsafe { ffi::DestroyWindow(dummy_window); }
                     return;
                 }
 
@@ -181,6 +184,7 @@ pub fn new_window(builder: WindowBuilder) -> Result<Window, String> {
                 if ffi::SetPixelFormat(dummy_hdc, 1, &pixel_format) == 0 {
                     tx.send(Err(format!("SetPixelFormat function failed: {}",
                         os::error_string(os::errno() as uint))));
+                    ffi::DestroyWindow(dummy_window);
                     return;
                 }
             }
@@ -191,6 +195,7 @@ pub fn new_window(builder: WindowBuilder) -> Result<Window, String> {
                 if ctxt.is_null() {
                     tx.send(Err(format!("wglCreateContext function failed: {}",
                         os::error_string(os::errno() as uint))));
+                    unsafe { ffi::DestroyWindow(dummy_window); }
                     return;
                 }
                 ctxt
@@ -259,6 +264,7 @@ pub fn new_window(builder: WindowBuilder) -> Result<Window, String> {
             if hdc.is_null() {
                 tx.send(Err(format!("GetDC function failed: {}",
                     os::error_string(os::errno() as uint))));
+                unsafe { ffi::DestroyWindow(real_window); }
                 return;
             }
             hdc
@@ -269,6 +275,7 @@ pub fn new_window(builder: WindowBuilder) -> Result<Window, String> {
             if ffi::SetPixelFormat(hdc, 1, &pixel_format) == 0 {
                 tx.send(Err(format!("SetPixelFormat function failed: {}",
                     os::error_string(os::errno() as uint))));
+                ffi::DestroyWindow(real_window);
                 return;
             }
         }
@@ -299,6 +306,7 @@ pub fn new_window(builder: WindowBuilder) -> Result<Window, String> {
             if ctxt.is_null() {
                 tx.send(Err(format!("OpenGL context creation failed: {}",
                     os::error_string(os::errno() as uint))));
+                unsafe { ffi::DestroyWindow(real_window); }
                 return;
             }
 
@@ -324,6 +332,8 @@ pub fn new_window(builder: WindowBuilder) -> Result<Window, String> {
             if lib.is_null() {
                 tx.send(Err(format!("LoadLibrary function failed: {}",
                     os::error_string(os::errno() as uint))));
+                unsafe { ffi::wglDeleteContext(context); }
+                unsafe { ffi::DestroyWindow(real_window); }
                 return;
             }
             lib