Bug Summary

File:programs/Utilities/hdevio_scan/hdevio_scan.cc
Location:line 107, column 4
Description:Potential leak of memory pointed to by 'hdevio'

Annotated Source Code

1
2#include <unistd.h>
3#include <stdlib.h>
4#include <stdint.h>
5#include <time.h>
6
7#include <iostream>
8#include <string>
9#include <vector>
10#include <stack>
11#include <thread>
12using namespace std;
13
14#include <TFile.h>
15
16#include "DMapEVIOWords.h"
17
18#include <DAQ/HDEVIO.h>
19
20
21void Usage(string mess);
22void ParseCommandLineArguments(int narg, char *argv[]);
23void PrintSummary(void);
24void MapEVIOWords(void);
25
26
27vector<string> filenames;
28bool PRINT_SUMMARY = true;
29bool MAP_WORDS = false;
30string ROOT_FILENAME = "hdevio_scan.root";
31uint64_t MAX_EVIO_EVENTS = 20000;
32uint32_t BLOCK_SIZE = 20; // used for daq_block_size histogram
33
34//----------------
35// main
36//----------------
37int main(int narg, char *argv[])
38{
39
40 ParseCommandLineArguments(narg, argv);
41
42 if(PRINT_SUMMARY) PrintSummary();
43
44 if(MAP_WORDS ) MapEVIOWords();
45
46 return 0;
47}
48
49//----------------
50// Usage
51//----------------
52void Usage(string mess="")
53{
54 cout << endl;
55 cout << "Usage:" << endl;
56 cout << endl;
57 cout <<" hdevio [options] file.evio [file2.evio ...]" << endl;
58 cout << endl;
59 cout << "options:" << endl;
60 cout << " -h, --help Print this usage statement" << endl;
61 cout << " -w Make histogram of population by word type" << endl;
62 cout << " -r file.root Set name of ROOT file to save histo to. " << endl;
63 cout << " (implies -w)" << endl;
64 cout << " -m max_events Max. EVIO events (not physics events) to process." << endl;
65 cout << " -b block_size EVIO events to add for daq_block_size histo." << endl;
66 cout << endl;
67
68 if(mess != "") cout << endl << mess << endl << endl;
69
70 exit(0);
71}
72
73//----------------
74// ParseCommandLineArguments
75//----------------
76void ParseCommandLineArguments(int narg, char *argv[])
77{
78
79 if(narg<2) Usage("You must supply a filename!");
80
81 for(int i=1; i<narg; i++){
82 string arg = argv[i];
83 string next = (i+1)<narg ? argv[i+1]:"";
84
85 if(arg == "-h" || arg == "--help") Usage();
86 else if(arg == "-w"){ MAP_WORDS = true; PRINT_SUMMARY = false; }
87 else if(arg == "-r"){ MAP_WORDS = true; PRINT_SUMMARY = false; ROOT_FILENAME = next; i++;}
88 else if(arg == "-m"){ MAX_EVIO_EVENTS = atoi(next.c_str()); i++;}
89 else if(arg == "-b"){ BLOCK_SIZE = atoi(next.c_str()); i++;}
90 else if(arg[0] == '-') {cout << "Unknown option \""<<arg<<"\" !" << endl; exit(-1);}
91 else filenames.push_back(arg);
92 }
93}
94
95//----------------
96// PrintSummary
97//----------------
98void PrintSummary(void)
99{
100 // Loop over input files
101 for(uint32_t i=0; i<filenames.size(); i++){
1
Loop condition is true. Entering loop body
3
Loop condition is true. Entering loop body
5
Loop condition is true. Entering loop body
102 string &filename = filenames[i];
103 cout << "Processing file " << (i+1) << "/" << filenames.size() << " : " << filename << endl;
104
105 HDEVIO *hdevio = new HDEVIO(filename);
6
Memory is allocated
106 if(!hdevio->is_open){
2
Taking false branch
4
Taking false branch
7
Taking true branch
107 cout << hdevio->err_mess.str() << endl;
8
Potential leak of memory pointed to by 'hdevio'
108 continue;
109 }
110
111 time_t start_time = time(NULL__null);
112 hdevio->PrintFileSummary();
113 time_t end_time = time(NULL__null);
114
115 delete hdevio;
116
117 cout << (end_time - start_time) << " sec " << endl;
118 }
119}
120
121//----------------
122// MapEVIOWords
123//----------------
124void MapEVIOWords(void)
125{
126 cout << endl;
127 cout << "Mapping will be limited to first " << MAX_EVIO_EVENTS << " events per input file" << endl;
128
129 // Open ROOT file
130 TFile *rootfile = new TFile(ROOT_FILENAME.c_str(), "RECREATE");
131
132 DMapEVIOWords mapevio;
133
134 // Loop over input files
135 uint64_t Nevents = 0;
136 for(uint32_t i=0; i<filenames.size(); i++){
137
138 // Open EVIO file
139 string &filename = filenames[i];
140 cout << "Processing file " << (i+1) << "/" << filenames.size() << " : " << filename << endl;
141 HDEVIO *hdevio = new HDEVIO(filename);
142 if(!hdevio->is_open){
143 cout << hdevio->err_mess.str() << endl;
144 continue;
145 }
146
147 // Read all events in file
148 uint32_t buff_len = 1000;
149 uint32_t *buff = new uint32_t[buff_len];
150 bool done = false;
151 while(!done){
152 hdevio->readNoFileBuff(buff, buff_len);
153 switch(hdevio->err_code){
154 case HDEVIO::HDEVIO_OK:
155 mapevio.ParseEvent(buff);
156 break;
157 case HDEVIO::HDEVIO_USER_BUFFER_TOO_SMALL:
158 buff_len = hdevio->last_event_len;
159 delete buff;
160 buff = new uint32_t[buff_len];
161 break;
162 case HDEVIO::HDEVIO_EOF:
163 cout << endl << " end of file" << endl;
164 done = true;
165 break;
166 default:
167 cout << endl;
168 cout << hdevio->err_mess.str() << endl;
169 done = true;
170 break;
171
172 }
173
174 if((++Nevents % 1000) == 0) {
175 int percent_done = (100*Nevents)/MAX_EVIO_EVENTS;
176 cout << " " << Nevents << "/" << MAX_EVIO_EVENTS << " (" << percent_done << "%) processed \r"; cout.flush();
177 }
178 if(Nevents>MAX_EVIO_EVENTS) break;
179 }
180 cout << endl;
181
182 // Close EVIO file
183 delete hdevio;
184 }
185
186 // Flush and close ROOT file
187 rootfile->Write();
188 rootfile->Close();
189 delete rootfile;
190}
191
192
193