如何清理内存?

你好,我必须写一个游戏,我陷入困境。 对不起,我知道这有点复杂,但是我知道它在哪里 我知道内存泄漏在哪里,但我无法解决。首先是主文件

main.c

int main() {
    struct room* home1 = create_room("start", "Nachadzas sa v chyzi svarneho suhaja. Na vychode sa nachadzaju dvere veduce z chyze von.");
    struct item *item2 = create_item("item", "description", 8);
    add_item_to_room(home1, item2);
    destroy_room(home1);
    return 0;
}

so you can see i create a room, a create also an item, i put it in the room, but when i destroy, i want to also to delete the item. but the main thing that i want to implement destroying that item in the function destroy_room so there is my code and also the h file

h文件

enum properties{
    NONE        = 0,    // no extra properties
    MOVABLE     = 1,    // item can be carried in the backpack
    USABLE      = 2,    // item can be used
    EXAMINABLE  = 4,    // item can be examined
    OPENABLE    = 8     // item can be opened or closed
};

struct item{
    char*         name;         /// item name
    char*         description;  /// item description
    unsigned int  properties;   /// item properties
};

struct room {
    char* name;                 /// name of the room/location
    char* description;          /// description of the room/location
    struct room *north;         /// reference to the room on north
    struct room *south;         /// reference to the room on south
    struct room *east;          /// reference to the room on east
    struct room *west;          /// reference to the room on west
    struct container* items;    /// list of items in the room/location
};

enum container_type {
    ROOM, ITEM, COMMAND, TEXT
};

struct container {
    enum container_type type;       /// type of container
    union{
        struct room*    room;       /// pointer to room
        struct item*    item;       /// pointer to item
        struct command* command;    /// pointer to command
        char*           text;       /// pointer to text
    };
    struct container* next;         /// pointer to next data container in list
};

功能列表我在这里使用的是什么

struct room* create_room(char *name, char *description) {
    if (name == NULL || description == NULL || strlen(name) < 1 || strlen(description) < 1) {
        return NULL;
    }

    struct room* new_room = (struct room*)malloc(sizeof(struct room));

    new_room->name = malloc(sizeof(char)*strlen(name)+1);
    strcpy(new_room->name, name);
    new_room->description = malloc(sizeof(char)*strlen(description)+1);
    strcpy(new_room->description, description);

    // tisztists ki minden agat bolond

    new_room->north = NULL;
    new_room->south = NULL;
    new_room->east = NULL;
    new_room->west = NULL;
    new_room->items = NULL;

    return new_room;
}

struct room* destroy_room(struct room* room) {
    if (room != NULL) {
        if (room->description != NULL) {
            free(room->description);
            room->description = NULL;
        }
        if (room->name != NULL) {
            free(room->name);
            room->name = NULL;
        }
        free(room);
        room = NULL;
    }

    return NULL;
}

void add_item_to_room(struct room* room, struct item* item) {
    if (room != NULL && item != NULL) {
        if (room->items == NULL) {
            room->items = create_container(room->items, ROOM, item);
        }
        else {
            create_container(room->items, ROOM, item);
        }
    }
}

struct container* create_container(struct container* first, enum container_type type, void* entry) {
    if (entry == NULL) {
        return NULL;
    } else {
        if (first == NULL) {
            struct container *containerNew = calloc(1, sizeof(struct container));
            containerNew->type = type;
            switch (type) {
                case ROOM:
                    containerNew->room = entry;
                    break;
                case ITEM:
                    containerNew->item = entry;
                    break;
                case TEXT:
                    containerNew->text = entry;
                    break;
            }
            return containerNew;
        } else {
            if (type != first->type) {
                return NULL;
            } else {
                struct container *containerNew = calloc(1, sizeof(struct container));
                char *textNew;
                containerNew->type = type;
                switch (type) {
                    case ROOM:
                        containerNew->room = entry;
                        break;
                    case ITEM:
                        containerNew->item = entry;
                        break;
                    case TEXT:
                        textNew = calloc(strlen(entry) + 1000, sizeof(char));
                        memcpy(textNew, entry, strlen(entry)*sizeof(char));
                        free(entry);
                        containerNew->text = textNew;
                        break;
                }
                while (first->next != NULL) {
                    first = first->next;
                }
                first->next = containerNew;
                return containerNew;
            }
        }
    }
}

struct item* create_item(char* name, char* description, unsigned int properties) {
    if (name == NULL || description == NULL || strlen(name) < 1 || strlen(description) < 1) {
        return NULL;
    }
    struct item* new_item = calloc(1,sizeof(struct item));
    char *new_name = calloc(strlen(name)+1, sizeof(char));
    strcpy(new_name, name);

    char *new_description = calloc(strlen(description) + 1, sizeof(char));
    strcpy(new_description, description);
    new_item->name = new_name;
    new_item->description = new_description;
    new_item->properties = properties;

    return new_item;
}

内存检查

==12971== HEAP SUMMARY:
==12971==     in use at exit: 65 bytes in 4 blocks
==12971==   total heap usage: 7 allocs, 3 frees, 216 bytes allocated
==12971== 
==12971== LEAK SUMMARY:
==12971==    definitely lost: 24 bytes in 1 blocks
==12971==    indirectly lost: 41 bytes in 3 blocks
==12971==      possibly lost: 0 bytes in 0 blocks
==12971==    still reachable: 0 bytes in 0 blocks
==12971==         suppressed: 0 bytes in 0 blocks
==12971== Rerun with --leak-check=full to see details of leaked memory
评论