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

This commit is contained in:
satbaby
2012-10-26 23:16:30 +02:00
parent 5db4b97d9d
commit a03445fe40

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,10 +294,13 @@ 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
long fsizeSrc = lseek(fd1, 0, SEEK_END); long fsizeSrc = lseek(fd1, 0, SEEK_END);
lseek(fd1, 0, SEEK_SET); lseek(fd1, 0, SEEK_SET);
@@ -316,10 +319,13 @@ 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);
close(fd2); close(fd2);