Not really SDL related. C++ problem

Hello there. I can compile my program, but when I run it, it crashes at a
specific point it the program with this error:
terminate called after throwing an instance of 'std::out_of_range’
what(): basic_string::substr
Aborted

I appreciate any help and opinions… I cannot seriously figure out what
makes the program crash…

Thanks beforehand.

/Chris

P.S
Here is the relevant code:
void Window::LoadSettings(){
//This function should load the config file and set the values in
tha
//classes. It should look for values like the windowsize etc. If
it’s
//able to open the file, but some data is missing, it should append
//some nice default-setting to the file. If it on the other hand
fails
//to open the file completely, it should call the CreateSettings()
//function.
filename = new char[11];
filename = “config.wtf”;
string line;
char type;
ifstream configfile;
do{
configfile.open(filename);
if(!(configfile.is_open())){
cout << "Unable to open config file " <<
filename << “.\nCreating a new configfile instead.\n”;
CreateSettings();
}
}while(!(configfile.is_open()));

    cout << "Successfully opened configfile " << filename << ".\n";

    while(!configfile.eof()){
    getline(configfile,line);
    istringstream typo(line.substr(0,line.find(" ")));
    typo >> type;
    unsigned int length = line.length() - line.find(" ");
            switch(type){
                    case 'w':{
                            istringstream convstr(line.substr(line.find("

“),length));
unsigned int value;
convstr >> value;
width = value;}
break;
case ‘h’:{
istringstream convstr(line.substr(line.find(”
"),length));
unsigned int value;
convstr >> value;
height = value;}
break;
case ‘d’:{
istringstream convstr(line.substr(line.find("
"),length));
unsigned int value;
convstr >> value;
depth = value;}
break;
case ‘s’:{
istringstream convstr(line.substr(line.find("
"),length));
unsigned int value;
convstr >> value;
//sensitivity = value;
}
break;
case ‘f’:{
istringstream convstr(line.substr(line.find("
"),length));
bool value;
convstr >> value;
fullscreen = value;
}
break;
default:
break;
}
}
configfile.close();
}

I don’t know which specific line of code is causing this, but I’ve
generally seen it when one attempts to store return values from some
C++ string methods. The appropriate return type is ‘size_type’, but I
see you’re attempting to store in ‘char’ and ‘unsigned int’, both of
which might be too small.

SteveOn March 8, 2007 04:58:44 pm Chris Rajula wrote:

Hello there. I can compile my program, but when I run it, it crashes
at a specific point it the program with this error:
terminate called after throwing an instance of 'std::out_of_range’
what(): basic_string::substr
Aborted

terminate called after throwing an instance of 'std::out_of_range’
what(): basic_string::substr
[…snip…]
istringstream typo(line.substr(0,line.find(" ")));

I would guess that line.find() is returning -1 or something on a line
without a space (an empty line?). As you don’t catch the exception (and
probably shouldn’t in this case), the default exception handler is
terminating the app.

That being said: we are helpful people–at least I hope so–but let’s
try to keep the SDL list limited to SDL questions.

–ryan.

Actually, the code in itself works just fine.
I’ve checked this by looking at the output with cout…
The switch-case-thingy does it’s thing, i.e it reads the width,height, depth
and so on from the file, and stores them in width height etc.
So, the code-snippet (the switch-case) finishes it’s task, but it never
seems to complete itself per se…

It’s very strange all in all…
P.S

Are there any good C+±related mailing lists out there?

/ChrisOn 3/8/07, Ryan C. Gordon wrote:

terminate called after throwing an instance of 'std::out_of_range’
what(): basic_string::substr
[…snip…]
istringstream typo(line.substr(0,line.find(" ")));

I would guess that line.find() is returning -1 or something on a line
without a space (an empty line?). As you don’t catch the exception (and
probably shouldn’t in this case), the default exception handler is
terminating the app.

That being said: we are helpful people–at least I hope so–but let’s
try to keep the SDL list limited to SDL questions.

–ryan.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

You may have a newline after your last line of data. IIRC that can
give you one blank line before the EOF but after your last line of
data. Try this: as soon as you read in a line, check the length of it
and echo that to the screen. That might help narrow it down.On 3/8/07, Chris Rajula wrote:

Actually, the code in itself works just fine.
I’ve checked this by looking at the output with cout…
The switch-case-thingy does it’s thing, i.e it reads the width,height, depth
and so on from the file, and stores them in width height etc.
So, the code-snippet (the switch-case) finishes it’s task, but it never
seems to complete itself per se…

It’s very strange all in all…
P.S

Are there any good C+±related mailing lists out there?

/Chris

On 3/8/07, Ryan C. Gordon wrote:

terminate called after throwing an instance of 'std::out_of_range’
what(): basic_string::substr
[…snip…]
istringstream typo(line.substr(0,line.find(" ")));

I would guess that line.find() is returning -1 or something on a line
without a space (an empty line?). As you don’t catch the exception (and
probably shouldn’t in this case), the default exception handler is
terminating the app.

That being said: we are helpful people–at least I hope so–but let’s
try to keep the SDL list limited to SDL questions.

–ryan.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

I’ve isolated the problem down to the conversion to the boolean value.

I used gdb to trace the error :)On 3/8/07, Justin Coleman wrote:

You may have a newline after your last line of data. IIRC that can
give you one blank line before the EOF but after your last line of
data. Try this: as soon as you read in a line, check the length of it
and echo that to the screen. That might help narrow it down.

On 3/8/07, Chris Rajula <@Chris_Rajula> wrote:

Actually, the code in itself works just fine.
I’ve checked this by looking at the output with cout…
The switch-case-thingy does it’s thing, i.e it reads the width,height,
depth
and so on from the file, and stores them in width height etc.
So, the code-snippet (the switch-case) finishes it’s task, but it never
seems to complete itself per se…

It’s very strange all in all…
P.S

Are there any good C+±related mailing lists out there?

/Chris

On 3/8/07, Ryan C. Gordon wrote:

terminate called after throwing an instance of 'std::out_of_range’
what(): basic_string::substr
[…snip…]
istringstream typo(line.substr(0,line.find(" ")));

I would guess that line.find() is returning -1 or something on a line
without a space (an empty line?). As you don’t catch the exception
(and

probably shouldn’t in this case), the default exception handler is
terminating the app.

That being said: we are helpful people–at least I hope so–but let’s
try to keep the SDL list limited to SDL questions.

–ryan.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org