diff --git a/src/webots/core/WbPosixMemoryMappedFile.cpp b/src/webots/core/WbPosixMemoryMappedFile.cpp index 468ad74c466..b85614ae1d5 100644 --- a/src/webots/core/WbPosixMemoryMappedFile.cpp +++ b/src/webots/core/WbPosixMemoryMappedFile.cpp @@ -14,7 +14,9 @@ #include "WbPosixMemoryMappedFile.hpp" +#include #include +#include #include #include @@ -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; } diff --git a/src/webots/core/WbPosixMemoryMappedFile.hpp b/src/webots/core/WbPosixMemoryMappedFile.hpp index 72beea63b3d..a343c3e5d36 100644 --- a/src/webots/core/WbPosixMemoryMappedFile.hpp +++ b/src/webots/core/WbPosixMemoryMappedFile.hpp @@ -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 diff --git a/src/webots/nodes/WbAbstractCamera.cpp b/src/webots/nodes/WbAbstractCamera.cpp index 2f1df435df9..d48b0b35cc0 100644 --- a/src/webots/nodes/WbAbstractCamera.cpp +++ b/src/webots/nodes/WbAbstractCamera.cpp @@ -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;