Skip to content
This repository has been archived by the owner on Nov 8, 2021. It is now read-only.

Commit

Permalink
close the mapped file handle
Browse files Browse the repository at this point in the history
added some build dirs to gitignore
  • Loading branch information
blagoev committed Nov 11, 2020
1 parent c162950 commit e61e054
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,5 @@ compile_commands.json

# vim
*.swp
/openssl/
/out/build/
16 changes: 11 additions & 5 deletions src/impl/windows/external_commit_helper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,22 @@ class SharedMemory {
//assume another process have already initialzied the shared memory
bool shouldInit = false;

HANDLE mapping = OpenFileMappingW(FILE_MAP_ALL_ACCESS, FALSE, name);
m_mapped_file = OpenFileMappingW(FILE_MAP_ALL_ACCESS, FALSE, name);
auto error = GetLastError();

if (mapping == NULL) {
mapping = CreateFileMappingW(INVALID_HANDLE_VALUE, nullptr, PAGE_READWRITE, 0, sizeof(T), name);
if (m_mapped_file == NULL) {
m_mapped_file = CreateFileMappingW(INVALID_HANDLE_VALUE, nullptr, PAGE_READWRITE, 0, sizeof(T), name);
error = GetLastError();

//init since this is the first process creating the shared memory
shouldInit = true;
}

if (mapping == NULL) {
if (m_mapped_file == NULL) {
throw std::system_error(error, std::system_category());
}

LPVOID view = MapViewOfFile(mapping, FILE_MAP_ALL_ACCESS, 0, 0, sizeof(T));
LPVOID view = MapViewOfFile(m_mapped_file, FILE_MAP_ALL_ACCESS, 0, 0, sizeof(T));
error = GetLastError();
if (view == NULL) {
throw std::system_error(error, std::system_category());
Expand All @@ -73,10 +73,16 @@ class SharedMemory {
UnmapViewOfFile(m_memory);
m_memory = nullptr;
}

if (m_mapped_file) {
CloseHandle(m_mapped_file);
m_mapped_file = nullptr;
}
}

private:
T* m_memory = nullptr;
HANDLE m_mapped_file = nullptr;
};
}

Expand Down

0 comments on commit e61e054

Please sign in to comment.