Report out-of-memory in some loading places

BTG can throw bad-alloc in the wild; catch this case and report it.
This commit is contained in:
James Turner 2021-03-03 11:04:47 +00:00
parent b7234be625
commit 087547c6a0
2 changed files with 20 additions and 5 deletions

View File

@ -559,9 +559,16 @@ ModelRegistry::readImage(const string& fileName,
} }
} }
// REVIEW: Memory Leak - 262,144 bytes in 1 blocks are indirectly lost try {
// The leak occurs with OSG, but may be related to opt being a raw pointer // REVIEW: Memory Leak - 262,144 bytes in 1 blocks are indirectly lost
res = registry->readImageImplementation(absFileName, opt); // The leak occurs with OSG, but may be related to opt being a raw pointer
res = registry->readImageImplementation(absFileName, opt);
} catch (std::bad_alloc&) {
simgear::reportFailure(simgear::LoadFailure::OutOfMemory, simgear::ErrorCode::ThreeDModelLoad,
"Out of memory loading texture", sg_location{absFileName});
return ReaderWriter::ReadResult::INSUFFICIENT_MEMORY_TO_LOAD;
}
if (!res.success()) { if (!res.success()) {
SG_LOG(SG_IO, SG_DEV_WARN, "Image loading failed:" << res.message()); SG_LOG(SG_IO, SG_DEV_WARN, "Image loading failed:" << res.message());
@ -733,7 +740,6 @@ ModelRegistry::readNode(const string& fileName,
ec.addFromMap(sgopt->getErrorContext()); ec.addFromMap(sgopt->getErrorContext());
} }
ReaderWriter::ReadResult res;
CallbackMap::iterator iter CallbackMap::iterator iter
= nodeCallbackMap.find(getFileExtension(fileName)); = nodeCallbackMap.find(getFileExtension(fileName));
ReaderWriter::ReadResult result; ReaderWriter::ReadResult result;
@ -742,6 +748,11 @@ ModelRegistry::readNode(const string& fileName,
else else
result = _defaultCallback->readNode(fileName, opt); result = _defaultCallback->readNode(fileName, opt);
if (!result.validNode()) {
simgear::reportFailure(simgear::LoadFailure::BadData, simgear::ErrorCode::ThreeDModelLoad,
"Failed to load 3D model:" + result.message(), sg_location{fileName});
}
return result; return result;
} }

View File

@ -73,8 +73,12 @@ SGReaderWriterBTG::readNode(const std::string& fileName,
"Failed to load BTG file:" + e.getFormattedMessage(), "Failed to load BTG file:" + e.getFormattedMessage(),
e.getLocation()); e.getLocation());
return ReadResult::ERROR_IN_READING_FILE; return ReadResult::ERROR_IN_READING_FILE;
} catch (std::bad_alloc&) {
simgear::reportFailure(simgear::LoadFailure::OutOfMemory, simgear::ErrorCode::BTGLoad,
"Out of memory loading BTG:" + fileName, sg_location{fileName});
return ReadResult::ERROR_IN_READING_FILE;
} }
return result; return result;
} }