From: Lars Op den Kamp Date: Thu, 31 May 2012 19:50:48 +0000 (+0200) Subject: cec: safe deletes for members X-Git-Tag: upstream/2.2.0~1^2~23^2^2~21 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=c9d154855efd508e1a4225841616504087f88e6e;p=deb_libcec.git cec: safe deletes for members --- diff --git a/src/lib/CECProcessor.cpp b/src/lib/CECProcessor.cpp index 3bfa061..016d88c 100644 --- a/src/lib/CECProcessor.cpp +++ b/src/lib/CECProcessor.cpp @@ -43,6 +43,7 @@ #include "LibCEC.h" #include "CECClient.h" #include "platform/util/timeutils.h" +#include "platform/util/util.h" using namespace CEC; using namespace std; @@ -66,7 +67,7 @@ CCECProcessor::CCECProcessor(CLibCEC *libcec) : CCECProcessor::~CCECProcessor(void) { Close(); - delete m_busDevices; + DELETE_AND_NULL(m_busDevices); } bool CCECProcessor::Start(const char *strPort, uint16_t iBaudRate /* = CEC_SERIAL_DEFAULT_BAUDRATE */, uint32_t iTimeoutMs /* = CEC_DEFAULT_CONNECT_TIMEOUT */) @@ -98,21 +99,13 @@ void CCECProcessor::Close(void) StopThread(); // close the connection - if (m_communication) - { - delete m_communication; - m_communication = NULL; - } + DELETE_AND_NULL(m_communication); } void CCECProcessor::ResetMembers(void) { // close the connection - if (m_communication) - { - delete m_communication; - m_communication = NULL; - } + DELETE_AND_NULL(m_communication); // reset the other members to the initial state m_iStandardLineTimeout = 3; @@ -517,7 +510,7 @@ bool CCECProcessor::StartBootloader(const char *strPort /* = NULL */) if (comm->IsOpen()) { bReturn = comm->StartBootloader(); - delete comm; + DELETE_AND_NULL(comm); } return bReturn; } diff --git a/src/lib/LibCEC.cpp b/src/lib/LibCEC.cpp index b64cdc7..b5dc74d 100644 --- a/src/lib/LibCEC.cpp +++ b/src/lib/LibCEC.cpp @@ -41,6 +41,7 @@ #include "devices/CECTV.h" #include "platform/util/timeutils.h" #include "platform/util/StdString.h" +#include "platform/util/util.h" #include "CECClient.h" @@ -48,6 +49,7 @@ using namespace std; using namespace CEC; using namespace PLATFORM; +//TODO replace deprecated constructor in 2.0 CLibCEC::CLibCEC(const char *UNUSED(strDeviceName), cec_device_type_list UNUSED(types), uint16_t UNUSED(iPhysicalAddress) /* = 0 */) : m_iStartTime(GetTimeMs()), m_client(NULL) @@ -55,6 +57,7 @@ CLibCEC::CLibCEC(const char *UNUSED(strDeviceName), cec_device_type_list UNUSED( m_cec = new CCECProcessor(this); } +//TODO replace deprecated constructor in 2.0 CLibCEC::CLibCEC(libcec_configuration *UNUSED(configuration)) : m_iStartTime(GetTimeMs()), m_client(NULL) @@ -68,8 +71,7 @@ CLibCEC::~CLibCEC(void) UnregisterClients(); // delete the adapter connection - delete m_cec; - m_cec = NULL; + DELETE_AND_NULL(m_cec); } bool CLibCEC::Open(const char *strPort, uint32_t iTimeoutMs /* = CEC_DEFAULT_CONNECT_TIMEOUT */) @@ -932,8 +934,7 @@ void CLibCEC::UnregisterClients(void) m_clients.clear(); - delete m_client; - m_client = NULL; + DELETE_AND_NULL(m_client); } void * CECInitialise(libcec_configuration *configuration) @@ -996,7 +997,7 @@ bool CECStartBootloader(void) void CECDestroy(CEC::ICECAdapter *instance) { - delete instance; + DELETE_AND_NULL(instance); } bool CLibCEC::GetDeviceInformation(const char *strPort, libcec_configuration *config, uint32_t iTimeoutMs /* = CEC_DEFAULT_CONNECT_TIMEOUT */) diff --git a/src/lib/adapter/USBCECAdapterCommands.cpp b/src/lib/adapter/USBCECAdapterCommands.cpp index bdc2412..87a91e9 100644 --- a/src/lib/adapter/USBCECAdapterCommands.cpp +++ b/src/lib/adapter/USBCECAdapterCommands.cpp @@ -65,6 +65,7 @@ cec_datapacket CUSBCECAdapterCommands::RequestSetting(cec_adapter_messagecode ms retVal.Shift(2); // shift out start and msgcode retVal.size -= 1; // remove end } + delete message; return retVal; } diff --git a/src/lib/adapter/USBCECAdapterCommunication.cpp b/src/lib/adapter/USBCECAdapterCommunication.cpp index 801ebc5..33eb260 100644 --- a/src/lib/adapter/USBCECAdapterCommunication.cpp +++ b/src/lib/adapter/USBCECAdapterCommunication.cpp @@ -35,6 +35,7 @@ #include "USBCECAdapterMessageQueue.h" #include "../platform/sockets/serialport.h" #include "../platform/util/timeutils.h" +#include "../platform/util/util.h" #include "../LibCEC.h" #include "../CECProcessor.h" @@ -70,9 +71,9 @@ CUSBCECAdapterCommunication::CUSBCECAdapterCommunication(IAdapterCommunicationCa CUSBCECAdapterCommunication::~CUSBCECAdapterCommunication(void) { Close(); - delete m_commands; - delete m_adapterMessageQueue; - delete m_port; + DELETE_AND_NULL(m_commands); + DELETE_AND_NULL(m_adapterMessageQueue); + DELETE_AND_NULL(m_port); } bool CUSBCECAdapterCommunication::Open(uint32_t iTimeoutMs /* = CEC_DEFAULT_CONNECT_TIMEOUT */, bool bSkipChecks /* = false */, bool bStartListening /* = true */) @@ -195,10 +196,7 @@ void CUSBCECAdapterCommunication::Close(void) m_adapterMessageQueue->Clear(); /* stop and delete the ping thread */ - if (m_pingThread) - m_pingThread->StopThread(0); - delete m_pingThread; - m_pingThread = NULL; + DELETE_AND_NULL(m_pingThread); /* close and delete the com port connection */ if (m_port) diff --git a/src/lib/devices/CECBusDevice.cpp b/src/lib/devices/CECBusDevice.cpp index b192391..3377960 100644 --- a/src/lib/devices/CECBusDevice.cpp +++ b/src/lib/devices/CECBusDevice.cpp @@ -38,6 +38,7 @@ #include "../implementations/VLCommandHandler.h" #include "../LibCEC.h" #include "../platform/util/timeutils.h" +#include "../platform/util/util.h" #include "CECAudioSystem.h" #include "CECPlaybackDevice.h" @@ -83,7 +84,7 @@ CCECBusDevice::CCECBusDevice(CCECProcessor *processor, cec_logical_address iLogi CCECBusDevice::~CCECBusDevice(void) { - delete m_handler; + DELETE_AND_NULL(m_handler); } bool CCECBusDevice::ReplaceHandler(bool bActivateSource /* = true */) @@ -105,7 +106,7 @@ bool CCECBusDevice::ReplaceHandler(bool bActivateSource /* = true */) if (CCECCommandHandler::HasSpecificHandler(m_vendor)) { LIB_CEC->AddLog(CEC_LOG_DEBUG, "replacing the command handler for device '%s' (%x)", GetLogicalAddressName(), GetLogicalAddress()); - delete m_handler; + DELETE_AND_NULL(m_handler); switch (m_vendor) { diff --git a/src/lib/implementations/CECCommandHandler.cpp b/src/lib/implementations/CECCommandHandler.cpp index 40d7ec3..376cfc4 100644 --- a/src/lib/implementations/CECCommandHandler.cpp +++ b/src/lib/implementations/CECCommandHandler.cpp @@ -37,6 +37,7 @@ #include "../CECClient.h" #include "../CECProcessor.h" #include "../LibCEC.h" +#include "../platform/util/util.h" using namespace CEC; using namespace std; @@ -60,7 +61,7 @@ CCECCommandHandler::CCECCommandHandler(CCECBusDevice *busDevice) : CCECCommandHandler::~CCECCommandHandler(void) { - delete m_waitForResponse; + DELETE_AND_NULL(m_waitForResponse); } bool CCECCommandHandler::HandleCommand(const cec_command &command) diff --git a/src/lib/platform/util/util.h b/src/lib/platform/util/util.h new file mode 100644 index 0000000..2b85c63 --- /dev/null +++ b/src/lib/platform/util/util.h @@ -0,0 +1,34 @@ +#pragma once +/* + * This file is part of the libCEC(R) library. + * + * libCEC(R) is Copyright (C) 2011-2012 Pulse-Eight Limited. All rights reserved. + * libCEC(R) is an original work, containing original code. + * + * libCEC(R) is a trademark of Pulse-Eight Limited. + * + * This program is dual-licensed; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * + * Alternatively, you can license this library under a commercial license, + * please contact Pulse-Eight Licensing for more information. + * + * For more information contact: + * Pulse-Eight Licensing + * http://www.pulse-eight.com/ + * http://www.pulse-eight.net/ + */ + +#define DELETE_AND_NULL(t) while (t) { delete (t); (t) = NULL; }