[Scummvm-cvs-logs] SF.net SVN: scummvm:[48319] scummvm/trunk/engines/teenagent/detection.cpp

megath at users.sourceforge.net megath at users.sourceforge.net
Sat Mar 20 15:53:47 CET 2010


Revision: 48319
          http://scummvm.svn.sourceforge.net/scummvm/?rev=48319&view=rev
Author:   megath
Date:     2010-03-20 14:53:46 +0000 (Sat, 20 Mar 2010)

Log Message:
-----------
fixed file leak, cleanups

Modified Paths:
--------------
    scummvm/trunk/engines/teenagent/detection.cpp

Modified: scummvm/trunk/engines/teenagent/detection.cpp
===================================================================
--- scummvm/trunk/engines/teenagent/detection.cpp	2010-03-20 14:24:19 UTC (rev 48318)
+++ scummvm/trunk/engines/teenagent/detection.cpp	2010-03-20 14:53:46 UTC (rev 48319)
@@ -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 @@
 
 #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 @@
 			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));
+				AutoPtr<Common::InSaveFile> in = g_system->getSavefileManager()->openForLoading(*file);
+				if (!in)
+					continue;
 
-					delete in;
-				}
+				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 @@
 
 	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,15 +209,14 @@
 		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;
 	}
 };


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.




More information about the Scummvm-git-logs mailing list