Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Found memory bug #7

Open
JimmyJames707 opened this issue Jun 22, 2019 · 0 comments
Open

Found memory bug #7

JimmyJames707 opened this issue Jun 22, 2019 · 0 comments

Comments

@JimmyJames707
Copy link

I found it in the project-restructure branch, so it is probably in the master as well. The list_remove_element_with_data() function should be checked for errors where it is used in other places as well.

I've labelled them BUG 1a) and BUG 1b) in game.c:

void item_lifetime_update() {
        ListElement *e = list_head(carriedItems);
        while (e != NULL) {
                GameObject *go = (GameObject *)list_data(e);
                Equipment *eq = game_object_get_component(go, COMP_EQUIPMENT);
                eq->lifetime -= 1;

                // Check to see if the item is aged beyond use
                if (eq->lifetime <= 0) {
                        // Unequip the item if equipped
                        bool wasEquipped = false;
                        if (eq->isEquipped) {
                                wasEquipped = true;
                                item_toggle_equip(go);
                        }

                        // Remove from carried items
                        // BUG 1a) list_remove_element_with_data: the ListElement removed here is deleted
                        list_remove_element_with_data(carriedItems, go);
                        
                        // Display a message to the player
                        Visibility *v = (Visibility *)game_object_get_component(go, COMP_VISIBILITY);
                        char *msg;
                        if (wasEquipped) {
                                msg = String_Create("The %s crumbles in your hands.", v->name);
                        } else {
                                msg = String_Create("The %s you are carrying crumbles to dust.", v->name);
                        }
                        add_message(msg, 0x990000ff);
                        String_Destroy(msg);
                        
                        game_object_destroy(go);
                }

                // BUG 1b) list_remove_element_with_data: the deleted ListElement is used again here and is corrupt
                e = list_next(e);
        }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant