diff --git a/src/HomeSpan.cpp b/src/HomeSpan.cpp index d00be5d..c2d2a01 100644 --- a/src/HomeSpan.cpp +++ b/src/HomeSpan.cpp @@ -1581,13 +1581,17 @@ SpanService::SpanService(const char *type, const char *hapName, boolean isCustom SpanService::~SpanService(){ + while(Characteristics.rbegin()!=Characteristics.rend()) // delete all Characteristics in this Service + delete *Characteristics.rbegin(); + auto svc=accessory->Services.begin(); // find Service in containing Accessory vector and erase entry while((*svc)!=this) svc++; accessory->Services.erase(svc); - for(svc=homeSpan.Loops.begin(); (*svc)!=this && svc!=homeSpan.Loops.end(); svc++); // find and erase entry in Loop vector - if(svc!=homeSpan.Loops.end()){ + for(svc=homeSpan.Loops.begin(); svc!=homeSpan.Loops.end() && (*svc)!=this; svc++); // search for entry in Loop vector... + + if(svc!=homeSpan.Loops.end()){ // ...if it exists, erase it homeSpan.Loops.erase(svc); Serial.printf("Deleted Loop Entry\n"); } @@ -1682,6 +1686,24 @@ SpanCharacteristic::SpanCharacteristic(HapChar *hapChar, boolean isCustom){ /////////////////////////////// +SpanCharacteristic::~SpanCharacteristic(){ + + auto chr=service->Characteristics.begin(); // find Characteristic in containing Service vector and erase entry + while((*chr)!=this) + chr++; + service->Characteristics.erase(chr); + + free(ev); + free(desc); + free(unit); + free(validValues); + free(nvsKey); + + Serial.printf("Deleted Characteristic AID=%d IID=%d\n",aid,iid); +} + +/////////////////////////////// + int SpanCharacteristic::sprintfAttributes(char *cBuf, int flags){ int nBytes=0; diff --git a/src/HomeSpan.h b/src/HomeSpan.h index fe5f98d..c6e9bbc 100644 --- a/src/HomeSpan.h +++ b/src/HomeSpan.h @@ -282,7 +282,7 @@ struct SpanAccessory{ int iidCount=0; // running count of iid to use for Services and Characteristics associated with this Accessory vector Services; // vector of pointers to all Services in this Accessory - SpanAccessory(uint32_t aid=0); + SpanAccessory(uint32_t aid=0); // constructor ~SpanAccessory(); int sprintfAttributes(char *cBuf, int flags); // prints Accessory JSON database into buf, unless buf=NULL; return number of characters printed, excluding null terminator, even if buf=NULL @@ -362,6 +362,7 @@ struct SpanCharacteristic{ SpanService *service=NULL; // pointer to Service containing this Characteristic SpanCharacteristic(HapChar *hapChar, boolean isCustom=false); // contructor + ~SpanCharacteristic(); int sprintfAttributes(char *cBuf, int flags); // prints Characteristic JSON records into buf, according to flags mask; return number of characters printed, excluding null terminator StatusCode loadUpdate(char *val, char *ev); // load updated val/ev from PUT /characteristic JSON request. Return intiial HAP status code (checks to see if characteristic is found, is writable, etc.)