Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/webots/core/WbPosixMemoryMappedFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@

#include "WbPosixMemoryMappedFile.hpp"

#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <sys/mman.h>
#include <unistd.h>

Expand All @@ -30,14 +32,20 @@ WbPosixMemoryMappedFile::~WbPosixMemoryMappedFile() {
bool WbPosixMemoryMappedFile::create(int size) {
unlink(mName.toUtf8()); // delete a possibly existing memory mapped file with the same name
int fd = open(mName.toUtf8(), O_CREAT | O_RDWR, 0666); // returns -1 in case of failure
if (fd < 0)
if (fd < 0) {
mErrorString = QString("Cannot open file: %1 (%2)").arg(mName).arg(strerror(errno));
return false;
if (ftruncate(fd, size) == -1)
}
if (ftruncate(fd, size) == -1) {
mErrorString = QString("Cannot truncate file: %1 (%2)").arg(mName).arg(strerror(errno));
return false;
}
mData = mmap(0, size, PROT_WRITE | PROT_READ, MAP_SHARED, fd, 0);
close(fd);
if (mData == MAP_FAILED)
if (mData == MAP_FAILED) {
mErrorString = QString("Cannot map file: %1 (%2)").arg(mName).arg(strerror(errno));
return false;
}
mSize = size;
return true;
}
2 changes: 2 additions & 0 deletions src/webots/core/WbPosixMemoryMappedFile.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ class WbPosixMemoryMappedFile {
int size() const { return mSize; }
void *data() const { return mData; }
const QString &nativeKey() const { return mName; }
const QString &errorString() const { return mErrorString; }

private:
QString mName;
int mSize;
void *mData;
QString mErrorString;
};

#endif
4 changes: 3 additions & 1 deletion src/webots/nodes/WbAbstractCamera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,13 +198,15 @@ WbMemoryMappedFile *WbAbstractCamera::initializeMemoryMappedFile(const QString &
const QString folder = WbStandardPaths::webotsTmpPath() + "ipc/" + QUrl::toPercentEncoding(robot()->name());
const QString memoryMappedFileName = folder + "/snap.webots." + QUrl::toPercentEncoding(name()) + id;
QDir().mkdir(folder);
info(tr("Allocating memory mapped file '%1'").arg(memoryMappedFileName));
WbMemoryMappedFile *imageMemoryMappedFile = new WbMemoryMappedFile(memoryMappedFileName);
// A controller of the previous simulation may have not released cleanly the memory mapped file (e.g. when the controller
// crashes). This can be detected by trying to attach, and the memory mapped file may be cleaned by detaching.
if (imageMemoryMappedFile->attach())
imageMemoryMappedFile->detach();
if (!imageMemoryMappedFile->create(size())) {
const QString message = tr("Cannot allocate memory mapped file for camera image.");
const QString message =
tr("Cannot allocate memory mapped file for camera image.\nCaused by: %1.").arg(imageMemoryMappedFile->errorString());
warn(message);
delete imageMemoryMappedFile;
return NULL;
Expand Down