Index: source/gui/IGUIObject.h =================================================================== --- source/gui/IGUIObject.h +++ source/gui/IGUIObject.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2017 Wildfire Games. +/* Copyright (C) 2018 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -506,8 +506,8 @@ // Internal storage for registered script handlers. std::map > m_ScriptHandlers; - // Cached JSObject representing this GUI object - JS::PersistentRootedObject m_JSObject; + // Cached JSValue representing this GUI object + JS::Heap m_JSObjectValue; }; Index: source/gui/IGUIObject.cpp =================================================================== --- source/gui/IGUIObject.cpp +++ source/gui/IGUIObject.cpp @@ -1,4 +1,4 @@ -/* Copyright (C) 2016 Wildfire Games. +/* Copyright (C) 2018 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -502,14 +502,15 @@ JSContext* cx = m_pGUI->GetScriptInterface()->GetContext(); JSAutoRequest rq(cx); // Cache the object when somebody first asks for it, because otherwise - // we end up doing far too much object allocation. TODO: Would be nice to - // not have these objects hang around forever using up memory, though. - if (!m_JSObject.initialized()) + // we end up doing far too much object allocation. + + if (m_JSObjectValue.isUndefined()) { - m_JSObject.init(cx, m_pGUI->GetScriptInterface()->CreateCustomObject("GUIObject")); - JS_SetPrivate(m_JSObject.get(), this); + m_JSObjectValue = JS::Heap(JS::ObjectValue(*m_pGUI->GetScriptInterface()->CreateCustomObject("GUIObject"))); + JS_SetPrivate(&m_JSObjectValue.toObject(), this); } - return m_JSObject.get(); + + return &(m_JSObjectValue.toObject()); } CStr IGUIObject::GetPresentableName() const @@ -542,6 +543,8 @@ void IGUIObject::TraceMember(JSTracer* trc) { + JS_CallValueTracer(trc, &m_JSObjectValue, "IGUIObject::m_JSObjectValue"); + for (std::pair>& handler : m_ScriptHandlers) JS_CallObjectTracer(trc, &handler.second, "IGUIObject::m_ScriptHandlers"); }