Re: [linux-audio-dev] question about a c++ error

From: Dirk Jagdmann <doj@email-addr-hidden>
Date: Fri Oct 13 2006 - 13:02:04 EEST

> This is perhaps a bit OT (or more than a bit). I'm eager to work with a
> new macro for Open Office that inserts LilyPond-encoded music fragments
> into an OOo document, but I've hit an unusual snag. The macro is
> installed correctly, but when I try to use it I receive this error:
>
> terminate called after throwing an instance of
> 'std::logic_error'
> what():basic_string::_S_construct
> NULL not valid
>
> It almost looks poetic. :)
>
> Alas, Google wasn't much help, and the author of the macro has no idea
> why this error results (his code contains no C++). Can any C++ guru here
> shed any light on the how/what/why of the error ? Maybe even suggest a
> fix ?

The following program produces the exact error message (aka throws this
c++ exception):
$ cat a.cpp
#include <string>
int main()
{
   std::string s(NULL);
   return 0;
}
$ c++ a.cpp
$ ./a.out
terminate called after throwing an instance of 'std::logic_error'
   what(): basic_string::_S_construct NULL not valid
Aborted (core dumped)

So somewhere in the OOo code a std::string object is created but it is
passed a null pointer in the constructor. Of course you should not find
a call like "std::string s(NULL);" in the OOo codebase, but more likely
something like "std::string s(somestr);" where "somestr" is a "char*".
It could also result from a function call like

void foo(std::string s) { }
char *str=NULL;
foo(str);

However the following test program shows a different behaviour:
$ cat b.cpp
#include <string>
int main()
{
   char *str=NULL;
   std::string s;
   s=str;
   return 0;
}
$ c++ b.cpp
$ ./a.out
Segmentation fault (core dumped)

This is the reason I suspect an array call to be the likeliest case for
throwing the exception.

Now how to fix this? I suggest you compile yourself OOo with debugging
enabled, then run your OOo with gdb and do your tests which should throw
the exception. Then you can easily backtrace (bt) or go up (up) untill
you find the throwling source code line. Then you can choose to either
do a quick'n'dirty fix and just prefix the string construction with some
  "if(str==NULL)return;" or similar or look further to fix the reason
why the C-String has not been initialized correctly (thus still be NULL).

-- 
---> Dirk Jagdmann ^ doj / cubic
----> http://cubic.org/~doj
-----> http://llg.cubic.org
Received on Fri Oct 13 20:15:01 2006

This archive was generated by hypermail 2.1.8 : Fri Oct 13 2006 - 20:15:02 EEST