Author Topic: Another C++ Issue  (Read 11651 times)

Capn_Fish

  • Hero Member
  • *****
  • Posts: 2342
    • View Profile
    • http://
Another C++ Issue
« on: November 21, 2007, 07:28:29 pm »
I'm getting an error:

Code: [Select]
terminate called after throwing an instance of 'std::ios_base::failure'
    what():  basic_ios::clear
Aborted

With my latest C++/SDL app. What causes this, and what can I do about it? It works fine on my laptop, but when I try to run it on my Z, I got the above error.

Any help would be much appreciated.
SL-C750- pdaXrom beta 1 (mostly unused)
Current distro: Gentoo

koan

  • Sr. Member
  • ****
  • Posts: 370
    • View Profile
    • http://www.lyndonhill.com
Another C++ Issue
« Reply #1 on: November 22, 2007, 04:05:41 am »
I don't know so much about exception processing or lowlevel ios stuff, so this is a bit of a guess.

The code is clearing a stream and setting some flags; the flags being set aren't supported by the Z C++ RTL. I would grep the code for "clear(" and look for an iostream. You'll need to change the parameters to clear(), probably some weird default that doesn't really matter or something new that isn't supported by the Z RTL.

Don't know about the what(); maybe it's the what() function that is calling clear() ? Another thing to grep for.
Zocalo Feed Reader : Thai on Zaurus : Dictionaries for zbedic : Sharp ROM package feed
HELUX Handheld Embedded Linux Blog
SL-C3200 Multiboot : SL-C750  Sharp ROM

Snappy

  • Hero Member
  • *****
  • Posts: 793
    • View Profile
    • http://
Another C++ Issue
« Reply #2 on: November 22, 2007, 09:36:06 am »
You may also want to put it some debug output statements and do a trace.
btw, was there a coredump? From your output, don't seem to have one. You could potentially trace the faulty statement with the dump.
Snappy!
------------------------
Akita (Daily use)
<span style='font-size:8pt;line-height:100%'>SL-C1000 with Cacko 1.23 full
Accessories: Wifi XI-825 CF  |  16GB (6) SDHC (Transcend) | 1GB 80x CF (RiDATA PRO-2) </span>

Collie (Sandbox)
<span style='font-size:8pt;line-height:100%'>SL-5500 with OZ/GPE 3.5.4.1 alpha3 build 2006-04-27
Accessories: 512MB A-Data SD | 64MB Toshiba SD</span>

Capn_Fish

  • Hero Member
  • *****
  • Posts: 2342
    • View Profile
    • http://
Another C++ Issue
« Reply #3 on: November 22, 2007, 11:06:36 am »
I don't call clear anywhere. What other functions call it?

Thanks for the help.

EDIT: Here's my backup tarball from yesterday, if anybody would care to look at it.
« Last Edit: November 22, 2007, 11:08:23 am by Capn_Fish »
SL-C750- pdaXrom beta 1 (mostly unused)
Current distro: Gentoo

Snappy

  • Hero Member
  • *****
  • Posts: 793
    • View Profile
    • http://
Another C++ Issue
« Reply #4 on: November 22, 2007, 12:20:01 pm »
A quick look .. could it be something to do with http://opensource.adobe.com/gil/html/group..._e_g___i_o.html

"Throws std::ios_base::failure if none of the types in Images are compatible with the type on disk. "

EDIT: btw, can you put in debug output messages in your main so that you know where this error is coming from? Will be helpful for me to human-trace your code as I don't have linux dev setup.
« Last Edit: November 22, 2007, 12:22:36 pm by Snappy »
Snappy!
------------------------
Akita (Daily use)
<span style='font-size:8pt;line-height:100%'>SL-C1000 with Cacko 1.23 full
Accessories: Wifi XI-825 CF  |  16GB (6) SDHC (Transcend) | 1GB 80x CF (RiDATA PRO-2) </span>

Collie (Sandbox)
<span style='font-size:8pt;line-height:100%'>SL-5500 with OZ/GPE 3.5.4.1 alpha3 build 2006-04-27
Accessories: 512MB A-Data SD | 64MB Toshiba SD</span>

Capn_Fish

  • Hero Member
  • *****
  • Posts: 2342
    • View Profile
    • http://
Another C++ Issue
« Reply #5 on: November 22, 2007, 12:49:33 pm »
Sure, I'll go put some cout'd numbers in now.

EDIT: Attached source

EDIT2: It's failing between 4 & 5, or in the "ReadMap" function:

EDIT3: Now, after making NO changes other than adding in some debug output, it's just segfaulting () after it tries to move to the last character of the map. I think I've got this now, so, unless you are really bent on helping, go enjoy your Thanksgivings (if you're in the US, otherwise, just enjoy your day).

I should really stop posting these types of issues at night when everything looks hopeless...

Thanks again for your help!

EDIT4: Yup, I got it (one too few characters in my array. Funny that it worked on the laptop...). I'll leave the source up as a teaser.
« Last Edit: November 22, 2007, 01:20:32 pm by Capn_Fish »
SL-C750- pdaXrom beta 1 (mostly unused)
Current distro: Gentoo

koan

  • Sr. Member
  • ****
  • Posts: 370
    • View Profile
    • http://www.lyndonhill.com
Another C++ Issue
« Reply #6 on: November 23, 2007, 04:26:36 am »
Quote from: Capn_Fish
EDIT3: Now, after making NO changes other than adding in some debug output, it's just segfaulting

When the behaviour of the program changes after making such slight changes that's a clear indication of reading/writing to illegal memory.

You should have run valgrind on the program when compiled on your desktop to make sure there are no obvious errors, it would have caught the problem and told you where it was.
Zocalo Feed Reader : Thai on Zaurus : Dictionaries for zbedic : Sharp ROM package feed
HELUX Handheld Embedded Linux Blog
SL-C3200 Multiboot : SL-C750  Sharp ROM

Snappy

  • Hero Member
  • *****
  • Posts: 793
    • View Profile
    • http://
Another C++ Issue
« Reply #7 on: November 25, 2007, 12:19:19 pm »
Quote from: Capn_Fish
Sure, I'll go put some cout'd numbers in now.

EDIT: Attached source

EDIT2: It's failing between 4 & 5, or in the "ReadMap" function:

EDIT3: Now, after making NO changes other than adding in some debug output, it's just segfaulting () after it tries to move to the last character of the map. I think I've got this now, so, unless you are really bent on helping, go enjoy your Thanksgivings (if you're in the US, otherwise, just enjoy your day).

I should really stop posting these types of issues at night when everything looks hopeless...

Thanks again for your help!

EDIT4: Yup, I got it (one too few characters in my array. Funny that it worked on the laptop...). I'll leave the source up as a teaser.

Something came up ... checking it out. Might be awhile, 'cos I'm outta town for 2 weeks ... sorry!

Quote from: koan
Quote from: Capn_Fish
EDIT3: Now, after making NO changes other than adding in some debug output, it's just segfaulting

When the behaviour of the program changes after making such slight changes that's a clear indication of reading/writing to illegal memory.

You should have run valgrind on the program when compiled on your desktop to make sure there are no obvious errors, it would have caught the problem and told you where it was.

Aye ... sounds like a pointer problem or something ...
Snappy!
------------------------
Akita (Daily use)
<span style='font-size:8pt;line-height:100%'>SL-C1000 with Cacko 1.23 full
Accessories: Wifi XI-825 CF  |  16GB (6) SDHC (Transcend) | 1GB 80x CF (RiDATA PRO-2) </span>

Collie (Sandbox)
<span style='font-size:8pt;line-height:100%'>SL-5500 with OZ/GPE 3.5.4.1 alpha3 build 2006-04-27
Accessories: 512MB A-Data SD | 64MB Toshiba SD</span>

Snappy

  • Hero Member
  • *****
  • Posts: 793
    • View Profile
    • http://
Another C++ Issue
« Reply #8 on: November 25, 2007, 12:36:33 pm »
Just curious ...

mapp.cpp, line 147
Quote
char Line[60];
   MapIn.open("../maps/test.map");
   for(int y = 0; y < 15; y++)
   {
      MapIn.getline(Line,61);

Should 61 MapIn.getline(Line, 61) be 60 instead? This might be causing an overflow or write pass buffer error, which can/will cause corruption if the byte past Line[59] is code or something vital.

http://www.cplusplus.com/reference/iostrea...am/getline.html

Have not read the other code yet, but this could be a place to start perhaps.
Snappy!
------------------------
Akita (Daily use)
<span style='font-size:8pt;line-height:100%'>SL-C1000 with Cacko 1.23 full
Accessories: Wifi XI-825 CF  |  16GB (6) SDHC (Transcend) | 1GB 80x CF (RiDATA PRO-2) </span>

Collie (Sandbox)
<span style='font-size:8pt;line-height:100%'>SL-5500 with OZ/GPE 3.5.4.1 alpha3 build 2006-04-27
Accessories: 512MB A-Data SD | 64MB Toshiba SD</span>

Snappy

  • Hero Member
  • *****
  • Posts: 793
    • View Profile
    • http://
Another C++ Issue
« Reply #9 on: November 25, 2007, 12:50:16 pm »
btw, if you wish, you can also include the functions GetLevelFromMap, ReadMap and SetTiles can be included as functions in the Tile class, since they seem to mainly manipulate the tiles. Well, but that's software architecture ... I digress ... hehe

Anyway, in case if you wish to have some classing done for your app, let me know, I happen to have a 'fetish' for doing classing etc ...
Snappy!
------------------------
Akita (Daily use)
<span style='font-size:8pt;line-height:100%'>SL-C1000 with Cacko 1.23 full
Accessories: Wifi XI-825 CF  |  16GB (6) SDHC (Transcend) | 1GB 80x CF (RiDATA PRO-2) </span>

Collie (Sandbox)
<span style='font-size:8pt;line-height:100%'>SL-5500 with OZ/GPE 3.5.4.1 alpha3 build 2006-04-27
Accessories: 512MB A-Data SD | 64MB Toshiba SD</span>

koan

  • Sr. Member
  • ****
  • Posts: 370
    • View Profile
    • http://www.lyndonhill.com
Another C++ Issue
« Reply #10 on: November 26, 2007, 01:08:48 am »
Quote from: Snappy
A quick look .. could it be something to do with http://opensource.adobe.com/gil/html/group..._e_g___i_o.html

"Throws std::ios_base::failure if none of the types in Images are compatible with the type on disk. "

Fish isn't using Adobe's Generic Image Library. This link is irrelevant.
Zocalo Feed Reader : Thai on Zaurus : Dictionaries for zbedic : Sharp ROM package feed
HELUX Handheld Embedded Linux Blog
SL-C3200 Multiboot : SL-C750  Sharp ROM

koan

  • Sr. Member
  • ****
  • Posts: 370
    • View Profile
    • http://www.lyndonhill.com
Another C++ Issue
« Reply #11 on: November 26, 2007, 02:55:32 am »
Hi

I decided to look at your code. I don't usually develop for SDL and my local Linux mirror is unavailable so I can't install the header files at the moment.

Anyway, first some tips:

1. If you want to add debug output, use cerr.
cout is buffered, so the program may have continued further than the output when it stopped but the buffer was not cleared.

2. Instead of outputting "1", "2" etc, add cerr << "SetUpDisplay() start\n" and cerr << "SetUpDisplay() end\n"
at the start and end of your functions. It's much easier to see what failed.

3. stringtochar
You obviously haven't heard of std::string::c_str(). Example:

char *mycharstar;
std::string mystring("Hello");
mycharstar = mystring.c_str();

4. Go easy on the global variables! A good C++ design rarely needs them.

5. Add comments in your header files, explain what a function is *supposed* to do.

When I have the SDL include files I'll be able to compile and do some debugging.
Zocalo Feed Reader : Thai on Zaurus : Dictionaries for zbedic : Sharp ROM package feed
HELUX Handheld Embedded Linux Blog
SL-C3200 Multiboot : SL-C750  Sharp ROM

Capn_Fish

  • Hero Member
  • *****
  • Posts: 2342
    • View Profile
    • http://
Another C++ Issue
« Reply #12 on: November 26, 2007, 07:42:55 am »
Thanks for the helpful comments! I love open-source for this reason, that I can just stick my code here with an issue, and somebody will find other things that are wrong with it and point me to them.

1. Never heard of cerr, I'll look into it.

2. Same as #1.

3. Haven't heard of that either. I'll look into it too.

4. I was thinking if I used some globals that would be used commonly, I could sacrifice a tiny bit of RAM to gain some speed from not having to create and destroy them so often. Misconception?

5. I don't usually do that (I never expect anybody else to look at my code), but I actually have comments in this stuff. I'm slowly adding more as I work on different sections.

Thanks a lot (Just note that much of the code posted has been replaced as  I am in the process of changing methods for moving the character about).
SL-C750- pdaXrom beta 1 (mostly unused)
Current distro: Gentoo

koan

  • Sr. Member
  • ****
  • Posts: 370
    • View Profile
    • http://www.lyndonhill.com
Another C++ Issue
« Reply #13 on: November 27, 2007, 08:12:30 pm »
Quote from: Capn_Fish
4. I was thinking if I used some globals that would be used commonly, I could sacrifice a tiny bit of RAM to gain some speed from not having to create and destroy them so often. Misconception?

Yes. Making the variable global pollutes the name space, any savings are insignificant.

Say for example you have a map class with public members width and height. All you need is a reference to the map object and you can access these variables.

If you keep a pointer to the map object then you spend an extra 32/64 bits to store the memory address and there might be 1 extra look up (go to map object, access member). Hardly a huge cost.

Quote
5. I don't usually do that (I never expect anybody else to look at my code), but I actually have comments in this stuff. I'm slowly adding more as I work on different sections.

It's design methodology. When you come back to that code to add something or hunt down some bug you'll be looking at a function and trying to work out if it's correct or not. Before I write any functions I write the class definition and comments on what the functions are supposed to do.

Some other comments:

In input.cpp:
Code: [Select]
bool eXit = false;This kind of variable naming is asking for trouble. In map.cpp
Code: [Select]
const int TileSize = 32;this is also not good: you have hard coded a global variable and the value is only accessible in a specific source file.

In screen.hpp:
Code: [Select]
const int screenWidth = 640; //Hardcoded for the Zaurus
const int screenHeight = 480; //Hardcoded for the Zaurus

You should #define this in some global configuration and #include it here. Then, when you want to recompile for another platform you can just change these variables.

In map.cpp:
Code: [Select]
        if(tens == '0' or tens == '1' or tens == '2' or tens == '3' or tens == '4' or tens == '5' or tens == '6' or tens == '7' or tens == '8' or tens == '9')
        {
                if(tens == '0')
                {
                        intTens = 0;
                }
etc
Very ouch!

Some comments on the layout:

Why don't you have a map class ?
Likewise a screen class ?
It would group together related functions and variables, improve the readability of your code

Why is CanMove() not a member of Character class ?

If you make an enumerated type for direction, then you can have code like myCharacter.Move(NORTH) which is a lot easier to read than myCharacter.Move(2).
Zocalo Feed Reader : Thai on Zaurus : Dictionaries for zbedic : Sharp ROM package feed
HELUX Handheld Embedded Linux Blog
SL-C3200 Multiboot : SL-C750  Sharp ROM