Sign in to follow this  

printf - Game Crashing

This topic is 4246 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've been scanning this for a while, and I can't figure out why the game keeps crashing. I'm using HGE and I've narrowed it down to this line...
hge_font->printf(200, 215, "You are a %s, %s %s. You have a %s \n%s\n\n%s",
 TranslateGender(gender), TranslatecType(ctype), TranslateRace(race),
 TranslateDescription(rndClr), TranslateMind(mind), TranslateRaceType(race));
Just wondering if someone can point out my mistake? Or perhaps suggest a better way to do things.

Share this post


Link to post
Share on other sites
Yeah, make certain that all the values you're passsing to Translate<whatever>() aren't NULL and that the returns from those function calls aren't NULL either.

Share this post


Link to post
Share on other sites
Quote:
Original post by Narcis
Or perhaps suggest a better way to do things.


How about "Don't use the elipsis construct"?[smile]

But seriously, you are not giving us enough information. You seem to be using some kind of library, so you should check its documentation and make sure that the types of the parameters you are passing are what the function expects. printf style functions can't have compile-time type saftey, so you have to be extra careful.

Share this post


Link to post
Share on other sites
Ok, well, do you have the option to use the standard library for this operation. There are so many things wrong with printf when considering the C++ standard that it doesn't bare using.

Dave

Share this post


Link to post
Share on other sites
Quote:
Look at the debugger and see if all the values you are passing are valid.



Program received signal (SIGSEGV)
Segmentation fault
Cannot access memory at address 0x6d206120
error


Quote:
Yeah, make certain that all the values you're passsing to Translate<whatever>() aren't NULL and that the returns from those function calls aren't NULL either.


Check, and all have values.

Quote:
But seriously, you are not giving us enough information. You seem to be using some kind of library, so you should check its documentation and make sure that the types of the parameters you are passing are what the function expects. printf style functions can't have compile-time type saftey, so you have to be extra careful.


HGE printf function works exactly like printf without HGE.

Quote:
Ok, well, do you have the option to use the standard library for this operation.


Actually I need to use hge for it.

Share this post


Link to post
Share on other sites
Make sure all those Translate functions are returning the valid values.

(If you're using VC++, go to the Immediate window and type them in ala TranslateGender(gender)<ENTER>)

If they are, is your font working properly?

(Can you print a test message with printf? If not, can you with Render?)

Share this post


Link to post
Share on other sites
Everything is returning valid values, however I just took out the last argument in that line, and used Render() to print that little peice of information, and it works fine - I suspect that printf can only print an limited amount of characters...

So now this works:

hge_font->printf(200, 215, "You are a %s, %s %s. You have a %s \n%s\n\n", TranslateGender(gender), TranslatecType(ctype), TranslateRace(race), TranslateDescription(rndClr), TranslateMind(mind));
hge_font->Render(200, 300, TranslateRaceType(race));


[Edited by - Narcis on May 1, 2006 9:37:26 PM]

Share this post


Link to post
Share on other sites
lemme guess, the printf function in hge_font looks something like:


void hge_font::printf(int x, int y, const char* format, ...) {
va_list args;
va_start(args, format);
char buffer[256];
vsprintf(buffer, args);


and it is overflowing the internal buffer. At very least it should be vsnprintf instead so it wouldn't crash.

Share this post


Link to post
Share on other sites
If you're really stuck, do a brute-force debugging and printf each item to the console in separate lines. If one of these lines causes a crash, then your problem lies in a Translate*() function.

If they all work, then something is wrong with your hge_font->printf() function. Maybe hge_font is invalid at that point in the code?

Share this post


Link to post
Share on other sites
Quote:
If they all work, then something is wrong with your hge_font->printf() function. Maybe hge_font is invalid at that point in the code?


Nope, it all works correctly except for the last Translate() in the printf line which returns a huge string (char*).

Quote:
lemme guess, the printf function in hge_font looks something like:

...

and it is overflowing the internal buffer.


... sounds like the best explanation.

Share this post


Link to post
Share on other sites

This topic is 4246 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this