Game doesn't crash if currently printing

Started by
16 comments, last by NicholasLopez 9 years, 9 months ago
Do include stdio.h?

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

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.

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
TWITTER: [twitter]KunosStefano[/twitter]
AssettoCorsa - netKar PRO - Kunos Simulazioni

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

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.

So just remove the clean up part?

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.

ah I get it now

This topic is closed to new replies.

Advertisement