Fix non freed windows handle. (thx to jaroslav.fojtik)
authoredouard
Fri, 10 Sep 2010 13:43:17 +0200
changeset 625 5482b98e82a3
parent 624 48bff18fc546
child 626 270b25cd9e8f
Fix non freed windows handle. (thx to jaroslav.fojtik)
drivers/can_peak_win32/can_peak_win32.c
--- a/drivers/can_peak_win32/can_peak_win32.c	Wed Jun 16 21:29:39 2010 +0200
+++ b/drivers/can_peak_win32/can_peak_win32.c	Fri Sep 10 13:43:17 2010 +0200
@@ -100,11 +100,12 @@
 		}
 
 		//Create the Event for the first board
-		hEvent2 = CreateEvent(NULL, // lpEventAttributes
-							FALSE,  // bManualReset
-							FALSE,  // bInitialState
-							"");    // lpName
-
+		if(hEvent2 != NULL){
+			hEvent2 = CreateEvent(NULL, // lpEventAttributes
+			                      FALSE,  // bManualReset
+			                      FALSE,  // bInitialState
+			                      "");    // lpName
+		}
 		//Set Event Handle for CANReadExt
 		CAN2_SetRcvEvent(hEvent2);
 	}
@@ -118,10 +119,12 @@
 				return 0;
 		}
 		//Create the Event for the first board
-		hEvent1 = CreateEvent(NULL, // lpEventAttributes
-							FALSE,  // bManualReset
-							FALSE,  // bInitialState
-							"");    // lpName
+		if(hEvent1 != NULL){
+			hEvent1 = CreateEvent(NULL, // lpEventAttributes
+			                      FALSE,  // bManualReset
+			                      FALSE,  // bInitialState
+			                      "");    // lpName
+		}
 		//Set Event Handle for CANReadExt
 		CAN_SetRcvEvent(hEvent1);
 	}
@@ -308,21 +311,31 @@
 int canClose_driver (CAN_HANDLE fd0)
 {
 #ifdef PCAN2_HEADER_
-	// if not the first handler
-	if(second_board == (s_BOARD *)fd0)
-	{
-		CAN2_SetRcvEvent(NULL);
-		CAN2_Close ();
-		SetEvent(hEvent2);
-		second_board = (s_BOARD *)NULL;
-	}else
-#endif
-	if(first_board == (s_BOARD *)fd0)
-	{
-		CAN_SetRcvEvent(NULL);
-		CAN_Close ();
-		SetEvent(hEvent1);
-		first_board = (s_BOARD *)NULL;
-	}
-	return 0;
-}
+      // if not the first handler
+      if(second_board == (s_BOARD *)fd0)
+      {
+           CAN2_SetRcvEvent(NULL);
+           CAN2_Close ();
+           if(hEvent2)
+           {
+             SetEvent(hEvent2);
+             CloseHandle(hEvent2);
+             hEvent2 = NULL;
+           }
+           second_board = (s_BOARD *)NULL;
+      }else
+#endif
+      if(first_board == (s_BOARD *)fd0)
+      {
+           CAN_SetRcvEvent(NULL);
+           CAN_Close ();
+           if(hEvent1)
+           {
+             SetEvent(hEvent1);
+             CloseHandle(hEvent1);
+             hEvent1 = NULL;
+            }
+           first_board = (s_BOARD *)NULL;
+      }
+      return 0;
+}