内存读取无效 - Valgrind

时间:2014-10-04 11:14:23

标签: c linux memory-management valgrind

我现在正在创建一个2D游戏,但为了避免所有的图形和声音开销(我打算使用allegro来处理,如果这有帮助),我正在将它原型化为命令行游戏

代码按预期工作,到目前为止没有任何问题。即使我使用valgrind来检查是否没有检测到内存泄漏,我发现没有泄漏是可能的作为valgrind状态。
但是,我看到一些行声明valgrind检测到无效的大小为8的读取 ..这是我以前没有遇到的问题,最终,不知道如何修复。

这是测试代码

的示例
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define MAX_X_GRID (3)
#define MAX_Y_GRID (3)
#define MAX_NUM ((int)(MAX_X_GRID * MAX_Y_GRID))

typedef struct
{
    int x;
    int y;
} Location;

typedef struct
{
    char * description;
    char * name;
    Location * location;
} Room;

Room *allrooms[MAX_NUM];
int num;

Room *Room_get_at_location(Location locs)
{ /* Get the room at location locs */
    Room *r = NULL;
    for ( int i = 0; i < MAX_NUM; ++i ) {
        r = allrooms[i];
        if ((r->location->x == locs.x) && (r->location->y == locs.y))
            return r;
    }
    return NULL;
}

void Room_destroy(Room *room)
{ /* Free Room memory */
    free(room->description);
    free(room->name);
    free(room->location);
    free(room);
}

void free_allrooms()
{ /* Destroy all rooms */
    Room *r;
    for(int x = 1; x <= MAX_X_GRID; ++x)
        for(int y = 1; y <= MAX_Y_GRID; ++y)
            if ((r = Room_get_at_location((Location){x, y})) != NULL)
                Room_destroy(r);
    num = 0;
}

Room *Room_create(char *name, char *desc, Location locs)
{ /* Creates a new room */
    if (num == MAX_NUM)
        return NULL;
    Room *r = malloc(sizeof(Room));
    r->description = strdup(desc);
    r->name = strdup(name);
    r->location = malloc(sizeof(Location));
    r->location->x = locs.x;
    r->location->y = locs.y;
    allrooms[num++] = r;
    return r;
}

int main(int argc, char const *argv[])
{
    num = 0;

    Room *r;
    for(int x = 1; x <= MAX_X_GRID; ++x) {
        for (int y = 1; y <= MAX_Y_GRID; ++y) {
            r = Room_create("Main Hall", "Gee, that's the Main Hall .. !", (Location){x, y});
            printf("==========\t%s\t[%d, %d]\t==========\n  %s\n", r->name, r->location->x, r->location->y, r->description);
        }
    }
    free_allrooms();
    return 0;
}

..这是valgrind报告

==6137== Memcheck, a memory error detector
==6137== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==6137== Using Valgrind-3.10.0 and LibVEX; rerun with -h for copyright info
==6137== Command: ./Main
==6137== 
==6137== Invalid read of size 8
==6137==    at 0x400604: Room_get_at_location (Main.c:30)
==6137==    by 0x4006B4: free_allrooms (Main.c:49)
==6137==    by 0x40084A: main (Main.c:79)
==6137==  Address 0x51d9050 is 16 bytes inside a block of size 24 free'd
==6137==    at 0x4C2B200: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6137==    by 0x400680: Room_destroy (Main.c:41)
==6137==    by 0x4006CB: free_allrooms (Main.c:50)
==6137==    by 0x40084A: main (Main.c:79)
==6137== 
==6137== Invalid read of size 4
==6137==    at 0x400608: Room_get_at_location (Main.c:30)
==6137==    by 0x4006B4: free_allrooms (Main.c:49)
==6137==    by 0x40084A: main (Main.c:79)
==6137==  Address 0x51d9150 is 0 bytes inside a block of size 8 free'd
==6137==    at 0x4C2B200: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6137==    by 0x400674: Room_destroy (Main.c:40)
==6137==    by 0x4006CB: free_allrooms (Main.c:50)
==6137==    by 0x40084A: main (Main.c:79)
==6137== 
==6137== Invalid read of size 8
==6137==    at 0x400615: Room_get_at_location (Main.c:30)
==6137==    by 0x4006B4: free_allrooms (Main.c:49)
==6137==    by 0x40084A: main (Main.c:79)
==6137==  Address 0x51d9050 is 16 bytes inside a block of size 24 free'd
==6137==    at 0x4C2B200: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6137==    by 0x400680: Room_destroy (Main.c:41)
==6137==    by 0x4006CB: free_allrooms (Main.c:50)
==6137==    by 0x40084A: main (Main.c:79)
==6137== 
==6137== Invalid read of size 4
==6137==    at 0x400619: Room_get_at_location (Main.c:30)
==6137==    by 0x4006B4: free_allrooms (Main.c:49)
==6137==    by 0x40084A: main (Main.c:79)
==6137==  Address 0x51d9154 is 4 bytes inside a block of size 8 free'd
==6137==    at 0x4C2B200: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6137==    by 0x400674: Room_destroy (Main.c:40)
==6137==    by 0x4006CB: free_allrooms (Main.c:50)
==6137==    by 0x40084A: main (Main.c:79)
==6137== 
==========  Main Hall   [1, 1]  ==========
  Gee, that's the Main Hall .. !
==========  Main Hall   [1, 2]  ==========
  Gee, that's the Main Hall .. !
==========  Main Hall   [1, 3]  ==========
  Gee, that's the Main Hall .. !
==========  Main Hall   [2, 1]  ==========
  Gee, that's the Main Hall .. !
==========  Main Hall   [2, 2]  ==========
  Gee, that's the Main Hall .. !
==========  Main Hall   [2, 3]  ==========
  Gee, that's the Main Hall .. !
==========  Main Hall   [3, 1]  ==========
  Gee, that's the Main Hall .. !
==========  Main Hall   [3, 2]  ==========
  Gee, that's the Main Hall .. !
==========  Main Hall   [3, 3]  ==========
  Gee, that's the Main Hall .. !
==6137== 
==6137== HEAP SUMMARY:
==6137==     in use at exit: 0 bytes in 0 blocks
==6137==   total heap usage: 36 allocs, 36 frees, 657 bytes allocated
==6137== 
==6137== All heap blocks were freed -- no leaks are possible
==6137== 
==6137== For counts of detected and suppressed errors, rerun with: -v
==6137== ERROR SUMMARY: 90 errors from 4 contexts (suppressed: 0 from 0)

我做错了什么?

1 个答案:

答案 0 :(得分:1)

原因:

函数Room_get_at_location存在问题。当您逐个释放房间时,有可能在调用Room_get_at_location的某个时刻,某些房间已经被释放。在释放的条目上保留r = allrooms[i]的测试字段会产生未定义的行为。

解决方案:

在释放它之后将NULL分配给数组allrooms中的条目(更改Room_get_at_location指向指针的返回值),然后在{{1}中跳过它们}:

Room_get_at_location

EDITED:

我已根据上述解决方案编辑了代码,这次Room *Room_get_at_location(Location locs) { /* Get the room at location locs */ Room *r = NULL; for ( int i = 0; i < MAX_NUM; ++i ) { r = allrooms[i]; if (!r) continue; if ((r->location->x == locs.x) && (r->location->y == locs.y)) return r; } return NULL; } 没有抱怨。

编辑后的代码如下。请注意这只是用于测试,我认为这个修复看起来很难看。你绝对可以重新设计它。

valgrind

结果:

#define _POSIX_C_SOURCE 201401L
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define MAX_X_GRID (3)
#define MAX_Y_GRID (3)
#define MAX_NUM ((int)(MAX_X_GRID * MAX_Y_GRID))

typedef struct
{
    int x;
    int y;
} Location;

typedef struct
{
    char * description;
    char * name;
    Location * location;
} Room;

Room *allrooms[MAX_NUM];
int num;

Room **Room_get_at_location(Location locs)
{ /* Get the room at location locs */
    Room **r = NULL;
    for ( int i = 0; i < MAX_NUM; ++i ) {
        r = &allrooms[i];
        if (!*r)
            continue;
        if (((*r)->location->x == locs.x) && ((*r)->location->y == locs.y))
            return r;
    }
    return NULL;
}

void Room_destroy(Room *room)
{ /* Free Room memory */
    free(room->description);
    free(room->name);
    free(room->location);
    free(room);
}

void free_allrooms()
{ /* Destroy all rooms */
    Room **r;
    for(int x = 1; x <= MAX_X_GRID; ++x)
        for(int y = 1; y <= MAX_Y_GRID; ++y)
        {
            r = Room_get_at_location((Location){x, y});
            if (*r)
            {
                Room_destroy(*r);
                *r = NULL;
            }
        }
    num = 0;
}

Room *Room_create(char *name, char *desc, Location locs)
{ /* Creates a new room */
    if (num == MAX_NUM)
        return NULL;
    Room *r = malloc(sizeof(Room));
    r->description = strdup(desc);
    r->name = strdup(name);
    r->location = malloc(sizeof(Location));
    r->location->x = locs.x;
    r->location->y = locs.y;
    allrooms[num++] = r;
    return r;
}

int main(int argc, char const *argv[])
{
    num = 0;

    Room *r;
    for(int x = 1; x <= MAX_X_GRID; ++x) {
        for (int y = 1; y <= MAX_Y_GRID; ++y) {
            r = Room_create("Main Hall", "Gee, that's the Main Hall .. !", (Location){x, y});
            printf("==========\t%s\t[%d, %d]\t==========\n  %s\n", r->name, r->location->x, r->location->y, r->description);
        }
    }
    free_allrooms();
    return 0;
}
相关问题