src/system/helpers.cpp: fix resource leak ,use new instead of malloc

Origin commit data
------------------
Commit: a03445fe40
Author: Jacek Jendrzej <overx300@gmail.com>
Date: 2012-10-26 (Fri, 26 Oct 2012)
This commit is contained in:
Jacek Jendrzej
2012-10-26 23:16:30 +02:00
parent d7b86a6957
commit 44a2bd7678

View File

@@ -246,14 +246,14 @@ std::string trim(std::string &str, const std::string &trimChars /*= " \n\r\t"*/)
CFileHelpers::CFileHelpers() CFileHelpers::CFileHelpers()
{ {
FileBufSize = 0xFFFF; FileBufSize = 0xFFFF;
FileBuf = (char*)malloc(FileBufSize); FileBuf = new char[FileBufSize];
doCopyFlag = true; doCopyFlag = true;
} }
CFileHelpers::~CFileHelpers() CFileHelpers::~CFileHelpers()
{ {
if (FileBuf != NULL) if (FileBuf != NULL)
free(FileBuf); delete [] FileBuf;
} }
CFileHelpers* CFileHelpers::getInstance() CFileHelpers* CFileHelpers::getInstance()
@@ -271,7 +271,7 @@ bool CFileHelpers::copyFile(const char *Src, const char *Dst, mode_t mode)
if ((fd1 = open(Src, O_RDONLY)) < 0) if ((fd1 = open(Src, O_RDONLY)) < 0)
return false; return false;
if ((fd2 = open(Dst, O_WRONLY | O_CREAT)) < 0) { if ((fd2 = open(Dst, O_WRONLY | O_CREAT)) < 0) {
close(fd2); close(fd1);
return false; return false;
} }
@@ -294,8 +294,11 @@ bool CFileHelpers::copyFile(const char *Src, const char *Dst, mode_t mode)
if (doCopyFlag) { if (doCopyFlag) {
lseek64(fd2, 0, SEEK_SET); lseek64(fd2, 0, SEEK_SET);
off64_t fsizeDst64 = lseek64(fd2, 0, SEEK_END); off64_t fsizeDst64 = lseek64(fd2, 0, SEEK_END);
if (fsizeSrc64 != fsizeDst64) if (fsizeSrc64 != fsizeDst64){
close(fd1);
close(fd2);
return false; return false;
}
} }
} }
else { // < 2GB else { // < 2GB
@@ -316,8 +319,11 @@ bool CFileHelpers::copyFile(const char *Src, const char *Dst, mode_t mode)
if (doCopyFlag) { if (doCopyFlag) {
lseek(fd2, 0, SEEK_SET); lseek(fd2, 0, SEEK_SET);
long fsizeDst = lseek(fd2, 0, SEEK_END); long fsizeDst = lseek(fd2, 0, SEEK_END);
if (fsizeSrc != fsizeDst) if (fsizeSrc != fsizeDst){
close(fd1);
close(fd2);
return false; return false;
}
} }
} }
close(fd1); close(fd1);