Tales from the debugging land
February 21, 2010
Here’s a funny bug I wanted to share with you (where “you” means all audience with bare minimum C++ understanding). I’ve been working today to add a putter interface to Kolf 2. (Attention: There are three Kolfs at the moment. I did previously explain why.) After fixing one or two crashes and fighting against Qt’s partly abstruse event forwarding (why do I have to setAcceptHoverEvents() in order to get mouseMoveEvent()s?!?), everything worked as it should, though there was one problem left: A mouse move event needed around 1 second to be processed. Callgrind to the rescue!
Okay, I admit that I had not expected the result to be so very clear. Let’s have a look at that method then:
QPointF lineEnd = m_cursorPosition;
for (int i = 0; i < m_direction; ++i)
Looks quite compact, doesn’t it? Hm, rotateBy90Degrees is called multiple times, perhaps that’s the bottleneck:
void rotateBy90Degrees(QPointF& p)
qreal x = p.x();
p.rx() = p.y();
p.ry() = -x;
Doesn’t look like it could be any more efficient. But even if it was unefficient, m_direction always has a value smaller than four, because everything else is nonsense. Anyway, let’s have a look at what value is in m_direction:
qDebug() << m_direction; //just before the for loop
A quick look in the constructor… Bah, I hate you, C++! Not only do you initialize int member variables with random values, but you also initialize them with random values that are always dividable by four so I don’t notice!