From a18590235dd7248b88275b7770e19b7c5a2f3aeb Mon Sep 17 00:00:00 2001 From: CoolSpy3 Date: Fri, 3 Oct 2025 20:13:52 -0700 Subject: [PATCH 1/3] print cause when WbAbstractCamera fails to create mmaped file --- src/webots/core/WbPosixMemoryMappedFile.cpp | 14 +++++++++++--- src/webots/core/WbPosixMemoryMappedFile.hpp | 2 ++ src/webots/nodes/WbAbstractCamera.cpp | 2 +- 3 files changed, 14 insertions(+), 4 deletions(-) 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..ee0a50ee00b 100644 --- a/src/webots/nodes/WbAbstractCamera.cpp +++ b/src/webots/nodes/WbAbstractCamera.cpp @@ -204,7 +204,7 @@ WbMemoryMappedFile *WbAbstractCamera::initializeMemoryMappedFile(const QString & 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; From 74a22f7a0c492bfdbee73bf2dcef8fcf03fcd8eb Mon Sep 17 00:00:00 2001 From: CoolSpy3 <55305038+CoolSpy3@users.noreply.github.com> Date: Fri, 3 Oct 2025 21:20:14 -0700 Subject: [PATCH 2/3] fix formatting --- src/webots/nodes/WbAbstractCamera.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/webots/nodes/WbAbstractCamera.cpp b/src/webots/nodes/WbAbstractCamera.cpp index ee0a50ee00b..1b644169213 100644 --- a/src/webots/nodes/WbAbstractCamera.cpp +++ b/src/webots/nodes/WbAbstractCamera.cpp @@ -204,7 +204,8 @@ WbMemoryMappedFile *WbAbstractCamera::initializeMemoryMappedFile(const QString & if (imageMemoryMappedFile->attach()) imageMemoryMappedFile->detach(); if (!imageMemoryMappedFile->create(size())) { - const QString message = tr("Cannot allocate memory mapped file for camera image.\nCaused by: %1.").arg(imageMemoryMappedFile->errorString()); + const QString message = + tr("Cannot allocate memory mapped file for camera image.\nCaused by: %1.").arg(imageMemoryMappedFile->errorString()); warn(message); delete imageMemoryMappedFile; return NULL; From 7e5d689ebae5d9d26b87acb30218aae5e47420c0 Mon Sep 17 00:00:00 2001 From: CoolSpy3 Date: Sat, 4 Oct 2025 13:41:34 -0700 Subject: [PATCH 3/3] log shared memory creations --- src/webots/nodes/WbAbstractCamera.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/webots/nodes/WbAbstractCamera.cpp b/src/webots/nodes/WbAbstractCamera.cpp index 1b644169213..d48b0b35cc0 100644 --- a/src/webots/nodes/WbAbstractCamera.cpp +++ b/src/webots/nodes/WbAbstractCamera.cpp @@ -198,6 +198,7 @@ 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.