Skip to content

Commit e9c1d91

Browse files
committed
Fix memory leak
1 parent fe49dc8 commit e9c1d91

File tree

3 files changed

+22
-22
lines changed

3 files changed

+22
-22
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
cmake_minimum_required(VERSION 3.1)
1+
cmake_minimum_required(VERSION 3.5)
22
project(slick_queue
33
VERSION 0.1.0
44
DESCRIPTION "A C++ Lock-Free MPMC queue"

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2020-2021 SlickTech
3+
Copyright (c) 2020-2025 SlickTech
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

include/slick_queue.h

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/********************************************************************************
2-
* Copyright (c) 2020-0201, SlickTech
2+
* Copyright (c) 2020-2025 SlickTech
33
* All rights reserved
44
*
55
* This file is part of the SlickQueue. Redistribution and use in source and
@@ -83,15 +83,15 @@ class SlickQueue {
8383
CloseHandle(hMapFile_);
8484
hMapFile_ = nullptr;
8585
}
86-
86+
#endif
87+
8788
if (!use_shm_) {
8889
delete[] data_;
8990
data_ = nullptr;
9091

9192
delete[] control_;
9293
control_ = nullptr;
9394
}
94-
#endif
9595
}
9696

9797
bool own_buffer() const noexcept { return own_; }
@@ -165,21 +165,21 @@ class SlickQueue {
165165
#if defined(_MSC_VER)
166166
void allocate_shm_data(const char* const shm_name, bool open_only) {
167167
SIZE_T BF_SZ;
168-
HANDLE hMapFile = NULL;
168+
hMapFile_ = NULL;
169169
if (open_only) {
170-
hMapFile = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, (LPCWSTR)shm_name);
170+
hMapFile_ = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, (LPCWSTR)shm_name);
171171
own_ = false;
172172
auto err = GetLastError();
173-
if (hMapFile == NULL) {
173+
if (hMapFile_ == NULL) {
174174
throw std::runtime_error("Failed to open shm. err=" + std::to_string(err));
175175
}
176176

177-
void* lpvMem = MapViewOfFile(hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, 64);
178-
if (!lpvMem) {
177+
lpvMem_ = MapViewOfFile(hMapFile_, FILE_MAP_ALL_ACCESS, 0, 0, 64);
178+
if (!lpvMem_) {
179179
auto err = GetLastError();
180180
throw std::runtime_error("Failed to map shm. err=" + std::to_string(err));
181181
}
182-
mask_ = *reinterpret_cast<uint32_t*>(reinterpret_cast<uint8_t*>(lpvMem) + sizeof(std::atomic_uint_fast64_t)) - 1;
182+
mask_ = *reinterpret_cast<uint32_t*>(reinterpret_cast<uint8_t*>(lpvMem_) + sizeof(std::atomic_uint_fast64_t)) - 1;
183183
size_ = mask_ + 1025;
184184
BF_SZ = 64 + sizeof(slot) * size_ + sizeof(T) * size_;
185185
UnmapViewOfFile(lpvMem_);
@@ -188,7 +188,7 @@ class SlickQueue {
188188
else {
189189
BF_SZ = 64 + sizeof(slot) * size_ + sizeof(T) * size_;
190190

191-
hMapFile = CreateFileMapping(
191+
hMapFile_ = CreateFileMapping(
192192
INVALID_HANDLE_VALUE, // use paging file
193193
NULL, // default security
194194
PAGE_READWRITE, // read/write access
@@ -199,7 +199,7 @@ class SlickQueue {
199199

200200
own_ = false;
201201
auto err = GetLastError();
202-
if (hMapFile == NULL) {
202+
if (hMapFile_ == NULL) {
203203
throw std::runtime_error("Failed to create shm. err=" + std::to_string(err));
204204
}
205205

@@ -208,22 +208,22 @@ class SlickQueue {
208208
}
209209
}
210210

211-
void* lpvMem = MapViewOfFile(hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, BF_SZ);
212-
if (!lpvMem) {
211+
lpvMem_ = MapViewOfFile(hMapFile_, FILE_MAP_ALL_ACCESS, 0, 0, BF_SZ);
212+
if (!lpvMem_) {
213213
auto err = GetLastError();
214214
throw std::runtime_error("Failed to map shm. err=" + std::to_string(err));
215215
}
216216

217217
if (own_) {
218-
reserved_ = new (lpvMem) std::atomic_uint_fast64_t{ 0 };
219-
*reinterpret_cast<uint32_t*>(reinterpret_cast<uint8_t*>(lpvMem) + sizeof(std::atomic_uint_fast64_t)) = mask_ + 1;
220-
control_ = new ((uint8_t*)lpvMem + 64) slot[size_];
221-
data_ = new ((uint8_t*)lpvMem + 64 + sizeof(slot) * size_) T[size_];
218+
reserved_ = new (lpvMem_) std::atomic_uint_fast64_t{ 0 };
219+
*reinterpret_cast<uint32_t*>(reinterpret_cast<uint8_t*>(lpvMem_) + sizeof(std::atomic_uint_fast64_t)) = mask_ + 1;
220+
control_ = new ((uint8_t*)lpvMem_ + 64) slot[size_];
221+
data_ = new ((uint8_t*)lpvMem_ + 64 + sizeof(slot) * size_) T[size_];
222222
}
223223
else {
224-
reserved_ = reinterpret_cast<std::atomic_uint_fast64_t*>(lpvMem);
225-
control_ = reinterpret_cast<slot*>((uint8_t*)lpvMem + 64);
226-
data_ = reinterpret_cast<T*>((uint8_t*)lpvMem + 64 + sizeof(slot) * size_);
224+
reserved_ = reinterpret_cast<std::atomic_uint_fast64_t*>(lpvMem_);
225+
control_ = reinterpret_cast<slot*>((uint8_t*)lpvMem_ + 64);
226+
data_ = reinterpret_cast<T*>((uint8_t*)lpvMem_ + 64 + sizeof(slot) * size_);
227227
}
228228
}
229229
#else

0 commit comments

Comments
 (0)