内存在嵌套对象中不可用

时间:2016-02-12 08:32:36

标签: c++ object memory-leaks valgrind free

我发现Valgrind的内存泄漏导致内存错误:free()/ delete / delete [] / realloc()无效。

==7367== Invalid free() / delete / delete[] / realloc()
==7367==    at 0x4C2BDEC: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==7367==    by 0x40077F: ItemSet::~ItemSet() (cart.cpp:33)
==7367==    by 0x40086B: ShoppingCart::~ShoppingCart() (cart.cpp:14)
==7367==    by 0x400828: main (cart.cpp:45)
==7367==  Address 0x5a1c0b0 is 0 bytes inside a block of size 40 free'd
==7367==    at 0x4C2BDEC: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==7367==    by 0x40077F: ItemSet::~ItemSet() (cart.cpp:33)
==7367==    by 0x400800: ShoppingCart::ShoppingCart() (cart.cpp:39)
==7367==    by 0x400817: main (cart.cpp:43)
==7367== 
==7367== 
==7367== HEAP SUMMARY:
==7367==     in use at exit: 40 bytes in 1 blocks
==7367==   total heap usage: 2 allocs, 2 frees, 80 bytes allocated
==7367== 
==7367== LEAK SUMMARY:
==7367==    definitely lost: 40 bytes in 1 blocks
==7367==    indirectly lost: 0 bytes in 0 blocks
==7367==      possibly lost: 0 bytes in 0 blocks
==7367==    still reachable: 0 bytes in 0 blocks
==7367==         suppressed: 0 bytes in 0 blocks
==7367== Rerun with --leak-check=full to see details of leaked memory
==7367== 
==7367== For counts of detected and suppressed errors, rerun with: -v
==7367== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

这是代码。它是我程序的简化版本,但它仍然存在我遇到的问题。

#include <cstdlib>

class ItemSet {
    private:
        int* items;

    public:
        ItemSet();
        virtual ~ItemSet();

        // ItemSet Methods...
};

class ShoppingCart {
    private:
        ItemSet items_in_cart;
    public:
        ShoppingCart();

        // ShoppingCart methods...
};

ItemSet::ItemSet() {
    // Constructor
    int max_num_of_items = 10;
    items = (int*)calloc(sizeof(int), max_num_of_items);
}

ItemSet::~ItemSet() {
    // Destructor
    if (NULL != items) {
        // Frees the dynamically allocated register files
        free(items);
    }
}

ShoppingCart::ShoppingCart() {
    // Constructor
    items_in_cart = ItemSet();
}

int main() {
    ShoppingCart cart = ShoppingCart();

    return 0;
}

1 个答案:

答案 0 :(得分:0)

ShoppingCart::ShoppingCart() {
    // Constructor
    items_in_cart = ItemSet();
}

最后一行不可行。在分配之后,我们有两个ItemSet个对象具有相同的items值 - 在右侧创建的临时对象items_in_cart。当第二个被摧毁时,你有一个双重免费。

使用合理的方法来收集std::liststd::vectorstd::array等对象。否则,请按照rule of 3,规则of 5或零。