Skip to content
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

Add a destructor for struct Circom_Circuit to release dynamically allocated memory and prevent memory leaks. #22

Merged
Merged
Show file tree
Hide file tree
Changes from 4 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
14 changes: 14 additions & 0 deletions src/calcwit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,20 @@ Circom_CalcWit::Circom_CalcWit (Circom_Circuit *aCircuit, uint maxTh) {

Circom_CalcWit::~Circom_CalcWit() {
// ...
if (inputSignalAssigned != nullptr) {
delete[] inputSignalAssigned;
inputSignalAssigned = nullptr;
}
yushihang marked this conversation as resolved.
Show resolved Hide resolved

if (signalValues != nullptr) {
delete[] signalValues;
signalValues = nullptr;
}

if (componentMemory != nullptr) {
delete[] componentMemory;
componentMemory = nullptr;
}
}

uint Circom_CalcWit::getInputSignalHashPosition(u64 h) {
Expand Down
49 changes: 38 additions & 11 deletions src/circom.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,47 @@ struct __attribute__((__packed__)) HashSignalInfo {
struct IODef {
u32 offset;
u32 len;
u32 *lengths;
u32 *lengths = nullptr;
};

struct IODefPair {
u32 len;
IODef* defs;
IODef* defs = nullptr;
};

struct Circom_Circuit {
// const char *P;
HashSignalInfo* InputHashMap;
u64* witness2SignalList;
FrElement* circuitConstants;
HashSignalInfo* InputHashMap = nullptr;
u64* witness2SignalList = nullptr;
FrElement* circuitConstants = nullptr;
std::map<u32,IODefPair> templateInsId2IOSignalInfo;

~Circom_Circuit() {

if (InputHashMap != nullptr) {
delete[] InputHashMap;
InputHashMap = nullptr;
}

if (witness2SignalList != nullptr) {
delete[] witness2SignalList;
witness2SignalList = nullptr;
}

if (circuitConstants != nullptr) {
delete[] circuitConstants;
circuitConstants = nullptr;
}

for (auto &pair : templateInsId2IOSignalInfo) {
auto *defs = pair.second.defs;
if (defs != nullptr) {
delete[] defs->lengths;
free(defs);
}

}
}
};


Expand All @@ -49,12 +76,12 @@ struct Circom_Component {
std::string templateName;
std::string componentName;
u64 idFather;
u32* subcomponents;
bool* subcomponentsParallel;
bool *outputIsSet; //one for each output
std::mutex *mutexes; //one for each output
std::condition_variable *cvs;
std::thread *sbct; //subcomponent threads
u32* subcomponents = nullptr;
bool* subcomponentsParallel = nullptr;
bool *outputIsSet = nullptr; //one for each output
std::mutex *mutexes = nullptr; //one for each output
std::condition_variable *cvs = nullptr;
std::thread *sbct = nullptr; //subcomponent threads

Circom_Component()
: subcomponents(0), subcomponentsParallel(0), outputIsSet(0), mutexes(0), cvs(0), sbct(0)
Expand Down
2 changes: 1 addition & 1 deletion src/witnesscalc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ Circom_Circuit* loadCircuit(const void *buffer, unsigned long buffer_size) {
memcpy((void *)defs[j].lengths,(void *)(pu32+2),len*sizeof(u32));
pu32 += len + 2;
}
p.defs = (IODef*)calloc(10, sizeof(IODef));
p.defs = (IODef*)calloc(p.len, sizeof(IODef));
for (u32 j = 0; j < p.len; j++){
p.defs[j] = defs[j];
}
Expand Down