diff options
author | Vladimir Menshakov | 2010-03-20 14:53:46 +0000 |
---|---|---|
committer | Vladimir Menshakov | 2010-03-20 14:53:46 +0000 |
commit | f122762309b84aaebb9b5f20559d1fe8e34e8577 (patch) | |
tree | 192549758a9c91e8c9054630f101976699716aff /engines | |
parent | e037ea19acfa510ade04730ac6e2fae0585b61cd (diff) | |
download | scummvm-rg350-f122762309b84aaebb9b5f20559d1fe8e34e8577.tar.gz scummvm-rg350-f122762309b84aaebb9b5f20559d1fe8e34e8577.tar.bz2 scummvm-rg350-f122762309b84aaebb9b5f20559d1fe8e34e8577.zip |
fixed file leak, cleanups
svn-id: r48319
Diffstat (limited to 'engines')
-rw-r--r-- | engines/teenagent/detection.cpp | 77 |
1 files changed, 57 insertions, 20 deletions
diff --git a/engines/teenagent/detection.cpp b/engines/teenagent/detection.cpp index 9cc75e91b2..0dc86d57db 100644 --- a/engines/teenagent/detection.cpp +++ b/engines/teenagent/detection.cpp @@ -25,6 +25,7 @@ #include "common/system.h" #include "common/savefile.h" #include "common/algorithm.h" +#include "common/noncopyable.h" #include "base/plugins.h" @@ -76,6 +77,45 @@ static const ADParams detectionParams = { #define MAX_SAVES 20 +//add it to ptr.h? +template<typename T> +class AutoPtr { +protected: + mutable T *object; + +public: + typedef T ValueType; + typedef T *PointerType; + + inline AutoPtr(T *o = NULL): object(o) {} + inline AutoPtr(const AutoPtr& other): object(other.release()) {} + inline AutoPtr& operator=(const AutoPtr& other) { + delete object; + object = other.release(); + return *this; + } + + inline T *operator->() const { return object; } + inline operator T*() const { return object; } + inline operator bool() const { return object != NULL; } + + inline ~AutoPtr() { + delete object; + } + + inline void reset(T * o) { + delete object; + object = o; + } + + inline T *release() const { + T *r = object; + object = NULL; + return r; + } +}; + + class TeenAgentMetaEngine : public AdvancedMetaEngine { public: TeenAgentMetaEngine() : AdvancedMetaEngine(detectionParams) { @@ -129,17 +169,15 @@ public: int slot; const char *ext = strrchr(file->c_str(), '.'); if (ext && (slot = atoi(ext + 1)) >= 0 && slot < MAX_SAVES) { - Common::InSaveFile *in = g_system->getSavefileManager()->openForLoading(*file); - if (in) { - char buf[25]; - in->seek(0); - in->read(buf, 24); - buf[24] = 0; - Common::String description = buf; - saveList.push_back(SaveStateDescriptor(slot, description)); - - delete in; - } + AutoPtr<Common::InSaveFile> in = g_system->getSavefileManager()->openForLoading(*file); + if (!in) + continue; + + char buf[25]; + in->seek(0); + in->read(buf, 24); + buf[24] = 0; + saveList.push_back(SaveStateDescriptor(slot, buf)); } } return saveList; @@ -156,8 +194,8 @@ public: virtual SaveStateDescriptor querySaveMetaInfos(const char *target, int slot) const { Common::String filename = generateGameStateFileName(target, slot); - Common::InSaveFile *in = g_system->getSavefileManager()->openForLoading(filename); - if (in == NULL) + AutoPtr<Common::InSaveFile> in = g_system->getSavefileManager()->openForLoading(filename); + if (!in) return SaveStateDescriptor(); char buf[25]; @@ -171,14 +209,13 @@ public: if (!Graphics::checkThumbnailHeader(*in)) return SaveStateDescriptor(slot, desc); - Graphics::Surface *thumb = new Graphics::Surface; - if (!Graphics::loadThumbnail(*in, *thumb)) { - delete thumb; - return SaveStateDescriptor(slot, desc); - } - SaveStateDescriptor ssd(slot, desc); - ssd.setThumbnail(thumb); + ssd.setDeletableFlag(true); + + //checking for the thumbnail + AutoPtr<Graphics::Surface> thumb = new Graphics::Surface; + if (Graphics::loadThumbnail(*in, *thumb)) + ssd.setThumbnail(thumb.release()); return ssd; } |