Skip to content

Commit

Permalink
Fix memory leaks (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lecrapouille authored and sebastiandev committed Mar 20, 2019
1 parent 7916d33 commit 81bb33b
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 20 deletions.
53 changes: 45 additions & 8 deletions zipper/unzipper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -329,17 +329,28 @@ namespace zipper {

Impl(Unzipper& outer) : m_outer(outer), m_zipmem(), m_filefunc()
{
m_zipmem.base = NULL;
m_zf = NULL;
}

~Impl()
{
close();
}

void close()
{
if (m_zf)
if (m_zf != NULL)
{
unzClose(m_zf);
m_zf = NULL;
}

if (m_zipmem.base != NULL)
{
free(m_zipmem.base);
m_zipmem.base = NULL;
}
}

bool initFile(const std::string& filename)
Expand Down Expand Up @@ -376,7 +387,8 @@ namespace zipper {
{
if (!buffer.empty())
{
m_zipmem.base = (char*)buffer.data();
m_zipmem.base = (char*) malloc (buffer.size() * sizeof (char));
memcpy(m_zipmem.base, (char*) buffer.data(), buffer.size());
m_zipmem.size = (uLong)buffer.size();
}

Expand Down Expand Up @@ -467,7 +479,10 @@ namespace zipper {
, m_impl(new Impl(*this))
{
if (!m_impl->initWithStream(m_ibuffer))
throw EXCEPTION_CLASS("Error loading zip in memory!");
{
release();
throw EXCEPTION_CLASS("Error loading zip in memory!");
}
m_open = true;
}

Expand All @@ -479,7 +494,10 @@ namespace zipper {
, m_impl(new Impl(*this))
{
if (!m_impl->initWithVector(m_vecbuffer))
throw EXCEPTION_CLASS("Error loading zip in memory!");
{
release();
throw EXCEPTION_CLASS("Error loading zip in memory!");
}

m_open = true;
}
Expand All @@ -493,7 +511,10 @@ namespace zipper {
, m_impl(new Impl(*this))
{
if (!m_impl->initFile(zipname))
throw EXCEPTION_CLASS("Error loading zip file!");
{
release();
throw EXCEPTION_CLASS("Error loading zip file!");
}

m_open = true;
}
Expand All @@ -508,14 +529,17 @@ namespace zipper {
, m_impl(new Impl(*this))
{
if (!m_impl->initFile(zipname))
throw EXCEPTION_CLASS("Error loading zip file!");

{
release();
throw EXCEPTION_CLASS("Error loading zip file!");
}
m_open = true;
}

Unzipper::~Unzipper(void)
Unzipper::~Unzipper()
{
close();
release();
}

std::vector<ZipEntry> Unzipper::entries()
Expand Down Expand Up @@ -550,6 +574,19 @@ namespace zipper {
return m_impl->extractAll(destination, std::map<std::string, std::string>());
}

void Unzipper::release()
{
if (!m_usingMemoryVector)
{
delete &m_vecbuffer;
}
if (!m_usingStream)
{
delete &m_ibuffer;
}
delete m_impl;
}

void Unzipper::close()
{
if (m_open)
Expand Down
3 changes: 2 additions & 1 deletion zipper/unzipper.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace zipper {
Unzipper(const std::string& zipname);
Unzipper(const std::string& zipname, const std::string& password);

~Unzipper(void);
~Unzipper();

std::vector<ZipEntry> entries();

Expand All @@ -33,6 +33,7 @@ namespace zipper {
void close();

private:
void release();
std::string m_password;
std::string m_zipname;
std::istream& m_ibuffer;
Expand Down
57 changes: 47 additions & 10 deletions zipper/zipper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,15 @@ namespace zipper {
Impl(Zipper& outer) : m_outer(outer), m_zipmem(), m_filefunc()
{
m_zf = NULL;
m_zipmem.base = NULL;
//m_filefunc = { 0 };
}

~Impl()
{
close();
}

bool initFile(const std::string& filename)
{
#ifdef USEWIN32IOAPI
Expand Down Expand Up @@ -56,7 +62,8 @@ namespace zipper {

if (size > 0)
{
m_zipmem.base = new char[(size_t)size];
if (m_zipmem.base != NULL) { free(m_zipmem.base); }
m_zipmem.base = (char*) malloc (size * sizeof (char));
stream.read(m_zipmem.base, size);
}

Expand All @@ -71,8 +78,9 @@ namespace zipper {

if (!buffer.empty())
{
m_zipmem.base = new char[buffer.size()];
memcpy(m_zipmem.base, (char*)buffer.data(), buffer.size());
if (m_zipmem.base != NULL) { free(m_zipmem.base); }
m_zipmem.base = (char*) malloc (buffer.size() * sizeof (char));
memcpy(m_zipmem.base, (char*) buffer.data(), buffer.size());
m_zipmem.size = (uLong)buffer.size();
}

Expand Down Expand Up @@ -168,8 +176,11 @@ namespace zipper {

void close()
{
if (m_zf)
if (m_zf != NULL)
{
zipClose(m_zf, NULL);
m_zf = NULL;
}

if (m_zipmem.base && m_zipmem.limit > 0)
{
Expand All @@ -183,7 +194,11 @@ namespace zipper {
m_outer.m_obuffer.write(m_zipmem.base, m_zipmem.limit);
}

free(m_zipmem.base);
if (m_zipmem.base != NULL)
{
free(m_zipmem.base);
m_zipmem.base = NULL;
}
}
};

Expand All @@ -199,8 +214,10 @@ namespace zipper {
, m_impl(new Impl(*this))
{
if (!m_impl->initFile(zipname))
{
release();
throw EXCEPTION_CLASS("Error creating zip in file!");

}
m_open = true;
}

Expand All @@ -214,8 +231,10 @@ namespace zipper {
, m_impl(new Impl(*this))
{
if (!m_impl->initFile(zipname))
{
release();
throw EXCEPTION_CLASS("Error creating zip in file!");

}
m_open = true;
}

Expand All @@ -227,8 +246,10 @@ namespace zipper {
, m_impl(new Impl(*this))
{
if (!m_impl->initWithStream(m_obuffer))
{
release();
throw EXCEPTION_CLASS("Error creating zip in memory!");

}
m_open = true;
}

Expand All @@ -240,14 +261,30 @@ namespace zipper {
, m_impl(new Impl(*this))
{
if (!m_impl->initWithVector(m_vecbuffer))
{
release();
throw EXCEPTION_CLASS("Error creating zip in memory!");

}
m_open = true;
}

Zipper::~Zipper(void)
Zipper::~Zipper()
{
close();
release();
}

void Zipper::release()
{
if (!m_usingMemoryVector)
{
delete &m_vecbuffer;
}
if (!m_usingStream)
{
delete &m_obuffer;
}
delete m_impl;
}

bool Zipper::add(std::istream& source, const std::string& nameInZip, zipFlags flags)
Expand Down
3 changes: 2 additions & 1 deletion zipper/zipper.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace zipper {
Zipper(const std::string& zipname);
Zipper(const std::string& zipname, const std::string& password);

~Zipper(void);
~Zipper();

bool add(std::istream& source, const std::string& nameInZip = std::string(), zipFlags flags = Better);
bool add(const std::string& fileOrFolderPath, zipFlags flags = Better);
Expand All @@ -29,6 +29,7 @@ namespace zipper {
void close();

private:
void release();
std::string m_password;
std::string m_zipname;
std::iostream& m_obuffer;
Expand Down

0 comments on commit 81bb33b

Please sign in to comment.