Skip to content

Commit 22a36be

Browse files
committed
Fix memory leak in document parsing code
1 parent 9210485 commit 22a36be

3 files changed

Lines changed: 17 additions & 6 deletions

File tree

DAQController.cc

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,11 @@ void DAQController::End(){
127127
fStatus = 0;
128128

129129
if(fRawDataBuffer != NULL){
130-
fLog->Entry("Deleting uncleared data buffer",
131-
MongoLog::Debug);
130+
std::stringstream warn_entry;
131+
warn_entry<<"Deleting uncleared data buffer of size "<<
132+
fRawDataBuffer->size();
133+
fLog->Entry(warn_entry.str(),
134+
MongoLog::Warning);
132135
for(unsigned int i=0; i<fRawDataBuffer->size(); i++){
133136
delete[] (*fRawDataBuffer)[i].buff;
134137
}

MongoInserter.cc

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ std::string MongoInserter::FormatString(const std::string format,
3535

3636
void MongoInserter::ParseDocuments(
3737
std::vector<bsoncxx::document::value> &doc_array,
38-
data_packet dp){
38+
data_packet dp, std::vector<u_int32_t*>&pointer_arr){
3939
// Take a buffer and break it up into one document per channel
4040
// Put these documents into doc array
4141

@@ -121,10 +121,11 @@ void MongoInserter::ParseDocuments(
121121
reinterpret_cast<unsigned char*>(channel_payload)}
122122
<< bsoncxx::builder::stream::finalize);
123123

124-
124+
pointer_arr.push_back(channel_payload);
125125
}
126126
else{
127127
std::cout<<"FAIL HARD"<<std::endl;
128+
delete[] channel_payload;
128129
}
129130
idx+=channel_size-2;
130131
}
@@ -164,6 +165,7 @@ int MongoInserter::ReadAndInsertData(){
164165
std::vector <data_packet> *readVector=NULL;
165166
int read_length = fDataSource->GetData(readVector);
166167
std::vector<bsoncxx::document::value> documents;
168+
std::vector<u_int32_t*> pointer_arr;
167169

168170
while(fActive || read_length>0){
169171
if(readVector != NULL){
@@ -188,7 +190,7 @@ int MongoInserter::ReadAndInsertData(){
188190
<< bsoncxx::builder::stream::finalize);
189191
*/
190192
// Bulk Inserts
191-
ParseDocuments(documents, (*readVector)[i]);
193+
ParseDocuments(documents, (*readVector)[i], pointer_arr);
192194

193195
if(documents.size()>fBulkInsertSize){
194196
try{
@@ -202,6 +204,9 @@ int MongoInserter::ReadAndInsertData(){
202204
fErrorBit = true;
203205
documents.clear();
204206
}
207+
for(unsigned int i=0; i<pointer_arr.size(); i++)
208+
delete[] pointer_arr[i];
209+
pointer_arr.clear();
205210
}
206211

207212
//coll.insert_one(bsoncxx::builder::stream::document{} <<
@@ -214,6 +219,9 @@ int MongoInserter::ReadAndInsertData(){
214219
if(documents.size()>0){
215220
coll.insert_many(documents);
216221
documents.clear();
222+
for(unsigned int i=0; i<pointer_arr.size(); i++)
223+
delete[] pointer_arr[i];
224+
pointer_arr.clear();
217225
}
218226
usleep(10000); // 10ms sleep
219227
read_length = fDataSource->GetData(readVector);

MongoInserter.hh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public:
4141
bool CheckError(){ return fErrorBit; };
4242
private:
4343
static void ParseDocuments(std::vector<bsoncxx::document::value> &doc_array,
44-
data_packet dp);
44+
data_packet dp, std::vector<u_int32_t*> &pointer_arr);
4545

4646
//std::string FormatString(const std::string& format, ...);
4747
std::string FormatString(const std::string format,

0 commit comments

Comments
 (0)