Another common mistake
int process_event (Event * evt)
Packet *p = evt->getPacket(1001);
cout << setw(6) << s->iValue(0)
<< setw(6) << s->iValue(4)
<< setw(6) << s->iValue(5) << endl;
We have seen something like this...
int process_event (Event * evt)
Packet *s1 = evt->getPacket(1001);
Packet *s2 = evt->getPacket(1002);
cout << setw(6) << s1->iValue(0)
<< setw(6) << s2->iValue(0)
Indeed. On the right, the “if” becomes true only if both the s1 and s2 objects are created. They usually are, so usually there is no problem. However, we might have a corrupt event and only one object gets created, not the other one. Then the “delete” is never reached, and as we go on, we leave the object behind. This is a “bad” memory leak. It is bad because
- Those memory leaks occur only very infrequently, and are extremely hard to recognize (not to mention to find) in the development phase of a project.
- It takes a corrupt or otherwise non-standard events to occur, and in the test phase you often work with simulated data which are less likely to show this problem;
- the problem will show up only when there is a significant amount of data to process, and that’s usually when the production has already started.
int process_event (Event * evt)
Packet *s1 = evt->getPacket(1001);
Packet *s2 = evt->getPacket(1002);
cout << setw(6) << s1->iValue(0)
<< setw(6) << s2->iValue(0)