Notes on ScopePort

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;
         }
};
Share:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • LinkedIn
  • StumbleUpon
  • Technorati
  • Yigg
  • FriendFeed
  • Identi.ca
  • MySpace
  • TwitThis
:, , , , ,

5 Comments for this entry

  • Pirsey

    This is very up-to-date info. I’ll share it on Delicious.

  • Serenity

    This really helped me unravel what was happening in my code. Thanks a lot.
    (really is a nasty bug!!)

  • Korigan

    It helped me also! thank’s, i learn too ;)

    ++Korigan

  • MrJones

    Just bumped into the same and even worse error :D 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.

  • louis vuitton handbags

    I’ll gear this review to 2 types of people: current Zune owners who are considering an upgrade, and people trying to decide between a Zune and an iPod. (There are other players worth considering out there, like the Sony Walkman X, but I hope this gives you enough info to make an informed decision of the Zune vs players other than the iPod line as well.)

Leave a Reply

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!