3

我有这个代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <sys/resource.h>

double get_time()
{
    struct timeval t;
    struct timezone tzp;
    gettimeofday(&t, &tzp);
    return t.tv_sec + t.tv_usec*1e-6;
}

#define LL_APPEND(what, where, type) { \
    what->next = NULL; \
    what->prev = NULL; \
    if(!where) { \
        where = what; \
    } else { \
        type current = where; \
        while(current->next != NULL) \
            current = current->next; \
        current->next = what; \
        what->prev = current; \
    } \
}

struct edge {
    struct node *from;
    struct node *to;
    char *label;
};

struct edge_list {
    struct edge *edge;
    struct edge_list *prev;
    struct edge_list *next;
};

struct graph {
    struct node_list *nodes;
    struct edge_list *edges;
    int nodes_count;
    int edges_count;
};

struct node {
    char *name;
    struct edge_list *in;
    struct edge_list *out;
    struct graph *graph;
};

struct node_list {
    struct node *node;
    struct node_list *prev;
    struct node_list *next;
};

struct graph *graph_create_empty() {
    struct graph *graph = malloc(sizeof(struct graph));

    graph->nodes = NULL;
    graph->edges = NULL;
    graph->nodes_count = 0;
    graph->edges_count = 0;

    return graph;
}


void edge_destroy(struct edge *edge) {
    free(edge->label);
    free(edge);
}


void node_destroy(struct node *node) {
    struct edge_list *tmp_edge, *edge;

    edge = node->in;
    while(edge != NULL) {
        tmp_edge = edge;
        edge     = edge->next;
        free(tmp_edge);
    }

    edge = node->out;
    while(edge != NULL) {
        tmp_edge = edge;
        edge     = edge->next;
        free(tmp_edge);
    }

    free(node->name);
    free(node);
}

void graph_destroy(struct graph *graph) {
    struct edge_list *tmp_edge, *edge;
    edge = graph->edges;
    while(edge != NULL) {
        tmp_edge = edge;
        edge     = edge->next;
        edge_destroy(tmp_edge->edge);
        free(tmp_edge);
    }

    struct node_list *tmp_node, *node;
    node = graph->nodes;
    while(node != NULL) {
        tmp_node = node;
        node     = node->next;
        node_destroy(tmp_node->node);
        free(tmp_node);
    }

    free(graph);
}


void graph_add_edge(struct graph *graph, struct edge *edge) {
    struct edge_list *new_edge = malloc(sizeof(struct edge_list));
    new_edge->edge = edge;

    LL_APPEND(new_edge, graph->edges, struct edge_list *);
    graph->edges_count++;
}

void graph_add_node(struct graph *graph, struct node *node) {
    node->graph = graph;

    struct node_list *new_node = malloc(sizeof(struct node_list));
    new_node->node = node;

    LL_APPEND(new_node, graph->nodes, struct node_list *);
    graph->nodes_count++;
}

struct node *node_create(const char *name, struct graph *graph) {
    struct node *node = malloc(sizeof(struct node));

    node->name = malloc(strlen(name)+1);
    strcpy(node->name, name);

    node->in  = NULL;
    node->out = NULL;

    if(graph)
        graph_add_node(graph, node);

    return node;
}

void node_add_edge(struct node *node, struct edge *edge) {
    struct edge_list *new_edge = malloc(sizeof(struct edge_list));

    if(node == edge->to) {
        LL_APPEND(new_edge, node->out, struct edge_list *);
    } else {
        LL_APPEND(new_edge, node->in, struct edge_list *);
    }
}

void node_connect(const char *label, struct node *from, struct node *to) {
    if(from->graph != to->graph || from->graph == NULL) 
        return;

    struct edge *edge = malloc(sizeof(struct edge));

    edge->label = malloc(strlen(label)+1);
    strcpy(edge->label, label);

    edge->from = from;
    edge->to   = to;

    graph_add_edge(from->graph, edge);
    node_add_edge(from,         edge);
    node_add_edge(to,           edge);
}

int main(int argc, char *argv[]) {
    double start = get_time();

    for(int i = 0; i < 100000; i++) {
        struct graph *graph = graph_create_empty();

        // Create the nodes
        struct node *useful_demand_heat = node_create("useful_demand_heat", graph);
        struct node *useful_demand_elec = node_create("useful_demand_elec", graph);

        struct node *space_heater_coal  = node_create("space_heater_coal", graph);
        struct node *space_heater_gas   = node_create("space_heater_gas", graph);
        struct node *space_heater_oil   = node_create("space_heater_oil", graph);
        struct node *space_heater_chp   = node_create("space_heater_chp", graph);
        struct node *space_heater_elec  = node_create("space_heater_elec", graph);

        struct node *final_demand_coal  = node_create("final_demand_coal", graph);
        struct node *final_demand_gas  = node_create("final_demand_gas", graph);
        struct node *final_demand_oil  = node_create("final_demand_oil", graph);
        struct node *final_demand_elec  = node_create("final_demand_elec", graph);

        struct node *lv_network = node_create("lv_network", graph);
        struct node *mv_network = node_create("mv_network", graph);
        struct node *hv_network = node_create("hv_network", graph);

        struct node *coal_plant = node_create("coal_plant", graph);
        struct node *elec_import = node_create("elec_import", graph);

        // Edges
        node_connect("electricity", elec_import, hv_network);
        node_connect("electricity", coal_plant, hv_network);

        node_connect("electricity", hv_network, mv_network);
        node_connect("electricity", mv_network, lv_network);
        node_connect("electricity", lv_network, final_demand_elec);

        node_connect("electricity", final_demand_elec, space_heater_elec);
        node_connect("coal", final_demand_coal, space_heater_coal);
        node_connect("gas", final_demand_gas, space_heater_gas);
        node_connect("gas", final_demand_gas, space_heater_chp);
        node_connect("oil", final_demand_oil, space_heater_oil);

        node_connect("heat", space_heater_coal, useful_demand_heat);
        node_connect("heat", space_heater_gas, useful_demand_heat);
        node_connect("heat", space_heater_oil, useful_demand_heat);
        node_connect("heat", space_heater_chp, useful_demand_heat);

        node_connect("electricity", space_heater_chp, useful_demand_elec);
        node_connect("electricity", space_heater_elec, useful_demand_elec);
        graph_destroy(graph);
    }

    double finish = get_time();
    printf("Took %.5f ms to run\n", (finish - start));
}

当我对它运行 valgrind 时,我收到以下警告:

==44508== Memcheck, a memory error detector
==44508== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==44508== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==44508== Command: ./graph
==44508== 
==44508== WARNING: Support on MacOS 10.8 is experimental and mostly broken.
==44508== WARNING: Expect incorrect results, assertions and crashes.
==44508== WARNING: In particular, Memcheck on 32-bit programs will fail to
==44508== WARNING: detect any errors associated with heap-allocated data.
==44508== 
--44508-- ./graph:
--44508-- dSYM directory is missing; consider using --dsymutil=yes
Took 22.74883 ms to run
Done.
==44508== 
==44508== HEAP SUMMARY:
==44508==     in use at exit: 74,666 bytes in 365 blocks
==44508==   total heap usage: 12,900,518 allocs, 12,900,153 frees, 284,078,620 bytes allocated
==44508== 
==44508== 16 bytes in 1 blocks are definitely lost in loss record 7 of 88
==44508==    at 0x54D7: malloc_zone_malloc (in /usr/local/Cellar/valgrind/3.8.1/lib/valgrind/vgpreload_memcheck-amd64-darwin.so)
==44508==    by 0x373381: recursive_mutex_init (in /usr/lib/libobjc.A.dylib)
==44508==    by 0x372025: _objc_init (in /usr/lib/libobjc.A.dylib)
==44508==    by 0xBB27: libSystem_initializer (in /usr/lib/libSystem.B.dylib)
==44508==    by 0x7FFF5FC13377: ImageLoaderMachO::doModInitFunctions(ImageLoader::LinkContext const&) (in /usr/lib/dyld)
==44508==    by 0x7FFF5FC13761: ImageLoaderMachO::doInitialization(ImageLoader::LinkContext const&) (in /usr/lib/dyld)
==44508==    by 0x7FFF5FC1006D: ImageLoader::recursiveInitialization(ImageLoader::LinkContext const&, unsigned int, ImageLoader::InitializerTimingList&) (in /usr/lib/dyld)
==44508==    by 0x7FFF5FC0FFC3: ImageLoader::recursiveInitialization(ImageLoader::LinkContext const&, unsigned int, ImageLoader::InitializerTimingList&) (in /usr/lib/dyld)
==44508==    by 0x7FFF5FC0FEB9: ImageLoader::runInitializers(ImageLoader::LinkContext const&, ImageLoader::InitializerTimingList&) (in /usr/lib/dyld)
==44508==    by 0x7FFF5FC01F9D: dyld::initializeMainExecutable() (in /usr/lib/dyld)
==44508==    by 0x7FFF5FC05B03: dyld::_main(macho_header const*, unsigned long, int, char const**, char const**, char const**, unsigned long*) (in /usr/lib/dyld)
==44508==    by 0x7FFF5FC01396: dyldbootstrap::start(macho_header const*, int, char const**, long, macho_header const*, unsigned long*) (in /usr/lib/dyld)
==44508== 
==44508== 32 bytes in 1 blocks are possibly lost in loss record 24 of 88
==44508==    at 0x5A95: malloc_zone_calloc (in /usr/local/Cellar/valgrind/3.8.1/lib/valgrind/vgpreload_memcheck-amd64-darwin.so)
==44508==    by 0x3756EA: NXHashInsert (in /usr/lib/libobjc.A.dylib)
==44508==    by 0x37546C: NXCreateHashTableFromZone (in /usr/lib/libobjc.A.dylib)
==44508==    by 0x374788: _read_images (in /usr/lib/libobjc.A.dylib)
==44508==    by 0x3739EB: map_images_nolock (in /usr/lib/libobjc.A.dylib)
==44508==    by 0x3734F3: map_images (in /usr/lib/libobjc.A.dylib)
==44508==    by 0x7FFF5FC04936: dyld::notifyBatchPartial(dyld_image_states, bool, char const* (*)(dyld_image_states, unsigned int, dyld_image_info const*)) (in /usr/lib/dyld)
==44508==    by 0x7FFF5FC0467C: dyld::registerImageStateBatchChangeHandler(dyld_image_states, char const* (*)(dyld_image_states, unsigned int, dyld_image_info const*)) (in /usr/lib/dyld)
==44508==    by 0x83ED9: dyld_register_image_state_change_handler (in /usr/lib/system/libdyld.dylib)
==44508==    by 0x37204C: _objc_init (in /usr/lib/libobjc.A.dylib)
==44508==    by 0xBB27: libSystem_initializer (in /usr/lib/libSystem.B.dylib)
==44508==    by 0x7FFF5FC13377: ImageLoaderMachO::doModInitFunctions(ImageLoader::LinkContext const&) (in /usr/lib/dyld)
==44508== 
==44508== 64 bytes in 2 blocks are possibly lost in loss record 36 of 88
==44508==    at 0x5A95: malloc_zone_calloc (in /usr/local/Cellar/valgrind/3.8.1/lib/valgrind/vgpreload_memcheck-amd64-darwin.so)
==44508==    by 0x3756EA: NXHashInsert (in /usr/lib/libobjc.A.dylib)
==44508==    by 0x385324: realizeClass(class_t*) (in /usr/lib/libobjc.A.dylib)
==44508==    by 0x374E9C: _read_images (in /usr/lib/libobjc.A.dylib)
==44508==    by 0x3739EB: map_images_nolock (in /usr/lib/libobjc.A.dylib)
==44508==    by 0x3734F3: map_images (in /usr/lib/libobjc.A.dylib)
==44508==    by 0x7FFF5FC04936: dyld::notifyBatchPartial(dyld_image_states, bool, char const* (*)(dyld_image_states, unsigned int, dyld_image_info const*)) (in /usr/lib/dyld)
==44508==    by 0x7FFF5FC0467C: dyld::registerImageStateBatchChangeHandler(dyld_image_states, char const* (*)(dyld_image_states, unsigned int, dyld_image_info const*)) (in /usr/lib/dyld)
==44508==    by 0x83ED9: dyld_register_image_state_change_handler (in /usr/lib/system/libdyld.dylib)
==44508==    by 0x37204C: _objc_init (in /usr/lib/libobjc.A.dylib)
==44508==    by 0xBB27: libSystem_initializer (in /usr/lib/libSystem.B.dylib)
==44508==    by 0x7FFF5FC13377: ImageLoaderMachO::doModInitFunctions(ImageLoader::LinkContext const&) (in /usr/lib/dyld)
==44508== 
==44508== 64 bytes in 2 blocks are possibly lost in loss record 37 of 88
==44508==    at 0x5A95: malloc_zone_calloc (in /usr/local/Cellar/valgrind/3.8.1/lib/valgrind/vgpreload_memcheck-amd64-darwin.so)
==44508==    by 0x3756EA: NXHashInsert (in /usr/lib/libobjc.A.dylib)
==44508==    by 0x3757F6: _NXHashRehashToCapacity (in /usr/lib/libobjc.A.dylib)
==44508==    by 0x375748: NXHashInsert (in /usr/lib/libobjc.A.dylib)
==44508==    by 0x385324: realizeClass(class_t*) (in /usr/lib/libobjc.A.dylib)
==44508==    by 0x374E9C: _read_images (in /usr/lib/libobjc.A.dylib)
==44508==    by 0x3739EB: map_images_nolock (in /usr/lib/libobjc.A.dylib)
==44508==    by 0x3734F3: map_images (in /usr/lib/libobjc.A.dylib)
==44508==    by 0x7FFF5FC04936: dyld::notifyBatchPartial(dyld_image_states, bool, char const* (*)(dyld_image_states, unsigned int, dyld_image_info const*)) (in /usr/lib/dyld)
==44508==    by 0x7FFF5FC0467C: dyld::registerImageStateBatchChangeHandler(dyld_image_states, char const* (*)(dyld_image_states, unsigned int, dyld_image_info const*)) (in /usr/lib/dyld)
==44508==    by 0x83ED9: dyld_register_image_state_change_handler (in /usr/lib/system/libdyld.dylib)
==44508==    by 0x37204C: _objc_init (in /usr/lib/libobjc.A.dylib)
==44508== 
==44508== 64 bytes in 1 blocks are definitely lost in loss record 38 of 88
==44508==    at 0x54D7: malloc_zone_malloc (in /usr/local/Cellar/valgrind/3.8.1/lib/valgrind/vgpreload_memcheck-amd64-darwin.so)
==44508==    by 0x3733CF: recursive_mutex_init (in /usr/lib/libobjc.A.dylib)
==44508==    by 0x372025: _objc_init (in /usr/lib/libobjc.A.dylib)
==44508==    by 0xBB27: libSystem_initializer (in /usr/lib/libSystem.B.dylib)
==44508==    by 0x7FFF5FC13377: ImageLoaderMachO::doModInitFunctions(ImageLoader::LinkContext const&) (in /usr/lib/dyld)
==44508==    by 0x7FFF5FC13761: ImageLoaderMachO::doInitialization(ImageLoader::LinkContext const&) (in /usr/lib/dyld)
==44508==    by 0x7FFF5FC1006D: ImageLoader::recursiveInitialization(ImageLoader::LinkContext const&, unsigned int, ImageLoader::InitializerTimingList&) (in /usr/lib/dyld)
==44508==    by 0x7FFF5FC0FFC3: ImageLoader::recursiveInitialization(ImageLoader::LinkContext const&, unsigned int, ImageLoader::InitializerTimingList&) (in /usr/lib/dyld)
==44508==    by 0x7FFF5FC0FEB9: ImageLoader::runInitializers(ImageLoader::LinkContext const&, ImageLoader::InitializerTimingList&) (in /usr/lib/dyld)
==44508==    by 0x7FFF5FC01F9D: dyld::initializeMainExecutable() (in /usr/lib/dyld)
==44508==    by 0x7FFF5FC05B03: dyld::_main(macho_header const*, unsigned long, int, char const**, char const**, char const**, unsigned long*) (in /usr/lib/dyld)
==44508==    by 0x7FFF5FC01396: dyldbootstrap::start(macho_header const*, int, char const**, long, macho_header const*, unsigned long*) (in /usr/lib/dyld)
==44508== 
==44508== 72 (24 direct, 48 indirect) bytes in 1 blocks are definitely lost in loss record 39 of 88
==44508==    at 0x5A95: malloc_zone_calloc (in /usr/local/Cellar/valgrind/3.8.1/lib/valgrind/vgpreload_memcheck-amd64-darwin.so)
==44508==    by 0x3796D2: _objc_fetch_pthread_data (in /usr/lib/libobjc.A.dylib)
==44508==    by 0x382C4D: _fetchInitializingClassList(signed char) (in /usr/lib/libobjc.A.dylib)
==44508==    by 0x379181: _class_initialize (in /usr/lib/libobjc.A.dylib)
==44508==    by 0x379137: _class_initialize (in /usr/lib/libobjc.A.dylib)
==44508==    by 0x379137: _class_initialize (in /usr/lib/libobjc.A.dylib)
==44508==    by 0x379137: _class_initialize (in /usr/lib/libobjc.A.dylib)
==44508==    by 0x3790F2: prepareForMethodLookup (in /usr/lib/libobjc.A.dylib)
==44508==    by 0x378EEE: lookUpMethod (in /usr/lib/libobjc.A.dylib)
==44508==    by 0x3772FB: objc_msgSend (in /usr/lib/libobjc.A.dylib)
==44508==    by 0x2DEE87: _libxpc_initializer (in /usr/lib/system/libxpc.dylib)
==44508==    by 0xBB2C: libSystem_initializer (in /usr/lib/libSystem.B.dylib)
==44508== 
==44508== 96 bytes in 3 blocks are possibly lost in loss record 43 of 88
==44508==    at 0x5A95: malloc_zone_calloc (in /usr/local/Cellar/valgrind/3.8.1/lib/valgrind/vgpreload_memcheck-amd64-darwin.so)
==44508==    by 0x3756EA: NXHashInsert (in /usr/lib/libobjc.A.dylib)
==44508==    by 0x38533D: realizeClass(class_t*) (in /usr/lib/libobjc.A.dylib)
==44508==    by 0x3845D0: realizeClass(class_t*) (in /usr/lib/libobjc.A.dylib)
==44508==    by 0x374E9C: _read_images (in /usr/lib/libobjc.A.dylib)
==44508==    by 0x3739EB: map_images_nolock (in /usr/lib/libobjc.A.dylib)
==44508==    by 0x3734F3: map_images (in /usr/lib/libobjc.A.dylib)
==44508==    by 0x7FFF5FC04936: dyld::notifyBatchPartial(dyld_image_states, bool, char const* (*)(dyld_image_states, unsigned int, dyld_image_info const*)) (in /usr/lib/dyld)
==44508==    by 0x7FFF5FC0467C: dyld::registerImageStateBatchChangeHandler(dyld_image_states, char const* (*)(dyld_image_states, unsigned int, dyld_image_info const*)) (in /usr/lib/dyld)
==44508==    by 0x83ED9: dyld_register_image_state_change_handler (in /usr/lib/system/libdyld.dylib)
==44508==    by 0x37204C: _objc_init (in /usr/lib/libobjc.A.dylib)
==44508==    by 0xBB27: libSystem_initializer (in /usr/lib/libSystem.B.dylib)
==44508== 
==44508== 96 bytes in 3 blocks are possibly lost in loss record 44 of 88
==44508==    at 0x5A95: malloc_zone_calloc (in /usr/local/Cellar/valgrind/3.8.1/lib/valgrind/vgpreload_memcheck-amd64-darwin.so)
==44508==    by 0x3756EA: NXHashInsert (in /usr/lib/libobjc.A.dylib)
==44508==    by 0x3757F6: _NXHashRehashToCapacity (in /usr/lib/libobjc.A.dylib)
==44508==    by 0x375748: NXHashInsert (in /usr/lib/libobjc.A.dylib)
==44508==    by 0x38533D: realizeClass(class_t*) (in /usr/lib/libobjc.A.dylib)
==44508==    by 0x3845D0: realizeClass(class_t*) (in /usr/lib/libobjc.A.dylib)
==44508==    by 0x374E9C: _read_images (in /usr/lib/libobjc.A.dylib)
==44508==    by 0x3739EB: map_images_nolock (in /usr/lib/libobjc.A.dylib)
==44508==    by 0x3734F3: map_images (in /usr/lib/libobjc.A.dylib)
==44508==    by 0x7FFF5FC04936: dyld::notifyBatchPartial(dyld_image_states, bool, char const* (*)(dyld_image_states, unsigned int, dyld_image_info const*)) (in /usr/lib/dyld)
==44508==    by 0x7FFF5FC0467C: dyld::registerImageStateBatchChangeHandler(dyld_image_states, char const* (*)(dyld_image_states, unsigned int, dyld_image_info const*)) (in /usr/lib/dyld)
==44508==    by 0x83ED9: dyld_register_image_state_change_handler (in /usr/lib/system/libdyld.dylib)
==44508== 
==44508== 120 bytes in 5 blocks are possibly lost in loss record 47 of 88
==44508==    at 0x5A95: malloc_zone_calloc (in /usr/local/Cellar/valgrind/3.8.1/lib/valgrind/vgpreload_memcheck-amd64-darwin.so)
==44508==    by 0x37566D: NXHashInsert (in /usr/lib/libobjc.A.dylib)
==44508==    by 0x3757F6: _NXHashRehashToCapacity (in /usr/lib/libobjc.A.dylib)
==44508==    by 0x375748: NXHashInsert (in /usr/lib/libobjc.A.dylib)
==44508==    by 0x38533D: realizeClass(class_t*) (in /usr/lib/libobjc.A.dylib)
==44508==    by 0x3845D0: realizeClass(class_t*) (in /usr/lib/libobjc.A.dylib)
==44508==    by 0x374E9C: _read_images (in /usr/lib/libobjc.A.dylib)
==44508==    by 0x3739EB: map_images_nolock (in /usr/lib/libobjc.A.dylib)
==44508==    by 0x3734F3: map_images (in /usr/lib/libobjc.A.dylib)
==44508==    by 0x7FFF5FC04936: dyld::notifyBatchPartial(dyld_image_states, bool, char const* (*)(dyld_image_states, unsigned int, dyld_image_info const*)) (in /usr/lib/dyld)
==44508==    by 0x7FFF5FC0467C: dyld::registerImageStateBatchChangeHandler(dyld_image_states, char const* (*)(dyld_image_states, unsigned int, dyld_image_info const*)) (in /usr/lib/dyld)
==44508==    by 0x83ED9: dyld_register_image_state_change_handler (in /usr/lib/system/libdyld.dylib)
==44508== 
...
==44508== 8,192 bytes in 8 blocks are definitely lost in loss record 86 of 88
==44508==    at 0x54D7: malloc_zone_malloc (in /usr/local/Cellar/valgrind/3.8.1/lib/valgrind/vgpreload_memcheck-amd64-darwin.so)
==44508==    by 0x3745AE: objc::DenseMap<objc_object*, unsigned long, true, objc::DenseMapInfo<objc_object*>, objc::DenseMapInfo<unsigned long> >::init(unsigned int) (in /usr/lib/libobjc.A.dylib)
==44508==    by 0x37455A: arr_init (in /usr/lib/libobjc.A.dylib)
==44508==    by 0x3739DF: map_images_nolock (in /usr/lib/libobjc.A.dylib)
==44508==    by 0x3734F3: map_images (in /usr/lib/libobjc.A.dylib)
==44508==    by 0x7FFF5FC04936: dyld::notifyBatchPartial(dyld_image_states, bool, char const* (*)(dyld_image_states, unsigned int, dyld_image_info const*)) (in /usr/lib/dyld)
==44508==    by 0x7FFF5FC0467C: dyld::registerImageStateBatchChangeHandler(dyld_image_states, char const* (*)(dyld_image_states, unsigned int, dyld_image_info const*)) (in /usr/lib/dyld)
==44508==    by 0x83ED9: dyld_register_image_state_change_handler (in /usr/lib/system/libdyld.dylib)
==44508==    by 0x37204C: _objc_init (in /usr/lib/libobjc.A.dylib)
==44508==    by 0xBB27: libSystem_initializer (in /usr/lib/libSystem.B.dylib)
==44508==    by 0x7FFF5FC13377: ImageLoaderMachO::doModInitFunctions(ImageLoader::LinkContext const&) (in /usr/lib/dyld)
==44508==    by 0x7FFF5FC13761: ImageLoaderMachO::doInitialization(ImageLoader::LinkContext const&) (in /usr/lib/dyld)
==44508== 
==44508== 10,808 bytes in 1 blocks are possibly lost in loss record 87 of 88
==44508==    at 0x5237: malloc (in /usr/local/Cellar/valgrind/3.8.1/lib/valgrind/vgpreload_memcheck-amd64-darwin.so)
==44508==    by 0x101745: tzsetwall_basic (in /usr/lib/system/libsystem_c.dylib)
==44508==    by 0x103043: localtime (in /usr/lib/system/libsystem_c.dylib)
==44508==    by 0xFB52C: gettimeofday (in /usr/lib/system/libsystem_c.dylib)
==44508==    by 0x1000017A0: main (in ./graph)
==44508== 
==44508== LEAK SUMMARY:
==44508==    definitely lost: 9,729 bytes in 16 blocks
==44508==    indirectly lost: 1,236 bytes in 7 blocks
==44508==      possibly lost: 15,749 bytes in 68 blocks
==44508==    still reachable: 47,952 bytes in 274 blocks
==44508==         suppressed: 0 bytes in 0 blocks
==44508== Reachable blocks (those to which a pointer was found) are not shown.
==44508== To see them, rerun with: --leak-check=full --show-reachable=yes
==44508== 
==44508== For counts of detected and suppressed errors, rerun with: -v
==44508== ERROR SUMMARY: 22 errors from 22 contexts (suppressed: 0 from 0)

他们没有指向我的代码,但我仍然有漏洞。我应该担心吗?

(此外,非常感谢任何关于我做错了什么或改进我的代码风格的指针(看看我在那里做了什么?)!)

4

3 回答 3

5

您使用的是最新最好的 Valgrind 3.8.1。对 OSX 10.8 的支持在不到完整版本(3.8.0)之前引入,正如valgrind 的新闻页面所说

最初支持 MacOSX 10.8,但目前不能用于严肃的工作。

3.8.0 版本也Reduced noise (false positive) level on MacOSX 10.6/10.7有,但 10.8 没有这样的运气。我强烈建议在valgrind 下载页面上查看当前版本,您也可以从那里订阅最新版本的通知(这应该会为 Mac OSX10.8 带来更好的支持)

注意: valgrind 3.7.0 增加了对 OSX 10.7 的支持,所以你可能只需要等待 3.9 出来就可以得到更好的支持。

就“可能的内存泄漏”而言,您始终可以使用--show-possibly-lost=no标志来抑制这些警告,并且现在对 valgrind 为您提供的所有内容持保留态度。

我指给你看的最后一页说:

“可能丢失”意味着您的程序正在泄漏内存,除非您正在使用可能导致它们指向已分配块中间的指针做不寻常的事情;请参阅用户手册了解一些可能的原因。

在新版本的 valgrind 出现更好地支持 OSX 10.8 之前,我强烈建议将 Oracle 的 VirtualBox 与一些 Linux 发行版一起使用,您可以在其中运行您的代码,而且这一切都是免费的,甚至还有一个 OSX 10.8 版本。当我在我的 Linux virtualbox 上运行您的程序时,我没有看到任何错误报告,(请记住,我有一个稍旧的版本)所以很有可能您只是看到误报:

mike@mike-VirtualBox:~/C$ valgrind ./a.out
==12465== Memcheck, a memory error detector
==12465== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==12465== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==12465== Command: ./a.out
==12465== 
Took 43.77033 ms to run
==12465== 
==12465== HEAP SUMMARY:
==12465==     in use at exit: 0 bytes in 0 blocks
==12465==   total heap usage: 12,900,000 allocs, 12,900,000 frees, 284,000,000 bytes allocated
==12465== 
==12465== All heap blocks were freed -- no leaks are possible
==12465== 
==12465== For counts of detected and suppressed errors, rerun with: -v
==12465== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 2 from 2)
mike@mike-VirtualBox:~/C$ 
于 2013-03-25T12:36:33.820 回答
1

我一般你应该最担心的definitely lost,它真的取决于图书馆。Possibly lost有时表示内存丢失,但实际上没有,如果您有复杂的数据类型并使用指针“嵌套”、强制转换和指针传递,则可能会发生这种情况。

另一点是,如果您正在使用图像并且问题在于图像分配或取消分配中,很多字节(可能)会丢失,因此您应该担心,因为这可能会很快积累并耗尽您的内存,具体取决于如何你经常这样做。

另一方面,有些库需要您调用*_init函数,它设置了一些全局变量并且从不清理它们。那是相当无害的。

你可以让 valgrind 将这些与所谓的 supression-files 混合。

于 2013-03-25T11:30:17.577 回答
0

我应该担心吗?

您是否应该担心 valgrind 在您的系统上无法正常工作?并不真地; 还有其他内存分析器可以在您的系统上正常工作。

您是否应该担心您的程序会泄漏内存?好吧,除非您使用正常工作的内存分析器对其进行测试,否则您将无法确定。

...任何指针...至于我是否做错了什么...

是的,你做错了什么...... Valgrind 告诉你它在你的操作系统上大部分都被破坏了,但是你已经绕过了任何合理的程序员会采取的所有选项,然后在这里提出问题:

  1. 获得一个稳定的内存分析器(与您的操作系统兼容)而不是一个不稳定的,并使用该分析器验证泄漏(例如,/usr/bin/leaks/usr/bin/malloc_history,或“ XCode Instruments ”)。
  2. 如果这不可用,请在虚拟机中安装 Linux 或 FreeBSD 并在那里运行 valgrind,它将是稳定的。

如果您的问题有这两种选择之一的输出,则它不会显示该警告,也可能不会显示损坏/令人困惑的输出。也许这表明您的程序没有泄漏,在这种情况下,您可以少一点担心,因为您的程序不太可能因泄漏内存而受到指责。也许这表明您的程序在函数 xxx 中泄漏了内存,在函数中分配,在这种情况下,您可以不用担心,因为您可以自己诊断和修复这些泄漏

于 2013-03-25T12:41:13.297 回答