• Advertisement
Sign in to follow this  

Game doesn't crash if currently printing

This topic is 1316 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 am a little confused as to why this happens in what I have been working on.

if( game_debug == true ) { 
	render_var( 16, 184, TEXT_LEFT, "FPS:", (int)framespersecond, true );
	render_var( 16, 16, TEXT_LEFT, "CAMERA X:", (int)camera_x, true );
	render_var( 16, 32, TEXT_LEFT, "CAMERA Y:", (int)camera_y, true );
	if( lvl_name != NULL ) { 
		render_text( 320 - 16, 16, TEXT_RIGHT, "LEVEL: ", true );
		render_text( 320 - 16, 32, TEXT_RIGHT, lvl_name, true );
	} 
} else { 
	render_text( 320 - 16, 16, TEXT_RIGHT, "F1: View Debug Info", true );
} 

when I call upon change_level

int change_level( const char* filename ) { 
/* clear any current level assets */
	/* level information */
	lvl_name = "";
	...
	/* camera coordinates */
	camera_x = 0;
	camera_y = 0;
	/* tiles */
	...
	/* objects */
/* parse new level */
	parse_level( filename );
/* tell user level has been changed */
	printf( "level has been changed to: %s\n", lvl_name );
} 

while game_debug is currently true, the game will crash. However, if I comment out the first three parts (rendering the framespersecond, camera_x, and camera_y) then the crash does not occur. The crash does not occur either if I put game_debug = false; somewhere within the change_level() function. Another weird thing (this is where the thread title comes in) is the game does not crash if I am currently printing something over and over again, so I could put printf("printing!\n"); above the line of code that renders framespersecond, and when I change levels (game_debug is not set to false in this instance), the crash does not occur.

 

Do you have any ideas as to why this anomaly happens? Thanks

Share this post


Link to post
Share on other sites
Advertisement

Don't know what you mean by crash-hunting, I listed some possible scenarios where crashing may/may not occur. If I don't have debug info toggle-able, and everything is just already out there, then the game doesn't crash either. I see if game_debug is true, the game crashes AFTER change_level is done. However say everything is commented out inside game_debug, then there is no crash at all. So it has to do something with rendering the text on screen, but nothing printed is erased when the level changes. So it's very confusing.

Share this post


Link to post
Share on other sites

You strongly imply that the three render_var lines appear to cause the "crash." In those lines, the only arguments that would seem to be a problem are framespersecond, camera_x and camera_y. As a wild guess, assuming those are float variables, have you tried converting them to int variables, and using the int variables as the render_var arguments?

 

I.e.,

int fps = (int)framespersecond;
render_var(..., fps,..);

EDIT: What does render_var do?

Edited by Buckeye

Share this post


Link to post
Share on other sites
int render_text( int x, int y, int allignment, char* text, int outline ) { 
	int i;
	int text_length = strlen(text) * text_w;
	int o_x, o_y;
	for( i = 1; i < strlen(text) + 1; i++ ) { 
		double t_x = x + (text_w * i);
		if( allignment == TEXT_LEFT )
			t_x = x + (text_w * i) - text_w;
		if( allignment == TEXT_CENTRE )
			t_x = (x + (text_w * i)) - text_length/2 - text_w;
		if( allignment == TEXT_RIGHT )
			t_x = (x + (text_w * i)) - text_length - text_w;
		if( outline == true )
			for( o_x = -1; o_x <= 1; o_x++ )
				for( o_y = -1; o_y <= 1; o_y++ )
					draw_letter( t_x + o_x, y + o_y, text[i - 1], true );
		draw_letter( t_x, y, text[i - 1], false );
	} 
	free(text);
} 
int render_var( int x, int y, int allignment, char *text, int var, int outline ) { 
	render_text( x, y, allignment, text, outline );
	int var_spot = 0;
	if( strlen(text) != 0 )
		var_spot = (strlen(text))*text_w;	
	char var_text[20];
	sprintf( var_text, "%d", var );
	render_text( x + var_spot, y, allignment, var_text, outline );
} 

Share this post


Link to post
Share on other sites

Couldn't add more text after I made that code bubble. They are converted to integers, as you can see, they render just fine. It's just weird that the game only crashes if game_debug is true and code for rendering text (is active) is inside the statement; if it's outside the game_debug statement, no crash; or if I am constantly printing anything inside the statement, no crash.

Share this post


Link to post
Share on other sites

I am just confused with this crashed, however I moved the function to another line (one line above it's comment) and now (for now) it works. I am baffled by this.

Share this post


Link to post
Share on other sites

Don't know what you mean by crash-hunting, I listed some possible scenarios where crashing may/may not occur. If I don't have debug info toggle-able, and everything is just already out there, then the game doesn't crash either. I see if game_debug is true, the game crashes AFTER change_level is done. However say everything is commented out inside game_debug, then there is no crash at all. So it has to do something with rendering the text on screen, but nothing printed is erased when the level changes. So it's very confusing.


It *might* have something to do with that. It might not. That is what debuggers were invented for.

I asked what you had done for crash hunting for the additional information linked to by ApochPiQ. For example, when the crash occurs in the debugger, where does the crash occur? Does it appear inside a function you know, inside a system function, or in some seemingly random memory location? What does your call stack look like? What was your previous instruction?

Crashes typically include some sort of message indicating the error, or provide a minidump or other useful data. If an error message what exactly does it say? If a minidump what happens when you load it up with your debug info? Those random numbers mean things, and the guide linked to above can help you understand them. (Or we could retype the information hundreds of times, doing it yet again for your post. Please just go read the other links.)

Other than uncommenting some lines and moving a bit of code around to hide the bug, what have you actually done (you know, like a computer scientist rather than code monkey) to experiment on the issue, identify it, and correct it?

Share this post


Link to post
Share on other sites

I am not sure if there is a bug, I just changed the order of some things and it worked. Maybe it was an order issue. Console did not give any error messages, even when I had a friend look at it and supply me a document of the console of what he ran.

Share this post


Link to post
Share on other sites
Do include stdio.h?

The printf-functions have a variable parameter list. Often make problems if no prototype available.

Share this post


Link to post
Share on other sites

I agree with LennyLen.. you HAVE a bug, and you are lucky enough to have found a way to reproduce it reliably. Moving things around will just hide the bug..it'll be back, on some of your users' PC and you will have no way to fix it. Fix it NOW that you can see it.

Share this post


Link to post
Share on other sites

As mentioned by LennyLen you're freeing memory in render_text (the line: free(text);) that was never allocated with malloc or new.  When you call a function with a literal string (example: function("hello")), it does not need to be freed.  Also the function render_var calls render_text which frees the text string and then render_var tries to operate on that text afterwards with strlen(text). These things can lead to a crash.

Share this post


Link to post
Share on other sites
Well, it's a bit more complicated than that:

void thing(const char* text) { 
    // Do stuff with text

    free(text);
}

int main() {
    const char* a = "abc";
    const char b[] = "def"
    char* c = malloc(4);
    char* d = new char[4];

    sprintf(c,"ghi");
    sprintf(d,"jkl");
   
    thing("mno"); // No!
    thing(a);     // No!
    thing(b);     // No!
    thing(c);     // Okay
    thing(d);     // No!

    // free(c);
    // delete[] d;
}
Here, thing is assuming ownership of text which isn't always the desired behavior, as shown in the sample program here.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement