Skip to content

Commit

Permalink
Merge pull request #22 from yushihang/release-memory-for-CircomCircuit
Browse files Browse the repository at this point in the history
Add a destructor for struct Circom_Circuit to release dynamically allocated memory and prevent memory leaks.
  • Loading branch information
OBrezhniev authored Apr 9, 2024
2 parents 353875e + 96a8195 commit bd5dc5e
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 12 deletions.
7 changes: 7 additions & 0 deletions src/calcwit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ Circom_CalcWit::Circom_CalcWit (Circom_Circuit *aCircuit, uint maxTh) {

Circom_CalcWit::~Circom_CalcWit() {
// ...

delete[] inputSignalAssigned;

delete[] signalValues;

delete[] componentMemory;

}

uint Circom_CalcWit::getInputSignalHashPosition(u64 h) {
Expand Down
40 changes: 29 additions & 11 deletions src/circom.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,38 @@ 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() {

delete[] InputHashMap;

delete[] witness2SignalList;

delete[] circuitConstants;

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

}
};


Expand All @@ -49,12 +67,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

0 comments on commit bd5dc5e

Please sign in to comment.