Fixed some possible memory leaks
This commit is contained in:
parent
d1e79e6195
commit
d2f81b089d
7 changed files with 58 additions and 20 deletions
|
@ -8,4 +8,5 @@ endif(COMMAND cmake_policy)
|
|||
|
||||
ADD_SUBDIRECTORY(driver)
|
||||
ADD_SUBDIRECTORY(tdtool)
|
||||
ADD_SUBDIRECTORY(rfcmd)
|
||||
|
||||
|
|
|
@ -1,10 +1,14 @@
|
|||
#include "Device.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
/*
|
||||
* Constructor
|
||||
*/
|
||||
Device::Device()
|
||||
{
|
||||
#ifdef _LINUX
|
||||
strDevice = 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -12,6 +16,11 @@ Device::Device()
|
|||
*/
|
||||
Device::~Device(void)
|
||||
{
|
||||
#ifdef _LINUX
|
||||
if (strDevice != 0) {
|
||||
free(strDevice);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
|
@ -14,7 +14,7 @@ public:
|
|||
virtual int dim(unsigned char level);
|
||||
virtual int methods(char* strModel);
|
||||
|
||||
#ifndef _WINDOWS
|
||||
#ifdef _LINUX
|
||||
void setDevice(const char *device);
|
||||
protected:
|
||||
char *strDevice;
|
||||
|
|
|
@ -75,7 +75,10 @@ int WINAPI devTurnOn(int intDeviceId){
|
|||
TelldusSettings ts;
|
||||
Device* dev = ts.getDevice(intDeviceId);
|
||||
if(dev != NULL){
|
||||
int methods = dev->methods( ts.getModel( intDeviceId ) );
|
||||
char *model = ts.getModel( intDeviceId );
|
||||
int methods = dev->methods( model );
|
||||
free(model);
|
||||
|
||||
int retval = 0;
|
||||
|
||||
if ( !(methods & TELLSTICK_TURNON) ) {
|
||||
|
@ -109,7 +112,9 @@ int WINAPI devTurnOff(int intDeviceId){
|
|||
TelldusSettings ts;
|
||||
Device* dev = ts.getDevice(intDeviceId);
|
||||
if(dev != NULL){
|
||||
int methods = dev->methods( ts.getModel( intDeviceId ) );
|
||||
char *model = ts.getModel( intDeviceId );
|
||||
int methods = dev->methods( model );
|
||||
free(model);
|
||||
int retval = 0;
|
||||
|
||||
if ( !(methods & TELLSTICK_TURNOFF) ) {
|
||||
|
@ -143,7 +148,9 @@ int WINAPI devBell(int intDeviceId){
|
|||
TelldusSettings ts;
|
||||
Device* dev = ts.getDevice(intDeviceId);
|
||||
if(dev != NULL){
|
||||
int methods = dev->methods( ts.getModel( intDeviceId ) );
|
||||
char *model = ts.getModel( intDeviceId );
|
||||
int methods = dev->methods( model );
|
||||
free(model);
|
||||
int retval = 0;
|
||||
|
||||
if ( !(methods & TELLSTICK_BELL) ) {
|
||||
|
@ -177,7 +184,9 @@ int WINAPI devDim(int intDeviceId, unsigned char level){
|
|||
TelldusSettings ts;
|
||||
Device* dev = ts.getDevice(intDeviceId);
|
||||
if(dev != NULL){
|
||||
int methods = dev->methods( ts.getModel( intDeviceId ) );
|
||||
char *model = ts.getModel( intDeviceId );
|
||||
int methods = dev->methods( model );
|
||||
free(model);
|
||||
int retval = 0;
|
||||
|
||||
if ( !(methods & TELLSTICK_DIM) ) {
|
||||
|
@ -451,7 +460,7 @@ char * WINAPI devGetErrorString(int intErrorNo) {
|
|||
strReturn = "Unknown error";
|
||||
} else {
|
||||
// Copy the error string to strReturn
|
||||
strReturn = (char *)malloc( sizeof(char) * strlen(responses[intErrorNo]) );
|
||||
strReturn = (char *)malloc( sizeof(char) * (strlen(responses[intErrorNo])+1) );
|
||||
strcpy( strReturn, responses[intErrorNo] );
|
||||
}
|
||||
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
#include "../DeviceSartano.h"
|
||||
#include "../DeviceIkea.h"
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
/*
|
||||
* Get the requested device
|
||||
|
@ -21,25 +22,35 @@ Device* TelldusSettings::getDevice(int intDeviceId){
|
|||
char *strHouse = getArgument(intDeviceId, "nexa_house");
|
||||
char *strCode = getArgument(intDeviceId, "nexa_unit");
|
||||
dev = new DeviceNexa(strHouse, strCode);
|
||||
|
||||
free(strHouse);
|
||||
free(strCode);
|
||||
|
||||
} else if (strcmp(vendor, "Waveman") == 0) {
|
||||
char *strHouse = getArgument(intDeviceId, "nexa_house");
|
||||
char *strCode = getArgument(intDeviceId, "nexa_unit");
|
||||
dev = new DeviceWaveman(strHouse, strCode);
|
||||
free(strHouse);
|
||||
free(strCode);
|
||||
|
||||
} else if (strcmp(vendor, "Sartano") == 0) {
|
||||
char *strCode = getArgument(intDeviceId, "sartano_code");
|
||||
dev = new DeviceSartano(strCode);
|
||||
free(strCode);
|
||||
|
||||
} else if (strcmp(vendor, "Ikea") == 0) {
|
||||
char *strSystem = getArgument(intDeviceId, "ikea_system");
|
||||
char *strUnits = getArgument(intDeviceId, "ikea_units");
|
||||
char *strFade = getArgument(intDeviceId, "ikea_fade");
|
||||
dev = new DeviceIkea(strSystem, strUnits, strFade);
|
||||
free(strSystem);
|
||||
free(strUnits);
|
||||
free(strFade);
|
||||
|
||||
} else {
|
||||
free(vendor);
|
||||
return NULL;
|
||||
}
|
||||
free(vendor);
|
||||
|
||||
#ifdef _LINUX
|
||||
dev->setDevice( getSetting("deviceNode") );
|
||||
|
|
|
@ -32,8 +32,6 @@ TelldusSettings::TelldusSettings(void)
|
|||
{
|
||||
d = new privateVars();
|
||||
readConfig(&d->cfg);
|
||||
// printf("Nu: %d\n", cfg_size(d->cfg, "device"));
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -43,6 +41,7 @@ TelldusSettings::~TelldusSettings(void)
|
|||
{
|
||||
if (d->cfg > 0)
|
||||
cfg_free(d->cfg);
|
||||
delete d;
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -175,7 +174,7 @@ char *TelldusSettings::getStringSetting(int intDeviceId, const char* name, bool
|
|||
if (strSetting == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
char *strReturn = (char *)malloc(strlen(strSetting) * sizeof(char));
|
||||
char *strReturn = (char *)malloc((strlen(strSetting)+1) * sizeof(char));
|
||||
strcpy(strReturn, strSetting);
|
||||
return strReturn;
|
||||
}
|
||||
|
|
|
@ -50,19 +50,21 @@ void print_usage( char *name ) {
|
|||
printf(" Both device-id and name is outputed with the --list option\n");
|
||||
printf("\n");
|
||||
printf("Written by Micke Prag <micke.prag@telldus.se>\n");
|
||||
printf("Report bugs to <info.tech@telldus.se>\n");
|
||||
printf("\n");
|
||||
printf("Copyright (C) 2008 Telldus Technologies AB\n");
|
||||
}
|
||||
|
||||
void print_version() {
|
||||
printf("tdtool 2.0\n");
|
||||
printf("\n");
|
||||
printf("Report bugs to <info.tech@telldus.se>\n");
|
||||
}
|
||||
|
||||
void print_device( int index ) {
|
||||
int intId = devGetDeviceId(index);
|
||||
char *name = devGetName(intId);
|
||||
printf("%i\t%s\n", intId, name);
|
||||
free(name);
|
||||
}
|
||||
|
||||
void list_devices() {
|
||||
|
@ -85,8 +87,10 @@ int find_device( char *device ) {
|
|||
char *name = devGetName( id );
|
||||
if (strcasecmp(name, device) == 0) {
|
||||
deviceId = id;
|
||||
free(name);
|
||||
break;
|
||||
}
|
||||
free(name);
|
||||
index++;
|
||||
}
|
||||
}
|
||||
|
@ -101,13 +105,12 @@ void switch_device( bool turnOn, char *device ) {
|
|||
}
|
||||
|
||||
char *name = devGetName( deviceId );
|
||||
if (turnOn) {
|
||||
int retval = devTurnOn( deviceId );
|
||||
printf("Turning on device: %i %s - %s\n", deviceId, name, devGetErrorString(retval));
|
||||
} else {
|
||||
int retval = devTurnOff( deviceId );
|
||||
printf("Turning off device: %i %s - %s\n", deviceId, name, devGetErrorString(retval));
|
||||
}
|
||||
int retval = (turnOn ? devTurnOn( deviceId ) : devTurnOff( deviceId ));
|
||||
char *errorString = devGetErrorString(retval);
|
||||
|
||||
printf("Turning %s device: %i %s - %s\n", (turnOn ? "on" : "off"), deviceId, name, errorString);
|
||||
free(name);
|
||||
free(errorString);
|
||||
sleep(1);
|
||||
}
|
||||
|
||||
|
@ -124,7 +127,10 @@ void dim_device( char *device, int level ) {
|
|||
|
||||
char *name = devGetName( deviceId );
|
||||
int retval = devDim( deviceId, (unsigned char)level );
|
||||
printf("Dimming device: %i %s to %i - %s\n", deviceId, name, level, devGetErrorString(retval));
|
||||
char *errorString = devGetErrorString(retval);
|
||||
printf("Dimming device: %i %s to %i - %s\n", deviceId, name, level, errorString);
|
||||
free(name);
|
||||
free(errorString);
|
||||
sleep(1);
|
||||
}
|
||||
|
||||
|
@ -137,7 +143,10 @@ void bell_device( char *device ) {
|
|||
|
||||
char *name = devGetName( deviceId );
|
||||
int retval = devBell( deviceId );
|
||||
printf("Sending bell to: %i %s - %s\n", deviceId, name, devGetErrorString(retval));
|
||||
char *errorString = devGetErrorString(retval);
|
||||
printf("Sending bell to: %i %s - %s\n", deviceId, name, errorString);
|
||||
free(name);
|
||||
free(errorString);
|
||||
sleep(1);
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue