From ac9086b600c687df4852358c834022963f75f256 Mon Sep 17 00:00:00 2001 From: Shawwwn Date: Fri, 16 May 2014 19:57:26 -0700 Subject: [PATCH] Fixed memory leakage --- H2OExtractor.dsp | 8 ++++---- Source/Archive.cpp | 14 +++++++++----- Source/Archive.h | 1 + Source/ArchiveComment.cpp | 2 -- Source/ArchiveComment.h | 8 ++++++++ Source/H2OExtractor.cpp | 1 + Source/Utils/FileUtils.cpp | 27 ++++++++++++++++----------- Source/Utils/FileUtils.h | 6 ++++-- Source/Utils/GeneralUtils.hpp | 30 ++++++++++++++++++++++++++++++ Source/predefine.h | 2 +- 10 files changed, 74 insertions(+), 25 deletions(-) delete mode 100644 Source/ArchiveComment.cpp create mode 100644 Source/Utils/GeneralUtils.hpp diff --git a/H2OExtractor.dsp b/H2OExtractor.dsp index 018f758..935e38d 100644 --- a/H2OExtractor.dsp +++ b/H2OExtractor.dsp @@ -146,10 +146,6 @@ SOURCE=.\Source\ArchiveHeader.h SOURCE=.\Source\Archive.cpp # End Source File -# Begin Source File - -SOURCE=.\Source\ArchiveComment.cpp -# End Source File # End Group # End Group # Begin Group "Main" @@ -241,6 +237,10 @@ SOURCE=.\Source\Utils\FileUtils.h # End Source File # Begin Source File +SOURCE=.\Source\Utils\GeneralUtils.hpp +# End Source File +# Begin Source File + SOURCE=.\Source\Utils\StringUtils.cpp # End Source File # Begin Source File diff --git a/Source/Archive.cpp b/Source/Archive.cpp index 20df2c8..23441c7 100644 --- a/Source/Archive.cpp +++ b/Source/Archive.cpp @@ -15,10 +15,11 @@ Archive::Archive(char* szFilePath) Archive::~Archive() { close(); - delete[] m_arrDirectoryParents; - delete[] m_pCompressedFileNameChunk; - delete[] m_pCompressedDirectoryNameChunk; - delete[] m_Comment.szComments; + SAFE_DELETE_ARRAY(m_arrDirectoryParents); + SAFE_DELETE_ARRAY(m_pCompressedFileNameChunk); + SAFE_DELETE_ARRAY(m_pCompressedDirectoryNameChunk); + Util::Gen::clearVectorOfArray(m_DirectoryNameList); + Util::Gen::clearVectorOfArray(m_FileNameList); } uint64_t Archive::readComment(uint64_t streamPos) @@ -328,7 +329,7 @@ void Archive::extractByIndex(uint32_t index) void Archive::extractAll() { const char* dirPath = "output/"; - if (checkPathType(dirPath)!=TYPE_DIR) + if (Util::File::checkPathType(dirPath)!=Util::File::TYPE_DIR) mkdir(dirPath); for (int i=0; i #define H2O_COMMENT_TERMINATOR 0x1A +struct OuterException : std::exception{}; + #pragma pack(push, 1) struct ArchiveComment { + ~ArchiveComment() + { + delete[] szComments; + }; char szHeader[8]; float VersionInfo; char* szComments; diff --git a/Source/H2OExtractor.cpp b/Source/H2OExtractor.cpp index 334a632..c3f3046 100644 --- a/Source/H2OExtractor.cpp +++ b/Source/H2OExtractor.cpp @@ -14,5 +14,6 @@ int main(int argc, char* argv[]) DB::debugLog("\nPress Any Key to Exist", "\n", NULL); getchar(); + h2o.close(); return 0; } \ No newline at end of file diff --git a/Source/Utils/FileUtils.cpp b/Source/Utils/FileUtils.cpp index e74d592..4c9534a 100644 --- a/Source/Utils/FileUtils.cpp +++ b/Source/Utils/FileUtils.cpp @@ -1,16 +1,21 @@ /* FileUtils.cpp */ #include "FileUtils.h" -PathType checkPathType(const char* dirPath) -{ - if (access(dirPath, 0)==0) +namespace Util { namespace File { + + // Is the provided path a folder or a file + PathType checkPathType(const char* dirPath) { - struct stat status; - stat(dirPath, &status); - if ( status.st_mode & S_IFDIR ) - return TYPE_DIR; - else - return TYPE_FILE; + if (access(dirPath, 0)==0) + { + struct stat status; + stat(dirPath, &status); + if ( status.st_mode & S_IFDIR ) + return TYPE_DIR; + else + return TYPE_FILE; + } + return TYPE_NOT_EXIST; } - return TYPE_NOT_EXIST; -} + +}} \ No newline at end of file diff --git a/Source/Utils/FileUtils.h b/Source/Utils/FileUtils.h index 0099fb2..5bb52e7 100644 --- a/Source/Utils/FileUtils.h +++ b/Source/Utils/FileUtils.h @@ -9,5 +9,7 @@ #include "./dirent/dirent.h" #include "../Types.h" -enum PathType { TYPE_DIR, TYPE_FILE, TYPE_NOT_EXIST }; -PathType checkPathType(const char* dirPath); +namespace Util { namespace File { + enum PathType { TYPE_DIR, TYPE_FILE, TYPE_NOT_EXIST }; + PathType checkPathType(const char* dirPath); +}} diff --git a/Source/Utils/GeneralUtils.hpp b/Source/Utils/GeneralUtils.hpp new file mode 100644 index 0000000..cb380b7 --- /dev/null +++ b/Source/Utils/GeneralUtils.hpp @@ -0,0 +1,30 @@ +/* GeneralUtils.hpp */ +#pragma once +#include + +#define SAFE_DELETE(a) if( (a) != NULL ) delete (a); (a) = NULL; +#define SAFE_DELETE_ARRAY(a) if( (a) != NULL ) delete[] (a); (a) = NULL; + +namespace Util { namespace Gen { + + // Delete all the allocated arrays + template void clearVectorOfArray(std::vector& vec) + { + for ( int i = 0; i < vec.size(); i++ ) + { + SAFE_DELETE_ARRAY(vec[i]); + } + vec.clear(); + } + + // Delete all the allocated (single) contents + template void clearVectorOfContent(std::vector& vec) + { + for ( int i = 0; i < vec.size(); i++ ) + { + SAFE_DELETE(vec[i]); + } + vec.clear(); + } + +}} \ No newline at end of file diff --git a/Source/predefine.h b/Source/predefine.h index fccf61b..4e7a59a 100644 --- a/Source/predefine.h +++ b/Source/predefine.h @@ -5,4 +5,4 @@ #define H2O_PRINT_WHEN_EXTRACT //#define H2O_PRINT_FILE_LIST //#define H2O_PRINT_FILE_DESC -//#define H2O_PRINT_DIR_INHEIRTANCY \ No newline at end of file +//#define H2O_PRINT_DIR_INHEIRTANCY