From bbda29628951acb8b3f65f5e3db03014b49d6602 Mon Sep 17 00:00:00 2001 From: Ykkrosh Date: Fri, 28 Oct 2005 01:43:16 +0000 Subject: [PATCH] TerrainProperties: Use CTerrainPropertiesPtr (boost::shared_ptr) instead of CTerrainProperties*, to fix (slightly inelegantly) memory leaks. Renderer: Initialise pointers when HW lighting is unavailable. Atlas: Avoid strange VS2005 iterator complaint. This was SVN commit r3036. --- source/graphics/TerrainProperties.cpp | 12 +++++------ source/graphics/TerrainProperties.h | 12 +++++++---- source/graphics/TextureEntry.cpp | 2 +- source/graphics/TextureEntry.h | 6 +++--- source/graphics/TextureManager.cpp | 21 +++++++++++-------- source/graphics/TextureManager.h | 11 ++++++---- source/renderer/Renderer.cpp | 6 ++++++ .../tools/atlas/GameInterface/CommandProc.cpp | 5 ++++- 8 files changed, 47 insertions(+), 28 deletions(-) diff --git a/source/graphics/TerrainProperties.cpp b/source/graphics/TerrainProperties.cpp index 5a58e56fcf..c65963033f 100644 --- a/source/graphics/TerrainProperties.cpp +++ b/source/graphics/TerrainProperties.cpp @@ -15,7 +15,7 @@ using namespace std; -CTerrainProperties::CTerrainProperties(CTerrainProperties *parent): +CTerrainProperties::CTerrainProperties(CTerrainPropertiesPtr parent): m_pParent(parent), m_BaseColor(0), m_HasBaseColor(false) @@ -24,11 +24,11 @@ CTerrainProperties::CTerrainProperties(CTerrainProperties *parent): m_Groups = m_pParent->m_Groups; } -CTerrainProperties *CTerrainProperties::FromXML(CTerrainProperties *parent, const char* path) +CTerrainPropertiesPtr CTerrainProperties::FromXML(CTerrainPropertiesPtr parent, const char* path) { CXeromyces XeroFile; if (XeroFile.Load(path) != PSRETURN_OK) - return NULL; + return CTerrainPropertiesPtr(); XMBElement root = XeroFile.getRoot(); CStr rootName = XeroFile.getElementString(root.getNodeName()); @@ -41,7 +41,7 @@ CTerrainProperties *CTerrainProperties::FromXML(CTerrainProperties *parent, cons "TextureManager: Loading %s: Root node is not terrains (found \"%s\")", path, rootName.c_str()); - return NULL; + return CTerrainPropertiesPtr(); } #define ELMT(x) int el_##x = XeroFile.getElementID(#x) @@ -62,7 +62,7 @@ CTerrainProperties *CTerrainProperties::FromXML(CTerrainProperties *parent, cons if (child.getNodeName() == el_terrain) { - CTerrainProperties *ret=new CTerrainProperties(parent); + CTerrainPropertiesPtr ret (new CTerrainProperties(parent)); ret->LoadXML(child, &XeroFile); return ret; } @@ -76,7 +76,7 @@ CTerrainProperties *CTerrainProperties::FromXML(CTerrainProperties *parent, cons } } - return NULL; + return CTerrainPropertiesPtr(); } void CTerrainProperties::LoadXML(XMBElement node, CXeromyces *pFile) diff --git a/source/graphics/TerrainProperties.h b/source/graphics/TerrainProperties.h index 3e668d0407..261f0bba3b 100644 --- a/source/graphics/TerrainProperties.h +++ b/source/graphics/TerrainProperties.h @@ -11,10 +11,14 @@ #define graphics_TerrainProperties_H #include "CStr.h" +#include "boost/shared_ptr.hpp" class CTerrainGroup; class XMBElement; class CXeromyces; +class CTerrainProperties; + +typedef boost::shared_ptr CTerrainPropertiesPtr; class CTerrainProperties { @@ -22,7 +26,7 @@ public: typedef std::vector GroupVector; private: - CTerrainProperties *m_pParent; + CTerrainPropertiesPtr m_pParent; // BGRA color of topmost mipmap level, for coloring minimap, or a color // manually specified in the Terrain XML (or by any parent) @@ -38,17 +42,17 @@ private: void LoadXML(XMBElement node, CXeromyces *pFile); public: - CTerrainProperties(CTerrainProperties *parent); + CTerrainProperties(CTerrainPropertiesPtr parent); // Create a new object and load the XML file specified. Returns NULL upon // failure // The parent pointer may be NULL, for the "root" terrainproperties object. - static CTerrainProperties *FromXML(CTerrainProperties *parent, const char* path); + static CTerrainPropertiesPtr FromXML(CTerrainPropertiesPtr parent, const char* path); // Save the object to an XML file. Implement when needed! ;-) // bool WriteXML(CStr path); - inline CTerrainProperties *GetParent() const + inline CTerrainPropertiesPtr GetParent() const { return m_pParent; } // Return true if this property object or any of its parents has a basecolor diff --git a/source/graphics/TextureEntry.cpp b/source/graphics/TextureEntry.cpp index 45a80a8326..15d04edca3 100755 --- a/source/graphics/TextureEntry.cpp +++ b/source/graphics/TextureEntry.cpp @@ -21,7 +21,7 @@ map CTextureEntry::m_LoadedTextures; ///////////////////////////////////////////////////////////////////////////////////// // CTextureEntry constructor -CTextureEntry::CTextureEntry(CTerrainProperties *props, CStr path): +CTextureEntry::CTextureEntry(CTerrainPropertiesPtr props, CStr path): m_pProperties(props), m_Bitmap(NULL), m_Handle(-1), diff --git a/source/graphics/TextureEntry.h b/source/graphics/TextureEntry.h index 9ae7a740c5..b47762e01c 100755 --- a/source/graphics/TextureEntry.h +++ b/source/graphics/TextureEntry.h @@ -24,7 +24,7 @@ private: CStr m_Tag; // The property sheet used by this texture - CTerrainProperties *m_pProperties; + CTerrainPropertiesPtr m_pProperties; // Path to the texture file CStr m_TexturePath; @@ -52,13 +52,13 @@ private: public: // Most of the texture's data is delay-loaded, so after the constructor has // been called, the texture entry is ready to be used. - CTextureEntry(CTerrainProperties *props, CStr path); + CTextureEntry(CTerrainPropertiesPtr props, CStr path); ~CTextureEntry(); CStr GetTag() const { return m_Tag; } - CTerrainProperties *GetProperties() const + CTerrainPropertiesPtr GetProperties() const { return m_pProperties; } CStr GetTexturePath() const diff --git a/source/graphics/TextureManager.cpp b/source/graphics/TextureManager.cpp index 5c0f543a61..5ff4465337 100755 --- a/source/graphics/TextureManager.cpp +++ b/source/graphics/TextureManager.cpp @@ -63,12 +63,12 @@ CTextureEntry* CTextureManager::FindTexture(Handle handle) return CTextureEntry::GetByHandle(handle); } -CTerrainProperties *CTextureManager::GetPropertiesFromFile(CTerrainProperties *props, const char* path) +CTerrainPropertiesPtr CTextureManager::GetPropertiesFromFile(CTerrainPropertiesPtr props, const char* path) { return CTerrainProperties::FromXML(props, path); } -CTextureEntry *CTextureManager::AddTexture(CTerrainProperties *props, CStr path) +CTextureEntry *CTextureManager::AddTexture(CTerrainPropertiesPtr props, CStr path) { CTextureEntry *entry = new CTextureEntry(props, path); m_TextureEntries.push_back(entry); @@ -91,7 +91,7 @@ void CTextureManager::DeleteTexture(CTextureEntry* entry) // jw: indeed this is inefficient and RecurseDirectory should be implemented // via VFSUtil::EnumFiles, but it works fine and "only" takes 25ms for // typical maps. therefore, we'll leave it for now. -void CTextureManager::LoadTextures(CTerrainProperties *props, const char* dir) +void CTextureManager::LoadTextures(CTerrainPropertiesPtr props, const char* dir) { VFSUtil::FileList files; if(!VFSUtil::FindFiles(dir, 0, files)) @@ -111,17 +111,20 @@ void CTextureManager::LoadTextures(CTerrainProperties *props, const char* dir) const char* ext = strrchr(texture_name, '.'); if(!ext || !stricmp(ext, ".xml") || !stricmp(ext, ".xmb") || !stricmp(ext, ".dtd")) continue; + // Also allow storage of temporary files in the texture directories + if(ext[strlen(ext)-1] == '~') + continue; // build name of associated xml file (i.e. replace extension) char xml_name[PATH_MAX+5]; // add room for .XML strcpy_s(xml_name, PATH_MAX, texture_name); strcpy(xml_name + (ext-texture_name), ".xml"); // safe - CTerrainProperties *myprops = NULL; + CTerrainPropertiesPtr myprops; // Has XML file -> attempt to load properties if (vfs_exists(xml_name)) { - myprops=GetPropertiesFromFile(props, xml_name); + myprops = GetPropertiesFromFile(props, xml_name); if (myprops) LOG(NORMAL, LOG_CATEGORY, "CTextureManager: Successfully loaded override xml %s for texture %s\n", xml_name, texture_name); } @@ -134,17 +137,17 @@ void CTextureManager::LoadTextures(CTerrainProperties *props, const char* dir) } } -void CTextureManager::RecurseDirectory(CTerrainProperties *parentProps, const char* cur_dir_path) +void CTextureManager::RecurseDirectory(CTerrainPropertiesPtr parentProps, const char* cur_dir_path) { //LOG(NORMAL, LOG_CATEGORY, "CTextureManager::RecurseDirectory(%s)", path.c_str()); - CTerrainProperties *props=NULL; + CTerrainPropertiesPtr props; // Load terrains.xml first, if it exists char fn[PATH_MAX]; snprintf(fn, PATH_MAX, "%s/%s", cur_dir_path, "terrains.xml"); if (vfs_exists(fn)) - props=GetPropertiesFromFile(parentProps, fn); + props = GetPropertiesFromFile(parentProps, fn); // No terrains.xml, or read failures -> use parent props (i.e. if (!props) @@ -168,7 +171,7 @@ void CTextureManager::RecurseDirectory(CTerrainProperties *parentProps, const ch int CTextureManager::LoadTerrainTextures() { - RecurseDirectory(NULL, "art/textures/terrain/types"); + RecurseDirectory(CTerrainPropertiesPtr(), "art/textures/terrain/types"); return 0; } diff --git a/source/graphics/TextureManager.h b/source/graphics/TextureManager.h index 55c1e542a9..99e2a88c09 100755 --- a/source/graphics/TextureManager.h +++ b/source/graphics/TextureManager.h @@ -3,6 +3,7 @@ #include #include +#include "boost/shared_ptr.hpp" #include "lib/res/handle.h" @@ -17,6 +18,8 @@ class CXeromyces; class CTextureEntry; class CTerrainProperties; +typedef boost::shared_ptr CTerrainPropertiesPtr; + class CTerrainGroup { // name of this terrain group (as specified by the terrain XML) @@ -65,12 +68,12 @@ private: // Find+load all textures in directory; check if // there's an override XML with the same basename (if there is, load it) - void LoadTextures(CTerrainProperties *props, const char* dir); + void LoadTextures(CTerrainPropertiesPtr props, const char* dir); // Load all terrains below path, using props as the parent property sheet. - void RecurseDirectory(CTerrainProperties *props, const char* dir); + void RecurseDirectory(CTerrainPropertiesPtr props, const char* dir); - CTerrainProperties *GetPropertiesFromFile(CTerrainProperties *props, const char* path); + CTerrainPropertiesPtr GetPropertiesFromFile(CTerrainPropertiesPtr props, const char* path); public: // constructor, destructor @@ -87,7 +90,7 @@ public: // Create a texture object for a new terrain texture at path, using the // property sheet props. - CTextureEntry *AddTexture(CTerrainProperties *props, CStr path); + CTextureEntry *AddTexture(CTerrainPropertiesPtr props, CStr path); // Remove the texture from all our maps and lists and delete it afterwards. void DeleteTexture(CTextureEntry* entry); diff --git a/source/renderer/Renderer.cpp b/source/renderer/Renderer.cpp index 8df58227e6..b37004a4ab 100755 --- a/source/renderer/Renderer.cpp +++ b/source/renderer/Renderer.cpp @@ -101,6 +101,12 @@ CRenderer::CRenderer() m_Models.PlayerHWLit = new HWLightingModelRenderer; m_Models.TransparentHWLit = new HWLightingModelRenderer; } + else + { + m_Models.NormalHWLit = NULL; + m_Models.PlayerHWLit = NULL; + m_Models.TransparentHWLit = NULL; + } m_Models.Transparency = new TransparencyRenderer; m_Models.ModWireframe = RenderModifierPtr(new WireframeRenderModifier); diff --git a/source/tools/atlas/GameInterface/CommandProc.cpp b/source/tools/atlas/GameInterface/CommandProc.cpp index b92561557c..680ed6bdf9 100644 --- a/source/tools/atlas/GameInterface/CommandProc.cpp +++ b/source/tools/atlas/GameInterface/CommandProc.cpp @@ -15,6 +15,8 @@ template void delete_erase(T list, I first, I last) } } +template void delete_fn(T* v) { delete v; } + ////////////////////////////////////////////////////////////////////////// using namespace AtlasMessage; @@ -43,7 +45,8 @@ CommandProc::CommandProc() CommandProc::~CommandProc() { - delete_erase(m_Commands, m_Commands.begin(), m_Commands.end()); + for_each(m_Commands.begin(), m_Commands.end(), delete_fn); + m_Commands.clear(); } void CommandProc::Submit(Command* cmd)