diff --git a/lib/BluetoothOTA/src/BluetoothSoftwareUpdate.cpp b/lib/BluetoothOTA/src/BluetoothSoftwareUpdate.cpp index ccf1bf5ce..a648364c7 100644 --- a/lib/BluetoothOTA/src/BluetoothSoftwareUpdate.cpp +++ b/lib/BluetoothOTA/src/BluetoothSoftwareUpdate.cpp @@ -135,3 +135,7 @@ BLEService *createUpdateService(BLEServer *server) return service; } + +void destroyUpdateService() { + resultC = NULL; +} \ No newline at end of file diff --git a/lib/BluetoothOTA/src/BluetoothSoftwareUpdate.h b/lib/BluetoothOTA/src/BluetoothSoftwareUpdate.h index 4f52c415c..b91e9c22d 100644 --- a/lib/BluetoothOTA/src/BluetoothSoftwareUpdate.h +++ b/lib/BluetoothOTA/src/BluetoothSoftwareUpdate.h @@ -4,4 +4,5 @@ BLEService *createUpdateService(BLEServer* server); +void destroyUpdateService(); void bluetoothRebootCheck(); \ No newline at end of file diff --git a/lib/BluetoothOTA/src/BluetoothUtil.cpp b/lib/BluetoothOTA/src/BluetoothUtil.cpp index be94e4ebb..e2833e436 100644 --- a/lib/BluetoothOTA/src/BluetoothUtil.cpp +++ b/lib/BluetoothOTA/src/BluetoothUtil.cpp @@ -192,7 +192,8 @@ class MySecurity : public BLESecurityCallbacks void deinitBLE() { batteryLevelC = NULL; // Don't let anyone generate bogus notifies - + destroyUpdateService(); + BLEDevice::deinit(false); btPool.reset(); } diff --git a/lib/BluetoothOTA/src/SimpleAllocator.cpp b/lib/BluetoothOTA/src/SimpleAllocator.cpp index 1fa3c203a..bc21cf12d 100644 --- a/lib/BluetoothOTA/src/SimpleAllocator.cpp +++ b/lib/BluetoothOTA/src/SimpleAllocator.cpp @@ -1,6 +1,8 @@ #include "SimpleAllocator.h" #include "assert.h" +SimpleAllocator *activeAllocator; + SimpleAllocator::SimpleAllocator() { reset(); } void *SimpleAllocator::alloc(size_t size) @@ -8,6 +10,7 @@ void *SimpleAllocator::alloc(size_t size) assert(nextFree + size <= sizeof(bytes)); void *res = &bytes[nextFree]; nextFree += size; + Serial.printf("Total simple allocs %u\n", nextFree); return res; } @@ -18,3 +21,34 @@ void *operator new(size_t size, SimpleAllocator &p) { return p.alloc(size); } + +AllocatorScope::AllocatorScope(SimpleAllocator &a) +{ + assert(!activeAllocator); + activeAllocator = &a; +} + +AllocatorScope::~AllocatorScope() +{ + assert(activeAllocator); + activeAllocator = NULL; +} + +/// Global new/delete, uses a simple allocator if it is in scope + +void *operator new(size_t sz) throw(std::bad_alloc) +{ + void *mem = activeAllocator ? activeAllocator->alloc(sz) : malloc(sz); + if (mem) + return mem; + else + throw std::bad_alloc(); +} + +void operator delete(void *ptr) throw() +{ + if (activeAllocator) + Serial.println("Warning: leaking an active allocator object"); // We don't properly handle this yet + else + free(ptr); +} \ No newline at end of file diff --git a/lib/BluetoothOTA/src/SimpleAllocator.h b/lib/BluetoothOTA/src/SimpleAllocator.h index 82c2dbce0..115cd2fa7 100644 --- a/lib/BluetoothOTA/src/SimpleAllocator.h +++ b/lib/BluetoothOTA/src/SimpleAllocator.h @@ -31,3 +31,12 @@ public: void *operator new(size_t size, SimpleAllocator &p); +/** + * Temporarily makes the specified Allocator be used for _all_ allocations. Useful when calling library routines + * that don't know about pools + */ +class AllocatorScope { +public: + AllocatorScope(SimpleAllocator &a); + ~AllocatorScope(); +}; diff --git a/src/MeshBluetoothService.cpp b/src/MeshBluetoothService.cpp index 143da9d1b..c1806a715 100644 --- a/src/MeshBluetoothService.cpp +++ b/src/MeshBluetoothService.cpp @@ -319,3 +319,7 @@ BLEService *createMeshBluetoothService(BLEServer *server) return service; } + +void destroyMeshBluetoothService() { + meshFromNumCharacteristic = NULL; +} \ No newline at end of file diff --git a/src/MeshBluetoothService.h b/src/MeshBluetoothService.h index d6f368e18..8c9330b9e 100644 --- a/src/MeshBluetoothService.h +++ b/src/MeshBluetoothService.h @@ -5,6 +5,7 @@ #include BLEService *createMeshBluetoothService(BLEServer* server); +void destroyMeshBluetoothService(); /** * Tell any bluetooth clients that the number of rx packets has changed diff --git a/src/main.ino b/src/main.ino index ed523b5c9..f0d5c580d 100644 --- a/src/main.ino +++ b/src/main.ino @@ -264,6 +264,10 @@ void setup() void initBluetooth() { DEBUG_MSG("Starting bluetooth\n"); + + // FIXME - we are leaking like crazy + // AllocatorScope scope(btPool); + BLEServer *serve = initBLE(getDeviceName(), HW_VENDOR, str(APP_VERSION)); // FIXME, use a real name based on the macaddr createMeshBluetoothService(serve); @@ -284,7 +288,8 @@ void setBluetoothEnable(bool on) } else { - // FIXME - we are leaking like crazy + // We have to totally teardown our bluetooth objects to prevent leaks + destroyMeshBluetoothService(); deinitBLE(); } }