From f1c024905359cc6d6d24f79f40b65d519d96b126 Mon Sep 17 00:00:00 2001 From: Jef Driesen Date: Mon, 12 Oct 2015 19:51:52 +0200 Subject: [PATCH] Simplify the error handling in the close function. When the close function returns, all resources should be freed, regardless of whether an error has occured or not. The error code is purely informative. However, in order to return the first error code, which is usually the most interesting one, the current implementation is unnecessary complicated. If an error occurs, there is no need to exit immediately. Simply store the error code unless there is already a previous one, and then continue. --- msvc/libdivecomputer.vcproj | 8 ++++++++ src/Makefile.am | 1 + src/citizen_aqualand.c | 6 +++--- src/common-private.h | 37 +++++++++++++++++++++++++++++++++++++ src/common.c | 34 ++++++++++++++++++++++++++++++++++ src/cressi_edy.c | 6 +++--- src/cressi_leonardo.c | 6 +++--- src/device-private.h | 2 ++ src/diverite_nitekq.c | 6 +++--- src/divesystem_idive.c | 6 +++--- src/hw_frog.c | 15 +++++++-------- src/hw_ostc.c | 6 +++--- src/hw_ostc3.c | 10 ++++------ src/irda.c | 13 +++++-------- src/mares_darwin.c | 6 +++--- src/mares_iconhd.c | 6 +++--- src/mares_nemo.c | 6 +++--- src/mares_puck.c | 6 +++--- src/oceanic_atom2.c | 6 +++--- src/oceanic_veo250.c | 6 +++--- src/oceanic_vtpro.c | 6 +++--- src/reefnet_sensus.c | 6 +++--- src/reefnet_sensuspro.c | 6 +++--- src/reefnet_sensusultra.c | 6 +++--- src/serial_posix.c | 11 +++++------ src/serial_win32.c | 11 +++++------ src/suunto_d9.c | 6 +++--- src/suunto_eon.c | 6 +++--- src/suunto_solution.c | 6 +++--- src/suunto_vyper.c | 6 +++--- src/suunto_vyper2.c | 6 +++--- src/uwatec_aladin.c | 6 +++--- src/uwatec_memomouse.c | 6 +++--- src/uwatec_meridian.c | 6 +++--- src/uwatec_smart.c | 6 +++--- src/zeagle_n2ition3.c | 6 +++--- 36 files changed, 186 insertions(+), 112 deletions(-) create mode 100644 src/common-private.h create mode 100644 src/common.c diff --git a/msvc/libdivecomputer.vcproj b/msvc/libdivecomputer.vcproj index aff2bd7..3da8750 100644 --- a/msvc/libdivecomputer.vcproj +++ b/msvc/libdivecomputer.vcproj @@ -210,6 +210,10 @@ RelativePath="..\src\citizen_aqualand_parser.c" > + + @@ -520,6 +524,10 @@ RelativePath="..\include\libdivecomputer\citizen_aqualand.h" > + + diff --git a/src/Makefile.am b/src/Makefile.am index 64c3649..c029a79 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -17,6 +17,7 @@ libdivecomputer_la_SOURCES = \ version.c \ descriptor.c \ iterator-private.h iterator.c \ + common-private.h common.c \ context-private.h context.c \ device-private.h device.c \ parser-private.h parser.c \ diff --git a/src/citizen_aqualand.c b/src/citizen_aqualand.c index 9a2812d..cda3dbc 100644 --- a/src/citizen_aqualand.c +++ b/src/citizen_aqualand.c @@ -125,18 +125,18 @@ error_free: static dc_status_t citizen_aqualand_device_close (dc_device_t *abstract) { + dc_status_t status = DC_STATUS_SUCCESS; citizen_aqualand_device_t *device = (citizen_aqualand_device_t*) abstract; // Close the device. if (serial_close (device->port) == -1) { - free (device); - return DC_STATUS_IO; + dc_status_set_error(&status, DC_STATUS_IO); } // Free memory. free (device); - return DC_STATUS_SUCCESS; + return status; } diff --git a/src/common-private.h b/src/common-private.h new file mode 100644 index 0000000..94cfd91 --- /dev/null +++ b/src/common-private.h @@ -0,0 +1,37 @@ +/* + * libdivecomputer + * + * Copyright (C) 2015 Jef Driesen + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301 USA + */ + +#ifndef COMMON_PRIVATE_H +#define COMMON_PRIVATE_H + +#include + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +void +dc_status_set_error (dc_status_t *status, dc_status_t error); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ +#endif /* COMMON_PRIVATE_H */ diff --git a/src/common.c b/src/common.c new file mode 100644 index 0000000..d1141d5 --- /dev/null +++ b/src/common.c @@ -0,0 +1,34 @@ +/* + * libdivecomputer + * + * Copyright (C) 2015 Jef Driesen + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301 USA + */ + +#include +#include + +#include "common-private.h" + +void +dc_status_set_error (dc_status_t *status, dc_status_t error) +{ + assert (status != NULL); + + if (*status == DC_STATUS_SUCCESS) + *status = error; +} diff --git a/src/cressi_edy.c b/src/cressi_edy.c index 602ca0d..e58619b 100644 --- a/src/cressi_edy.c +++ b/src/cressi_edy.c @@ -331,6 +331,7 @@ error_free: static dc_status_t cressi_edy_device_close (dc_device_t *abstract) { + dc_status_t status = DC_STATUS_SUCCESS; cressi_edy_device_t *device = (cressi_edy_device_t*) abstract; // Send the quit command. @@ -338,14 +339,13 @@ cressi_edy_device_close (dc_device_t *abstract) // Close the device. if (serial_close (device->port) == -1) { - free (device); - return DC_STATUS_IO; + dc_status_set_error(&status, DC_STATUS_IO); } // Free memory. free (device); - return DC_STATUS_SUCCESS; + return status; } diff --git a/src/cressi_leonardo.c b/src/cressi_leonardo.c index c02bea7..6ca321d 100644 --- a/src/cressi_leonardo.c +++ b/src/cressi_leonardo.c @@ -142,18 +142,18 @@ error_free: static dc_status_t cressi_leonardo_device_close (dc_device_t *abstract) { + dc_status_t status = DC_STATUS_SUCCESS; cressi_leonardo_device_t *device = (cressi_leonardo_device_t *) abstract; // Close the device. if (serial_close (device->port) == -1) { - free (device); - return DC_STATUS_IO; + dc_status_set_error(&status, DC_STATUS_IO); } // Free memory. free (device); - return DC_STATUS_SUCCESS; + return status; } static dc_status_t diff --git a/src/device-private.h b/src/device-private.h index 8e183fb..9254c6c 100644 --- a/src/device-private.h +++ b/src/device-private.h @@ -27,6 +27,8 @@ #include #include +#include "common-private.h" + #ifdef __cplusplus extern "C" { #endif /* __cplusplus */ diff --git a/src/diverite_nitekq.c b/src/diverite_nitekq.c index 2a7c4b4..1d8b96a 100644 --- a/src/diverite_nitekq.c +++ b/src/diverite_nitekq.c @@ -218,6 +218,7 @@ error_free: static dc_status_t diverite_nitekq_device_close (dc_device_t *abstract) { + dc_status_t status = DC_STATUS_SUCCESS; diverite_nitekq_device_t *device = (diverite_nitekq_device_t*) abstract; // Disconnect. @@ -225,14 +226,13 @@ diverite_nitekq_device_close (dc_device_t *abstract) // Close the device. if (serial_close (device->port) == -1) { - free (device); - return DC_STATUS_IO; + dc_status_set_error(&status, DC_STATUS_IO); } // Free memory. free (device); - return DC_STATUS_SUCCESS; + return status; } diff --git a/src/divesystem_idive.c b/src/divesystem_idive.c index af36cd0..e4eec6a 100644 --- a/src/divesystem_idive.c +++ b/src/divesystem_idive.c @@ -173,18 +173,18 @@ error_free: static dc_status_t divesystem_idive_device_close (dc_device_t *abstract) { + dc_status_t status = DC_STATUS_SUCCESS; divesystem_idive_device_t *device = (divesystem_idive_device_t*) abstract; // Close the device. if (serial_close (device->port) == -1) { - free (device); - return DC_STATUS_IO; + dc_status_set_error(&status, DC_STATUS_IO); } // Free memory. free (device); - return DC_STATUS_SUCCESS; + return status; } diff --git a/src/hw_frog.c b/src/hw_frog.c index 7c7b45a..48c9175 100644 --- a/src/hw_frog.c +++ b/src/hw_frog.c @@ -271,27 +271,26 @@ error_free: static dc_status_t hw_frog_device_close (dc_device_t *abstract) { + dc_status_t status = DC_STATUS_SUCCESS; hw_frog_device_t *device = (hw_frog_device_t*) abstract; + dc_status_t rc = DC_STATUS_SUCCESS; // Send the exit command. - dc_status_t status = hw_frog_transfer (device, NULL, EXIT, NULL, 0, NULL, 0); - if (status != DC_STATUS_SUCCESS) { + rc = hw_frog_transfer (device, NULL, EXIT, NULL, 0, NULL, 0); + if (rc != DC_STATUS_SUCCESS) { ERROR (abstract->context, "Failed to send the command."); - serial_close (device->port); - free (device); - return status; + dc_status_set_error(&status, rc); } // Close the device. if (serial_close (device->port) == -1) { - free (device); - return DC_STATUS_IO; + dc_status_set_error(&status, DC_STATUS_IO); } // Free memory. free (device); - return DC_STATUS_SUCCESS; + return status; } diff --git a/src/hw_ostc.c b/src/hw_ostc.c index d62466e..42dc778 100644 --- a/src/hw_ostc.c +++ b/src/hw_ostc.c @@ -187,18 +187,18 @@ error_free: static dc_status_t hw_ostc_device_close (dc_device_t *abstract) { + dc_status_t status = DC_STATUS_SUCCESS; hw_ostc_device_t *device = (hw_ostc_device_t*) abstract; // Close the device. if (serial_close (device->port) == -1) { - free (device); - return DC_STATUS_IO; + dc_status_set_error(&status, DC_STATUS_IO); } // Free memory. free (device); - return DC_STATUS_SUCCESS; + return status; } diff --git a/src/hw_ostc3.c b/src/hw_ostc3.c index a7bc9a3..20cbe8d 100644 --- a/src/hw_ostc3.c +++ b/src/hw_ostc3.c @@ -430,6 +430,7 @@ hw_ostc3_device_init (hw_ostc3_device_t *device, hw_ostc3_state_t state) static dc_status_t hw_ostc3_device_close (dc_device_t *abstract) { + dc_status_t status = DC_STATUS_SUCCESS; hw_ostc3_device_t *device = (hw_ostc3_device_t*) abstract; dc_status_t rc = DC_STATUS_SUCCESS; @@ -438,22 +439,19 @@ hw_ostc3_device_close (dc_device_t *abstract) rc = hw_ostc3_transfer (device, NULL, EXIT, NULL, 0, NULL, 0); if (rc != DC_STATUS_SUCCESS) { ERROR (abstract->context, "Failed to send the command."); - serial_close (device->port); - free (device); - return rc; + dc_status_set_error(&status, rc); } } // Close the device. if (serial_close (device->port) == -1) { - free (device); - return DC_STATUS_IO; + dc_status_set_error(&status, DC_STATUS_IO); } // Free memory. free (device); - return DC_STATUS_SUCCESS; + return status; } diff --git a/src/irda.c b/src/irda.c index 3903b7b..3be1151 100644 --- a/src/irda.c +++ b/src/irda.c @@ -133,6 +133,8 @@ error_free: int irda_socket_close (irda_t *device) { + int errcode = 0; + if (device == NULL) return -1; @@ -146,26 +148,21 @@ irda_socket_close (irda_t *device) if (close (device->fd) != 0) { #endif SYSERROR (device->context, ERRNO); -#ifdef _WIN32 - WSACleanup (); -#endif - free (device); - return -1; + errcode = -1; } #ifdef _WIN32 // Terminate the winsock dll. if (WSACleanup () != 0) { SYSERROR (device->context, ERRNO); - free (device); - return -1; + errcode = -1; } #endif // Free memory. free (device); - return 0; + return errcode; } diff --git a/src/mares_darwin.c b/src/mares_darwin.c index 1113a46..5ab153d 100644 --- a/src/mares_darwin.c +++ b/src/mares_darwin.c @@ -173,18 +173,18 @@ error_free: static dc_status_t mares_darwin_device_close (dc_device_t *abstract) { + dc_status_t status = DC_STATUS_SUCCESS; mares_darwin_device_t *device = (mares_darwin_device_t *) abstract; // Close the device. if (serial_close (device->base.port) == -1) { - free (device); - return DC_STATUS_IO; + dc_status_set_error(&status, DC_STATUS_IO); } // Free memory. free (device); - return DC_STATUS_SUCCESS; + return status; } diff --git a/src/mares_iconhd.c b/src/mares_iconhd.c index af6b1de..488852c 100644 --- a/src/mares_iconhd.c +++ b/src/mares_iconhd.c @@ -324,18 +324,18 @@ error_free: static dc_status_t mares_iconhd_device_close (dc_device_t *abstract) { + dc_status_t status = DC_STATUS_SUCCESS; mares_iconhd_device_t *device = (mares_iconhd_device_t*) abstract; // Close the device. if (serial_close (device->port) == -1) { - free (device); - return DC_STATUS_IO; + dc_status_set_error(&status, DC_STATUS_IO); } // Free memory. free (device); - return DC_STATUS_SUCCESS; + return status; } diff --git a/src/mares_nemo.c b/src/mares_nemo.c index 840c27e..2b40905 100644 --- a/src/mares_nemo.c +++ b/src/mares_nemo.c @@ -159,18 +159,18 @@ error_free: static dc_status_t mares_nemo_device_close (dc_device_t *abstract) { + dc_status_t status = DC_STATUS_SUCCESS; mares_nemo_device_t *device = (mares_nemo_device_t*) abstract; // Close the device. if (serial_close (device->port) == -1) { - free (device); - return DC_STATUS_IO; + dc_status_set_error(&status, DC_STATUS_IO); } // Free memory. free (device); - return DC_STATUS_SUCCESS; + return status; } diff --git a/src/mares_puck.c b/src/mares_puck.c index 2b85b9f..07448c0 100644 --- a/src/mares_puck.c +++ b/src/mares_puck.c @@ -181,18 +181,18 @@ error_free: static dc_status_t mares_puck_device_close (dc_device_t *abstract) { + dc_status_t status = DC_STATUS_SUCCESS; mares_puck_device_t *device = (mares_puck_device_t*) abstract; // Close the device. if (serial_close (device->base.port) == -1) { - free (device); - return DC_STATUS_IO; + dc_status_set_error(&status, DC_STATUS_IO); } // Free memory. free (device); - return DC_STATUS_SUCCESS; + return status; } diff --git a/src/oceanic_atom2.c b/src/oceanic_atom2.c index c009ab1..26f6fd3 100644 --- a/src/oceanic_atom2.c +++ b/src/oceanic_atom2.c @@ -651,6 +651,7 @@ error_free: static dc_status_t oceanic_atom2_device_close (dc_device_t *abstract) { + dc_status_t status = DC_STATUS_SUCCESS; oceanic_atom2_device_t *device = (oceanic_atom2_device_t*) abstract; // Send the quit command. @@ -658,14 +659,13 @@ oceanic_atom2_device_close (dc_device_t *abstract) // Close the device. if (serial_close (device->port) == -1) { - free (device); - return DC_STATUS_IO; + dc_status_set_error(&status, DC_STATUS_IO); } // Free memory. free (device); - return DC_STATUS_SUCCESS; + return status; } diff --git a/src/oceanic_veo250.c b/src/oceanic_veo250.c index b21f6a0..d2b5d6e 100644 --- a/src/oceanic_veo250.c +++ b/src/oceanic_veo250.c @@ -314,6 +314,7 @@ error_free: static dc_status_t oceanic_veo250_device_close (dc_device_t *abstract) { + dc_status_t status = DC_STATUS_SUCCESS; oceanic_veo250_device_t *device = (oceanic_veo250_device_t*) abstract; // Switch the device back to surface mode. @@ -321,14 +322,13 @@ oceanic_veo250_device_close (dc_device_t *abstract) // Close the device. if (serial_close (device->port) == -1) { - free (device); - return DC_STATUS_IO; + dc_status_set_error(&status, DC_STATUS_IO); } // Free memory. free (device); - return DC_STATUS_SUCCESS; + return status; } diff --git a/src/oceanic_vtpro.c b/src/oceanic_vtpro.c index 92faf7a..0bce08e 100644 --- a/src/oceanic_vtpro.c +++ b/src/oceanic_vtpro.c @@ -357,6 +357,7 @@ error_free: static dc_status_t oceanic_vtpro_device_close (dc_device_t *abstract) { + dc_status_t status = DC_STATUS_SUCCESS; oceanic_vtpro_device_t *device = (oceanic_vtpro_device_t*) abstract; // Switch the device back to surface mode. @@ -364,14 +365,13 @@ oceanic_vtpro_device_close (dc_device_t *abstract) // Close the device. if (serial_close (device->port) == -1) { - free (device); - return DC_STATUS_IO; + dc_status_set_error(&status, DC_STATUS_IO); } // Free memory. free (device); - return DC_STATUS_SUCCESS; + return status; } diff --git a/src/reefnet_sensus.c b/src/reefnet_sensus.c index ec0ee07..c4cc685 100644 --- a/src/reefnet_sensus.c +++ b/src/reefnet_sensus.c @@ -154,6 +154,7 @@ error_free: static dc_status_t reefnet_sensus_device_close (dc_device_t *abstract) { + dc_status_t status = DC_STATUS_SUCCESS; reefnet_sensus_device_t *device = (reefnet_sensus_device_t*) abstract; // Safely close the connection if the last handshake was @@ -163,14 +164,13 @@ reefnet_sensus_device_close (dc_device_t *abstract) // Close the device. if (serial_close (device->port) == -1) { - free (device); - return DC_STATUS_IO; + dc_status_set_error(&status, DC_STATUS_IO); } // Free memory. free (device); - return DC_STATUS_SUCCESS; + return status; } diff --git a/src/reefnet_sensuspro.c b/src/reefnet_sensuspro.c index d20f432..f1bb9ca 100644 --- a/src/reefnet_sensuspro.c +++ b/src/reefnet_sensuspro.c @@ -132,18 +132,18 @@ error_free: static dc_status_t reefnet_sensuspro_device_close (dc_device_t *abstract) { + dc_status_t status = DC_STATUS_SUCCESS; reefnet_sensuspro_device_t *device = (reefnet_sensuspro_device_t*) abstract; // Close the device. if (serial_close (device->port) == -1) { - free (device); - return DC_STATUS_IO; + dc_status_set_error(&status, DC_STATUS_IO); } // Free memory. free (device); - return DC_STATUS_SUCCESS; + return status; } diff --git a/src/reefnet_sensusultra.c b/src/reefnet_sensusultra.c index f16c535..c52d7b3 100644 --- a/src/reefnet_sensusultra.c +++ b/src/reefnet_sensusultra.c @@ -141,18 +141,18 @@ error_free: static dc_status_t reefnet_sensusultra_device_close (dc_device_t *abstract) { + dc_status_t status = DC_STATUS_SUCCESS; reefnet_sensusultra_device_t *device = (reefnet_sensusultra_device_t*) abstract; // Close the device. if (serial_close (device->port) == -1) { - free (device); - return DC_STATUS_IO; + dc_status_set_error(&status, DC_STATUS_IO); } // Free memory. free (device); - return DC_STATUS_SUCCESS; + return status; } diff --git a/src/serial_posix.c b/src/serial_posix.c index ee9312f..ffd4047 100644 --- a/src/serial_posix.c +++ b/src/serial_posix.c @@ -195,15 +195,15 @@ error_free: int serial_close (serial_t *device) { + int errcode = 0; + if (device == NULL) return 0; // Restore the initial terminal attributes. if (tcsetattr (device->fd, TCSANOW, &device->tty) != 0) { SYSERROR (device->context, errno); - close (device->fd); - free (device); - return -1; + errcode = -1; } #ifndef ENABLE_PTY @@ -214,14 +214,13 @@ serial_close (serial_t *device) // Close the device. if (close (device->fd) != 0) { SYSERROR (device->context, errno); - free (device); - return -1; + errcode = -1; } // Free memory. free (device); - return 0; + return errcode; } // diff --git a/src/serial_win32.c b/src/serial_win32.c index 00aeb4d..56c3866 100644 --- a/src/serial_win32.c +++ b/src/serial_win32.c @@ -181,6 +181,8 @@ error_free: int serial_close (serial_t *device) { + int errcode = 0; + if (device == NULL) return 0; @@ -188,22 +190,19 @@ serial_close (serial_t *device) if (!SetCommState (device->hFile, &device->dcb) || !SetCommTimeouts (device->hFile, &device->timeouts)) { SYSERROR (device->context, GetLastError ()); - CloseHandle (device->hFile); - free (device); - return -1; + errcode = -1; } // Close the device. if (!CloseHandle (device->hFile)) { SYSERROR (device->context, GetLastError ()); - free (device); - return -1; + errcode = -1; } // Free memory. free (device); - return 0; + return errcode; } // diff --git a/src/suunto_d9.c b/src/suunto_d9.c index c0903bb..708070a 100644 --- a/src/suunto_d9.c +++ b/src/suunto_d9.c @@ -215,18 +215,18 @@ error_free: static dc_status_t suunto_d9_device_close (dc_device_t *abstract) { + dc_status_t status = DC_STATUS_SUCCESS; suunto_d9_device_t *device = (suunto_d9_device_t*) abstract; // Close the device. if (serial_close (device->port) == -1) { - free (device); - return DC_STATUS_IO; + dc_status_set_error(&status, DC_STATUS_IO); } // Free memory. free (device); - return DC_STATUS_SUCCESS; + return status; } diff --git a/src/suunto_eon.c b/src/suunto_eon.c index e71e315..01b2743 100644 --- a/src/suunto_eon.c +++ b/src/suunto_eon.c @@ -135,18 +135,18 @@ error_free: static dc_status_t suunto_eon_device_close (dc_device_t *abstract) { + dc_status_t status = DC_STATUS_SUCCESS; suunto_eon_device_t *device = (suunto_eon_device_t*) abstract; // Close the device. if (serial_close (device->port) == -1) { - free (device); - return DC_STATUS_IO; + dc_status_set_error(&status, DC_STATUS_IO); } // Free memory. free (device); - return DC_STATUS_SUCCESS; + return status; } diff --git a/src/suunto_solution.c b/src/suunto_solution.c index 421a090..2a2e8de 100644 --- a/src/suunto_solution.c +++ b/src/suunto_solution.c @@ -129,18 +129,18 @@ error_free: static dc_status_t suunto_solution_device_close (dc_device_t *abstract) { + dc_status_t status = DC_STATUS_SUCCESS; suunto_solution_device_t *device = (suunto_solution_device_t*) abstract; // Close the device. if (serial_close (device->port) == -1) { - free (device); - return DC_STATUS_IO; + dc_status_set_error(&status, DC_STATUS_IO); } // Free memory. free (device); - return DC_STATUS_SUCCESS; + return status; } diff --git a/src/suunto_vyper.c b/src/suunto_vyper.c index 03fbc4a..a6bbfc1 100644 --- a/src/suunto_vyper.c +++ b/src/suunto_vyper.c @@ -161,18 +161,18 @@ error_free: static dc_status_t suunto_vyper_device_close (dc_device_t *abstract) { + dc_status_t status = DC_STATUS_SUCCESS; suunto_vyper_device_t *device = (suunto_vyper_device_t*) abstract; // Close the device. if (serial_close (device->port) == -1) { - free (device); - return DC_STATUS_IO; + dc_status_set_error(&status, DC_STATUS_IO); } // Free memory. free (device); - return DC_STATUS_SUCCESS; + return status; } diff --git a/src/suunto_vyper2.c b/src/suunto_vyper2.c index efba846..66d3859 100644 --- a/src/suunto_vyper2.c +++ b/src/suunto_vyper2.c @@ -167,18 +167,18 @@ error_free: static dc_status_t suunto_vyper2_device_close (dc_device_t *abstract) { + dc_status_t status = DC_STATUS_SUCCESS; suunto_vyper2_device_t *device = (suunto_vyper2_device_t*) abstract; // Close the device. if (serial_close (device->port) == -1) { - free (device); - return DC_STATUS_IO; + dc_status_set_error(&status, DC_STATUS_IO); } // Free memory. free (device); - return DC_STATUS_SUCCESS; + return status; } diff --git a/src/uwatec_aladin.c b/src/uwatec_aladin.c index 796e867..d4b1c83 100644 --- a/src/uwatec_aladin.c +++ b/src/uwatec_aladin.c @@ -142,18 +142,18 @@ error_free: static dc_status_t uwatec_aladin_device_close (dc_device_t *abstract) { + dc_status_t status = DC_STATUS_SUCCESS; uwatec_aladin_device_t *device = (uwatec_aladin_device_t*) abstract; // Close the device. if (serial_close (device->port) == -1) { - free (device); - return DC_STATUS_IO; + dc_status_set_error(&status, DC_STATUS_IO); } // Free memory. free (device); - return DC_STATUS_SUCCESS; + return status; } diff --git a/src/uwatec_memomouse.c b/src/uwatec_memomouse.c index 9608cb5..12fc7a8 100644 --- a/src/uwatec_memomouse.c +++ b/src/uwatec_memomouse.c @@ -141,18 +141,18 @@ error_free: static dc_status_t uwatec_memomouse_device_close (dc_device_t *abstract) { + dc_status_t status = DC_STATUS_SUCCESS; uwatec_memomouse_device_t *device = (uwatec_memomouse_device_t*) abstract; // Close the device. if (serial_close (device->port) == -1) { - free (device); - return DC_STATUS_IO; + dc_status_set_error(&status, DC_STATUS_IO); } // Free memory. free (device); - return DC_STATUS_SUCCESS; + return status; } diff --git a/src/uwatec_meridian.c b/src/uwatec_meridian.c index 4828a32..3689592 100644 --- a/src/uwatec_meridian.c +++ b/src/uwatec_meridian.c @@ -252,18 +252,18 @@ error_free: static dc_status_t uwatec_meridian_device_close (dc_device_t *abstract) { + dc_status_t status = DC_STATUS_SUCCESS; uwatec_meridian_device_t *device = (uwatec_meridian_device_t*) abstract; // Close the device. if (serial_close (device->port) == -1) { - free (device); - return DC_STATUS_IO; + dc_status_set_error(&status, DC_STATUS_IO); } // Free memory. free (device); - return DC_STATUS_SUCCESS; + return status; } diff --git a/src/uwatec_smart.c b/src/uwatec_smart.c index 2b327ba..7d27cb5 100644 --- a/src/uwatec_smart.c +++ b/src/uwatec_smart.c @@ -216,18 +216,18 @@ error_free: static dc_status_t uwatec_smart_device_close (dc_device_t *abstract) { + dc_status_t status = DC_STATUS_SUCCESS; uwatec_smart_device_t *device = (uwatec_smart_device_t*) abstract; // Close the device. if (irda_socket_close (device->socket) == -1) { - free (device); - return DC_STATUS_IO; + dc_status_set_error(&status, DC_STATUS_IO); } // Free memory. free (device); - return DC_STATUS_SUCCESS; + return status; } diff --git a/src/zeagle_n2ition3.c b/src/zeagle_n2ition3.c index a98825a..2559a5a 100644 --- a/src/zeagle_n2ition3.c +++ b/src/zeagle_n2ition3.c @@ -201,18 +201,18 @@ error_free: static dc_status_t zeagle_n2ition3_device_close (dc_device_t *abstract) { + dc_status_t status = DC_STATUS_SUCCESS; zeagle_n2ition3_device_t *device = (zeagle_n2ition3_device_t*) abstract; // Close the device. if (serial_close (device->port) == -1) { - free (device); - return DC_STATUS_IO; + dc_status_set_error(&status, DC_STATUS_IO); } // Free memory. free (device); - return DC_STATUS_SUCCESS; + return status; }