From 26bb6236a05bdfcabcd28be5488a4e1943ede805 Mon Sep 17 00:00:00 2001 From: James Turner Date: Mon, 28 Dec 2020 15:08:40 +0000 Subject: [PATCH] Effect builder: catch allocation failures Fail on bad_alloc of reading texture images, instead of crashing. --- simgear/scene/material/TextureBuilder.cxx | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/simgear/scene/material/TextureBuilder.cxx b/simgear/scene/material/TextureBuilder.cxx index 25fab866..a314bd2a 100644 --- a/simgear/scene/material/TextureBuilder.cxx +++ b/simgear/scene/material/TextureBuilder.cxx @@ -273,11 +273,19 @@ bool setAttrs(const TexTuple& attrs, Texture* tex, options->setLoadOriginHint(SGReaderWriterOptions::LoadOriginHint::ORIGIN_EFFECTS_NORMALIZED); else options->setLoadOriginHint(SGReaderWriterOptions::LoadOriginHint::ORIGIN_EFFECTS); -#if OSG_VERSION_LESS_THAN(3,4,2) - result = osgDB::readImageFile(imageName, options); -#else - result = osgDB::readRefImageFile(imageName, options); -#endif + + try { + #if OSG_VERSION_LESS_THAN(3,4,2) + result = osgDB::readImageFile(imageName, options); + #else + result = osgDB::readRefImageFile(imageName, options); + #endif + } catch (std::bad_alloc& ba) { + SG_LOG(SG_GL, SG_ALERT, "Bad allocation loading:" << imageName); + // todo: report low memory warning + return false; + } + options->setLoadOriginHint(origLOH); osg::ref_ptr image; if (result.success())