Skip to content

Commit

Permalink
Fixed memory leakage
Browse files Browse the repository at this point in the history
  • Loading branch information
shawwwn committed May 17, 2014
1 parent e5a733c commit ac9086b
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 25 deletions.
8 changes: 4 additions & 4 deletions H2OExtractor.dsp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 9 additions & 5 deletions Source/Archive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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<m_FileList.size(); i++) //m_Header.FileCount
Expand All @@ -352,4 +353,7 @@ void Archive::init()
memset(&m_Header, 0, sizeof(m_Header));
memset(&m_Header, 0, sizeof(m_FileNameDesc));
memset(&m_Header, 0, sizeof(m_DirectoryNameDesc));
m_pCompressedDirectoryNameChunk = NULL;
m_pCompressedFileNameChunk = NULL;
m_arrDirectoryParents = NULL;
}
1 change: 1 addition & 0 deletions Source/Archive.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "./Utils/FileUtils.h"
#include "./Utils/StringUtils.h"
#include "./Utils/DebugUtils.h"
#include "./Utils/GeneralUtils.hpp"
#include "ArchiveComment.h"
#include "ArchiveHeader.h"
#include "ArchiveFile.h"
Expand Down
2 changes: 0 additions & 2 deletions Source/ArchiveComment.cpp

This file was deleted.

8 changes: 8 additions & 0 deletions Source/ArchiveComment.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,20 @@
#pragma once

#include "Types.h"
#include "Windows.h"
#include <exception>

#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;
Expand Down
1 change: 1 addition & 0 deletions Source/H2OExtractor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ int main(int argc, char* argv[])
DB::debugLog("\nPress Any Key to Exist", "\n", NULL);
getchar();

h2o.close();
return 0;
}
27 changes: 16 additions & 11 deletions Source/Utils/FileUtils.cpp
Original file line number Diff line number Diff line change
@@ -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;
}

}}
6 changes: 4 additions & 2 deletions Source/Utils/FileUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}}
30 changes: 30 additions & 0 deletions Source/Utils/GeneralUtils.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/* GeneralUtils.hpp */
#pragma once
#include <vector>

#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<typename T> void clearVectorOfArray(std::vector<T>& vec)
{
for ( int i = 0; i < vec.size(); i++ )
{
SAFE_DELETE_ARRAY(vec[i]);
}
vec.clear();
}

// Delete all the allocated (single) contents
template<typename T> void clearVectorOfContent(std::vector<T>& vec)
{
for ( int i = 0; i < vec.size(); i++ )
{
SAFE_DELETE(vec[i]);
}
vec.clear();
}

}}
2 changes: 1 addition & 1 deletion Source/predefine.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
#define H2O_PRINT_WHEN_EXTRACT
//#define H2O_PRINT_FILE_LIST
//#define H2O_PRINT_FILE_DESC
//#define H2O_PRINT_DIR_INHEIRTANCY
//#define H2O_PRINT_DIR_INHEIRTANCY

0 comments on commit ac9086b

Please sign in to comment.