Skip to content

Commit

Permalink
Fix mismatched new/delete issues with SharedMemoryStream
Browse files Browse the repository at this point in the history
  • Loading branch information
mikee47 committed Apr 14, 2024
1 parent 599b69e commit 8ed0253
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -301,10 +301,10 @@ void WebsocketConnection::broadcast(const char* message, size_t length, ws_frame
{
char* copy = new char[length];
memcpy(copy, message, length);
std::shared_ptr<const char> data(copy, [](const char* ptr) { delete[] ptr; });
std::shared_ptr<const char[]> data(copy);

for(auto skt : websocketList) {
auto stream = new SharedMemoryStream<const char>(data, length);
auto stream = new SharedMemoryStream<const char[]>(data, length);
skt->send(stream, type);
}
}
Expand Down
4 changes: 2 additions & 2 deletions Sming/Core/Data/Stream/SharedMemoryStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ template <typename T> 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<T>(buffer), size_t size) : buffer(buffer), capacity(size * sizeof(T))
SharedMemoryStream(std::shared_ptr<T>(buffer), size_t size) : buffer(buffer), capacity(size * sizeof(buffer[0]))
{
}

Expand Down
10 changes: 5 additions & 5 deletions tests/HostTests/modules/Stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,8 @@ class StreamTest : public TestGroup

{
// STL may perform one-time memory allocation for mutexes, etc.
std::shared_ptr<const char> data(new char[18]);
SharedMemoryStream<const char>(data, 18);
std::shared_ptr<const char[]> data(new char[18]);
SharedMemoryStream stream(data, 18);
}

auto memStart = MallocCount::getCurrent();
Expand All @@ -182,13 +182,13 @@ class StreamTest : public TestGroup
{
char* message = new char[18];
memcpy(message, "Wonderful data...", 18);
std::shared_ptr<const char> data(message, [&message](const char* p) { delete[] p; });
std::shared_ptr<const char[]> data(message);

debug_d("RefCount: %d", data.use_count());

Vector<SharedMemoryStream<const char>*> list;
Vector<SharedMemoryStream<const char[]>*> list;
for(unsigned i = 0; i < 4; i++) {
list.addElement(new SharedMemoryStream<const char>(data, strlen(message)));
list.addElement(new SharedMemoryStream<const char[]>(data, strlen(message)));
}

for(unsigned i = 0; i < list.count(); i++) {
Expand Down

0 comments on commit 8ed0253

Please sign in to comment.