Skip to content

Commit c960054

Browse files
Guard against bad memory access in RenderPassSystem (#1153)
Fixes a bad memory access that in RenderPassSystem that happens in some linux builds. On Ubuntu with gcc, global_##classname##Factory is initialized after RenderPassSystem::renderPassMap, so the Register call in the constructor for ##classname##Factory works as expected. However, on some linux builds, global_##classname##Factory may be initialized before RenderPassSystem::renderPassMap, leading to a bad memory access. This is fixed by using the Construct On First Use idiom, where the static renderPassMap member variable is replaced by a GetRenderPassMap() member function in which the actual static render pass map is instantiated. This member function is then called in RenderPassSystem::Register() and in RenderPassSystem::CreateImpl() to write to/ read from the static render map. Signed-off-by: Shameek Ganguly <[email protected]>
1 parent 3e695f9 commit c960054

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

include/gz/rendering/RenderPassSystem.hh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,9 @@ namespace gz
8181

8282
/// \brief A map of render pass type id name to its factory class
8383
GZ_UTILS_WARN_IGNORE__DLL_INTERFACE_MISSING
84-
private: static std::map<std::string, RenderPassFactory *> renderPassMap;
84+
private: typedef std::map<std::string, RenderPassFactory *>
85+
RenderPassMap;
86+
private: static RenderPassMap &GetRenderPassMap();
8587

8688
/// \internal
8789
/// \brief Pointer to private data class

src/RenderPassSystem.cc

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ class gz::rendering::RenderPassSystemPrivate
2727
{
2828
};
2929

30-
std::map<std::string, RenderPassFactory *> RenderPassSystem::renderPassMap;
31-
3230
//////////////////////////////////////////////////
3331
// RenderPassSystem
3432
//////////////////////////////////////////////////
@@ -44,8 +42,8 @@ RenderPassSystem::~RenderPassSystem() = default;
4442
RenderPassPtr RenderPassSystem::CreateImpl(const std::string &_type)
4543
{
4644
RenderPassPtr pass;
47-
auto it = renderPassMap.find(_type);
48-
if (it != renderPassMap.end())
45+
auto it = GetRenderPassMap().find(_type);
46+
if (it != GetRenderPassMap().end())
4947
{
5048
pass.reset(it->second->New());
5149
}
@@ -61,5 +59,12 @@ RenderPassPtr RenderPassSystem::CreateImpl(const std::string &_type)
6159
void RenderPassSystem::Register(const std::string &_name,
6260
RenderPassFactory *_factory)
6361
{
64-
renderPassMap[_name] = _factory;
62+
GetRenderPassMap()[_name] = _factory;
63+
}
64+
65+
//////////////////////////////////////////////////
66+
RenderPassSystem::RenderPassMap &RenderPassSystem::GetRenderPassMap()
67+
{
68+
static RenderPassMap renderPassMap;
69+
return renderPassMap;
6570
}

0 commit comments

Comments
 (0)