From c391bb97a49fe111b0a1294018b380ad67240c49 Mon Sep 17 00:00:00 2001 From: Paul Hollinsky Date: Tue, 25 Sep 2018 18:58:37 -0400 Subject: [PATCH] Manage memory for the C interface in a much less hack way --- api/icsneoc/icsneoc.cpp | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/api/icsneoc/icsneoc.cpp b/api/icsneoc/icsneoc.cpp index fe305f5..09c10fc 100644 --- a/api/icsneoc/icsneoc.cpp +++ b/api/icsneoc/icsneoc.cpp @@ -19,11 +19,8 @@ using namespace icsneo; // Holds references for the shared_ptrs so they do not get freed until we're ready static std::vector> connectableFoundDevices, connectedDevices; -// Any shared_ptrs we've let go should be placed here so they're not accessed -static std::vector freedDevices; - -// We store an array of shared_ptr messages per device (by serial), this means we own the memory of the data pointer -static std::map>> polledMessageStorage; +// We store an array of shared_ptr messages per device, this is the owner of the shared_ptr on behalf of the C interface +static std::map>> polledMessageStorage; void icsneoFindAllDevices(neodevice_t* devices, size_t* count) { icsneoFreeUnconnectedDevices(); // Mark previous results as freed so they can no longer be connected to @@ -44,9 +41,6 @@ void icsneoFindAllDevices(neodevice_t* devices, size_t* count) { } void icsneoFreeUnconnectedDevices() { - for(auto& devptr : connectableFoundDevices) { - freedDevices.push_back(devptr.get()); - } connectableFoundDevices.clear(); } @@ -68,7 +62,15 @@ uint32_t icsneoSerialStringToNum(const char* str) { bool icsneoIsValidNeoDevice(const neodevice_t* device) { // If this neodevice_t was returned by a previous search, it will no longer be valid (as the underlying icsneo::Device is freed) - return std::find(freedDevices.begin(), freedDevices.end(), device->device) == freedDevices.end(); + for(auto& dev : connectedDevices) { + if(dev.get() == device->device) + return true; + } + for(auto& dev : connectableFoundDevices) { + if(dev.get() == device->device) + return true; + } + return false; } bool icsneoOpenDevice(const neodevice_t* device) { @@ -99,7 +101,7 @@ bool icsneoCloseDevice(const neodevice_t* device) { if(!device->device->close()) return false; - // We disconnected successfully, free the device and mark it as freed + // We disconnected successfully, free the device std::vector>::iterator> itemsToDelete; for(auto it = connectedDevices.begin(); it < connectedDevices.end(); it++) { if((*it).get() == device->device) @@ -107,8 +109,6 @@ bool icsneoCloseDevice(const neodevice_t* device) { } for(auto it : itemsToDelete) connectedDevices.erase(it); - - freedDevices.push_back(device->device); return true; } @@ -162,7 +162,7 @@ bool icsneoGetMessages(const neodevice_t* device, neomessage_t* messages, size_t return true; } - std::vector>& storage = polledMessageStorage[device->serial]; + std::vector>& storage = polledMessageStorage[device->device]; if(!device->device->getMessages(storage, *items)) return false;