Skip to content

Commit 10aeab0

Browse files
committed
Check ownership of MemoryMappedFile before cleaning file buffer
1 parent 4fd5722 commit 10aeab0

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

include/ReadoutCard/MemoryMappedFile.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,14 @@ class MemoryMappedFile
3232
std::string getFileName() const;
3333

3434
private:
35-
void map(const std::string& fileName, size_t fileSize);
35+
bool map(const std::string& fileName, size_t fileSize);
3636

3737
std::unique_ptr<MemoryMappedFileInternal> mInternal;
3838

3939
std::unique_ptr<Interprocess::Lock> mInterprocessLock;
40+
41+
// Flag that shows if the map was succesful
42+
bool mMapAcquired = false;
4043
};
4144

4245
} // namespace roc

src/MemoryMappedFile.cxx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,18 @@ MemoryMappedFile::MemoryMappedFile(const std::string& fileName, size_t fileSize,
4242
if (lockMap) {
4343
try{
4444
Utilities::resetSmartPtr(mInterprocessLock, "Alice_O2_RoC_MMF_" + fileName + "_lock");
45+
mMapAcquired = map(fileName, fileSize);
4546
}
4647
catch (const boost::exception& e) {
4748
BOOST_THROW_EXCEPTION(LockException()
4849
<< ErrorInfo::Message("Memory map file is locked by another process."));
4950
}
5051
}
51-
52-
map(fileName, fileSize);
5352
}
5453

5554
MemoryMappedFile::~MemoryMappedFile()
5655
{
57-
if (mInternal->deleteFileOnDestruction) {
56+
if (mInternal->deleteFileOnDestruction && mMapAcquired) {
5857
bfs::remove(mInternal->fileName);
5958
}
6059
}
@@ -74,7 +73,7 @@ std::string MemoryMappedFile::getFileName() const
7473
return mInternal->fileName;
7574
}
7675

77-
void MemoryMappedFile::map(const std::string& fileName, size_t fileSize)
76+
bool MemoryMappedFile::map(const std::string& fileName, size_t fileSize)
7877
{
7978
try {
8079
// Check the directory exists
@@ -132,6 +131,8 @@ void MemoryMappedFile::map(const std::string& fileName, size_t fileSize)
132131
e << ErrorInfo::FileName(fileName) << ErrorInfo::FileSize(fileSize);
133132
throw;
134133
}
134+
135+
return true;
135136
}
136137

137138
} // namespace roc

0 commit comments

Comments
 (0)