diff --git a/Sming/Components/Network/src/Network/Http/Websocket/WebsocketConnection.cpp b/Sming/Components/Network/src/Network/Http/Websocket/WebsocketConnection.cpp index ec4d5253a2..e38363e4c0 100644 --- a/Sming/Components/Network/src/Network/Http/Websocket/WebsocketConnection.cpp +++ b/Sming/Components/Network/src/Network/Http/Websocket/WebsocketConnection.cpp @@ -299,12 +299,14 @@ bool WebsocketConnection::send(IDataSourceStream* source, ws_frame_type_t type, void WebsocketConnection::broadcast(const char* message, size_t length, ws_frame_type_t type) { - char* copy = new char[length]; - memcpy(copy, message, length); - std::shared_ptr data(copy, [](const char* ptr) { delete[] ptr; }); + std::shared_ptr data(new char[length]); + if(!data) { + return; + } + memcpy(data.get(), message, length); for(auto skt : websocketList) { - auto stream = new SharedMemoryStream(data, length); + auto stream = new SharedMemoryStream(data, length); skt->send(stream, type); } } diff --git a/Sming/Core/Data/Stream/LimitedMemoryStream.cpp b/Sming/Core/Data/Stream/LimitedMemoryStream.cpp index 89112e547f..5fe7fadf8b 100644 --- a/Sming/Core/Data/Stream/LimitedMemoryStream.cpp +++ b/Sming/Core/Data/Stream/LimitedMemoryStream.cpp @@ -50,7 +50,10 @@ int LimitedMemoryStream::seekFrom(int offset, SeekOrigin origin) size_t LimitedMemoryStream::write(const uint8_t* data, size_t size) { if(buffer == nullptr) { - buffer = new char[capacity]; + buffer = static_cast(malloc(capacity)); + if(buffer == nullptr) { + return 0; + } owned = true; } diff --git a/Sming/Core/Data/Stream/LimitedMemoryStream.h b/Sming/Core/Data/Stream/LimitedMemoryStream.h index 9457482d91..fb3148eba1 100644 --- a/Sming/Core/Data/Stream/LimitedMemoryStream.h +++ b/Sming/Core/Data/Stream/LimitedMemoryStream.h @@ -44,7 +44,7 @@ class LimitedMemoryStream : public ReadWriteStream ~LimitedMemoryStream() { if(owned) { - delete[] buffer; + free(buffer); } } diff --git a/Sming/Core/Data/Stream/SharedMemoryStream.h b/Sming/Core/Data/Stream/SharedMemoryStream.h index 6b44b55f35..7650189004 100644 --- a/Sming/Core/Data/Stream/SharedMemoryStream.h +++ b/Sming/Core/Data/Stream/SharedMemoryStream.h @@ -26,9 +26,9 @@ template class SharedMemoryStream : public IDataSourceStream public: /** @brief Constructor for use with pre-existing buffer * @param buffer - * @param capacity Size of buffer in elements + * @param size Size of buffer in elements */ - SharedMemoryStream(std::shared_ptr(buffer), size_t size) : buffer(buffer), capacity(size * sizeof(T)) + SharedMemoryStream(std::shared_ptr(buffer), size_t size) : buffer(buffer), capacity(size * sizeof(buffer[0])) { } diff --git a/tests/HostTests/modules/Stream.cpp b/tests/HostTests/modules/Stream.cpp index d360be6d1d..20199bb51e 100644 --- a/tests/HostTests/modules/Stream.cpp +++ b/tests/HostTests/modules/Stream.cpp @@ -171,8 +171,8 @@ class StreamTest : public TestGroup { // STL may perform one-time memory allocation for mutexes, etc. - std::shared_ptr data(new char[18]); - SharedMemoryStream(data, 18); + std::shared_ptr data(new char[18]); + SharedMemoryStream stream(data, 18); } auto memStart = MallocCount::getCurrent(); @@ -180,21 +180,21 @@ class StreamTest : public TestGroup TEST_CASE("SharedMemoryStream") { - char* message = new char[18]; - memcpy(message, "Wonderful data...", 18); - std::shared_ptr data(message, [&message](const char* p) { delete[] p; }); + const char* message = "Wonderful data..."; + const size_t msglen = strlen(message); + std::shared_ptr data(new char[msglen]); + memcpy(data.get(), message, msglen); debug_d("RefCount: %d", data.use_count()); - Vector*> list; + Vector*> list; for(unsigned i = 0; i < 4; i++) { - list.addElement(new SharedMemoryStream(data, strlen(message))); + list.addElement(new SharedMemoryStream(data, msglen)); } - for(unsigned i = 0; i < list.count(); i++) { + for(auto element : list) { constexpr size_t bufferSize{5}; char buffer[bufferSize]{}; - auto element = list[i]; String output; while(!element->isFinished()) {