Jump to content

  • Log In with Google      Sign In   
  • Create Account


Game doesn't crash if currently printing

  • You cannot reply to this topic
17 replies to this topic

#1 DrNicholas   Members   -  Reputation: 128

Like
0Likes
Like

Posted 13 July 2014 - 06:34 PM

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



Sponsor:

#2 frob   Moderators   -  Reputation: 19602

Like
5Likes
Like

Posted 13 July 2014 - 06:45 PM

Could be a huge number of things. Memory corruption, race conditions, and improper shared state all immediately jump to mind. In your code snippet null pointers and uninitialized values also seem likely.


"Crash" is usually a good thing, a hard stop that triggers the OS to stop the program. Trap it in a debugger, or review the crash dump, and collect all the useful bits of information at the time of the crash.

What kind of crash-hunting techniques have you tried so far?
Check out my personal indie blog at bryanwagstaff.com.

#3 DrNicholas   Members   -  Reputation: 128

Like
0Likes
Like

Posted 13 July 2014 - 08:07 PM

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.



#4 Buckeye   Crossbones+   -  Reputation: 3990

Like
4Likes
Like

Posted 13 July 2014 - 08:08 PM

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, 13 July 2014 - 08:13 PM.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.


#5 DrNicholas   Members   -  Reputation: 128

Like
0Likes
Like

Posted 13 July 2014 - 08:52 PM

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 );
} 


#6 DrNicholas   Members   -  Reputation: 128

Like
0Likes
Like

Posted 13 July 2014 - 08:58 PM

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.



#7 ApochPiQ   Moderators   -  Reputation: 14617

Like
4Likes
Like

Posted 13 July 2014 - 09:08 PM

Mandatory reading.

#8 DrNicholas   Members   -  Reputation: 128

Like
0Likes
Like

Posted 13 July 2014 - 10:07 PM

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.



#9 frob   Moderators   -  Reputation: 19602

Like
1Likes
Like

Posted 13 July 2014 - 10:39 PM

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?
Check out my personal indie blog at bryanwagstaff.com.

#10 DrNicholas   Members   -  Reputation: 128

Like
0Likes
Like

Posted 13 July 2014 - 11:51 PM

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.



#11 Tribad   Members   -  Reputation: 826

Like
0Likes
Like

Posted 14 July 2014 - 12:01 AM

Do include stdio.h?

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

#12 LennyLen   Crossbones+   -  Reputation: 3528

Like
7Likes
Like

Posted 14 July 2014 - 12:13 AM

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.

 

It is a bug.  Changing the order may have made it appear to go away for now, but it could pop back up later if you add more code or change existing code.  

 

Change the code back so that the crash is occurring and debug the code properly. In other words, use the debugger. That's what it's for.  

 

edit: One odd thing I just noticed in your code is that you are freeing memory in your render_text function that you are not allocating yourself, since you are passing literal strings to the function.


Edited by LennyLen, 14 July 2014 - 12:18 AM.


#13 kunos   Crossbones+   -  Reputation: 2203

Like
3Likes
Like

Posted 14 July 2014 - 05:22 AM

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.


Stefano Casillo
Lead Programmer
TWITTER: @KunosStefano
AssettoCorsa - netKar PRO - Kunos Simulazioni

#14 DrNicholas   Members   -  Reputation: 128

Like
0Likes
Like

Posted 14 July 2014 - 04:27 PM

In response to LennyLen, could you show me what you notice or how I might fix it?



#15 megadan   Members   -  Reputation: 328

Like
1Likes
Like

Posted 14 July 2014 - 04:49 PM

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.



#16 DrNicholas   Members   -  Reputation: 128

Like
0Likes
Like

Posted 14 July 2014 - 05:25 PM

So just remove the clean up part?



#17 fastcall22   Crossbones+   -  Reputation: 4108

Like
1Likes
Like

Posted 14 July 2014 - 05:31 PM

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.
c3RhdGljIGNoYXIgeW91cl9tb21bMVVMTCA8PCA2NF07CnNwcmludGYoeW91cl9tb20sICJpcyBmYXQiKTs=

#18 DrNicholas   Members   -  Reputation: 128

Like
0Likes
Like

Posted 14 July 2014 - 09:36 PM

ah I get it now







PARTNERS