Nasty bugs: std::logic_error because of NULL string
by Lennart on Nov.01, 2008, under ScopePort
I recently changed the Warning::getConditions() method of ScopePort. It now takes two parameters:
- string hostid – The ID of the host on which the sensor to check is running
- string st – The ID of the sensor to check.
I started up ScopePort and got the following error after about 2-3 minutes:
terminate called after throwing an instance of ’std::logic_error’
what(): basic_string::_S_construct NULL not valid
Because of the time delay I searched for the error in the recently changed Warning::getConditions() method. I expected that an usual alarm was causing something to fail. But as my investigations went on I noticed that the time delay was exactly 180 seconds. Each time. So I thought about it and remembered that the onlineStateChecks() thread was going into full operations after 180 seconds (That is to give the clients time to send data.). I quickly found out where the error was caused: Just at the time of the Warning::getConditions() method call. The debugger didn’t even let me step into the method. It failed right at the method call. As it was pretty late and I had to get up early the next day I went to sleep and planned to ask a friend the next day.
The next morning. At the time I was explaining my Problem to the friend I already found the error. (Sometimes it is really better to go to bed…): When I updated the onlineStateChecks and clientHandler() threads to use the new method I made a mistake: I used the wrong column of the database result. This ended in 0 as second parameter. Exactly. Not “0″ but 0.
My friend explained me that the compiler was not complaining because he just accepted the 0 as a NULL string. I didn’t even look for wrong parameter types because I thought the compiler wouldn’t even think about compiling 0 as string. Lesson learnt. Thank you.
Here is a little example program that reproduces the error:
#include "TestClass.h"
using namespace std;
int main(){
TestClass test;
test.testMe("foo", 0);
return 0;
}
class TestClass {
public:
void testMe(string foo, string bar){
return;
}
};


April 24th, 2009 on 1:09 pm
This is very up-to-date info. I’ll share it on Delicious.
December 15th, 2009 on 5:02 pm
This really helped me unravel what was happening in my code. Thanks a lot.
(really is a nasty bug!!)
January 20th, 2010 on 5:33 pm
It helped me also! thank’s, i learn too
++Korigan
January 31st, 2010 on 10:15 pm
Just bumped into the same and even worse error
You can actually call something like:
test.testMe(“foo”, false);
that also gives the exception. This page gave a quick clue where to look and helped a lot.