Skip to content

Fix memory leak in zzX #515

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions src/zzX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
**/
#include <mutex>
#include <map>
#include <memory>

#include <helib/PAlgebra.h>
#include <helib/timing.h>
Expand Down Expand Up @@ -78,7 +79,7 @@ void normalize(zzX& f)
// This function changes the NTL current zz_p modulus.
const NTL::zz_pXModulus& getPhimXMod(const PAlgebra& palg)
{
static std::map<long, NTL::zz_pXModulus*> moduli; // pointer per value of m
static std::map<long, std::unique_ptr<NTL::zz_pXModulus>> moduli; // pointer per value of m
static std::mutex pt_mtx; // control access to modifying the map

NTL::zz_p::FFTInit(0); // set "the best FFT prime" as NTL's current modulus
Expand All @@ -91,15 +92,10 @@ const NTL::zz_pXModulus& getPhimXMod(const PAlgebra& palg)

// Got the lock, insert a new entry for this value of m into the map
NTL::zz_pX phimX = NTL::conv<NTL::zz_pX>(palg.getPhimX());
NTL::zz_pXModulus* ptr =
new NTL::zz_pXModulus(phimX); // will "never" be deleted
auto ptr = std::make_unique<NTL::zz_pXModulus>(phimX);

// insert returns a pair (iterator, bool)
auto ret = moduli.insert(std::pair<long, NTL::zz_pXModulus*>(m, ptr));
if (ret.second == false) // Another thread inserted it, delete your copy
delete ptr;
// FIXME: Could leak memory if insert throws an exception
// without inserting the element (but who cares)
auto ret = moduli.insert(std::make_pair(m, std::move(ptr)));

it = ret.first; // point to the entry in the map
}
Expand Down