Scan to open on your phone
Point a camera at the QR code - it opens this exact paste, no app needed.
https://codepastes.com/zzgrsmqs4tyj1ztvsdfsafPlain Text
Hi, no idea why this is happening. I've been looking into it and I cant find much.
I am on MinGW and I have the following example cases to show of std::jthread causing a memory leak:
Here is the context:
```cpp
enum class DataFileType {
ActiviewSamples = 0,
DrivingSimulatorEvents = 1,
};
struct DataFile {
std::string filename;
std::jthread listener_thread;
};
std::unordered_map<DataFileType, DataFile> dataFiles;
``` (members of a class)
Here is some code that I run on command that does not leak memory that creates std::jthreads that do nothing and then cleans them up right after:
```cpp
// no memory leak
dataFiles.emplace(ft, DataFile{
filename});
for(auto&[type, df] : dataFiles) {
// spdlog::info("Joining {} thread", dataFileTypeStr.at((uint32_t)type));
if(df.listener_thread.joinable()) {
df.listener_thread.request_stop();
df.listener_thread.join();
}
}
dataFiles.clear();
```
Here is some more code that also does not cause a memory leak, the only difference is putting it in an infinite while loop and actually constructing a jthread with a lambda:
```cpp
// no memory leak
while(true) {
dataFiles.emplace(ft, DataFile{
filename,
std::jthread([](std::stop_token st) {
// Session::data_file_async_fn[(size_t)ft](stream, config, st);
})
});
for(auto&[type, df] : dataFiles) {
// spdlog::info("Joining {} thread", dataFileTypeStr.at((uint32_t)type));
if(df.listener_thread.joinable()) {
df.listener_thread.request_stop();
df.listener_thread.join();
}
}
dataFiles.clear();
}
```
And here is the code that causes a memory leak when i run it on command, actually initializing a jthread but not in a while loop:
```cpp
// memory leak
dataFiles.emplace(ft, DataFile{
filename,
std::jthread([](std::stop_token st) {
// Session::data_file_async_fn[(size_t)ft](stream, config, st);
})
});
for(auto&[type, df] : dataFiles) {
// spdlog::info("Joining {} thread", dataFileTypeStr.at((uint32_t)type));
if(df.listener_thread.joinable()) {
df.listener_thread.request_stop();
df.listener_thread.join();
}
}
dataFiles.clear();
```
not sure if I've just made a silly mistake but im not sure whats going on, any dialogue appreciated2,541 chars · codepastes.com