找不到内存泄漏

时间:2017-10-26 13:02:13

标签: c++ memory-leaks

也许这不是一个好问题,但我有点绝望。我有记忆泄漏,但我不知道如何克服它。 使用valgrind find:

  • 总堆使用量:3个allocs,2个frees,73,804个字节分配
  • 仍然可以访问:1个块中的72,704个字节

但是我找不到丢失的地方"删除"在代码中;也许有人可以帮助我

class TData {
public:
    bool IsEmpty;
    int Key;
    char Value[65];
    TData();
    void Print();
};

class TVector {
private:
    size_t capacity;
public:
    TData *array;
    size_t size;
    TVector();
TVector(const size_t &);
    size_t Size() const;
    size_t Capacity() const;
    void Push_back(const TData &);
    void CountingSort(TVector* vector);
    ~TVector();
};

TData::TData() {
    this->Key = 0;
}


void TData::Print() {
    printf("%d\t%s\n", this->Key, this->Value);
}

TVector::TVector() {
    size = 0;
    capacity = 1;
    array = new TData[capacity];
}

TVector::TVector(const size_t & sizeVector) {
    capacity = sizeVector;
    size = 0;
    array = new TData[sizeVector];
}

void TVector::Push_back(const TData &temp) {
    if (size == capacity) {
        capacity *= 2;
        TData *result = new TData[capacity];
        for (int index = 0; index < size; index++) {
            result[index] = array[index];
        }
        delete[] array;
        this->array = result;
    }
    array[size++] = temp;
}


size_t TVector::Size() const {
    return size;
}

size_t TVector::Capacity() const {
    return capacity;
}

void TVector::CountingSort(TVector* vector) {
    int tmp[RANGE] = { 0 };
    TData *out = new TData[vector->Size()];

    for (int i = 0; i < vector->Size(); i++) {
        tmp[vector->array[i].Key]++;
    }
    for (int i = 1; i < RANGE; i++) {
        tmp[i] += tmp[i - 1];
    }
    for (int i = vector->Size() - 1; i >= 0; i--) {
        out[--tmp[vector->array[i].Key]] = vector->array[i];
    }
    for (int i = 0; i < vector->Size(); ++i) {
        vector->array[i] = out[i];
    }
delete[] out;
}



TVector::~TVector() {
    delete[] array;
}


int main(void) {

    TVector v;
    TData data;


    while (scanf("%d%s", &data.Key, data.Value) == 2) {
        v.Push_back(data);
    }

    if (v.Size() > 1) {
        v.CountingSort(&v);
    }
    for (int i = 0; i < v.Size(); ++i) {
        printf("%d\t%s\n", v.array[i].Key, v.array[i].Value);
    }

    return 0;
}

我尝试使用带有测试的程序时找到了它。我有时间限制错误,我想也许它可能是内存泄漏。我不知道为什么我之前没有检查过它。

添加了delete[] out,但是stil有内存泄漏

 HEAP SUMMARY:
==4554==     in use at exit: 72,704 bytes in 1 blocks
==4554==   total heap usage: 3 allocs, 2 frees, 73,804 bytes allocated
==4554== 
==4554== 72,704 bytes in 1 blocks are still reachable in loss record 1 of 1
==4554==    at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==4554==    by 0x4EC3EFF: ??? (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21)
==4554==    by 0x40106B9: call_init.part.0 (dl-init.c:72)
==4554==    by 0x40107CA: call_init (dl-init.c:30)
==4554==    by 0x40107CA: _dl_init (dl-init.c:120)
==4554==    by 0x4000C69: ??? (in /lib/x86_64-linux-gnu/ld-2.23.so)
==4554== 
==4554== LEAK SUMMARY:
==4554==    definitely lost: 0 bytes in 0 blocks
==4554==    indirectly lost: 0 bytes in 0 blocks
==4554==      possibly lost: 0 bytes in 0 blocks
==4554==    still reachable: 72,704 bytes in 1 blocks
==4554==         suppressed: 0 bytes in 0 blocks
==4554== 
==4554== For counts of detected and suppressed errors, rerun with: -v
==4554== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

4 个答案:

答案 0 :(得分:3)

我没有看到为此释放记忆:

TData *out = new TData[vector->Size()];

答案 1 :(得分:2)

  1. 使用调试信息构建程序(使用gcc或clang标记-g)。
  2. 使用--leak-check = full运行valgrind:valgrind --leak-check=full a.out
  3. 您没有内存泄漏,您的输出显示某些内存不是免费的,但仍然可以访问

    ==4554== LEAK SUMMARY:
    ==4554==    definitely lost: 0 bytes in 0 blocks
    ==4554==    indirectly lost: 0 bytes in 0 blocks
    ==4554==      possibly lost: 0 bytes in 0 blocks
    ==4554==    still reachable: 72,704 bytes in 1 blocks
    ==4554==         suppressed: 0 bytes in 0 blocks
    

    这不是你的错,而是GNU Libc的一个问题/特性。您可以在https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66339

    获取更多信息

    此内存被分配为紧急缓冲区,用于低内存情况下的C ++异常。您可以在此处找到更多信息:What happens if 'throw' fails to allocate memory for exception object?

    这是导致此警告的代码(请参阅第123行):https://github.com/gcc-mirror/gcc/blob/master/libstdc%2B%2B-v3/libsupc%2B%2B/eh_alloc.cc

    分配

    EMERGENCY_OBJ_SIZE * EMERGENCY_OBJ_COUNT + EMERGENCY_OBJ_COUNT * sizeof (__cxa_dependent_exception)

    相当于

    64 * 1024 + 64 * 112 = 72704

    这是故意的,因为如果你在终止之前分配了一些内存块并没有真正受到伤害,所以一些库(特别是低级别的)这样做,主要是为了解决它们在对象生命周期中的问题。线程环境。你也可能会遇到一些Boost库的问题 - 我认为Boost.System或Boost.Filesystem总是留下32字节的内存块。

答案 2 :(得分:1)

您可以尝试使用智能指针来避免此类问题。 some info here

答案 3 :(得分:1)

我想说的是这里有内存泄漏(当您使用“新”陈述时,您必须记住在同一对象上也使用“删除”)

void TVector::Push_back(const TData &temp) {
    if (size == capacity) {
        capacity *= 2;
        TData *result = new TData[capacity]; <--allocation
        for (int index = 0; index < size; index++) {
            result[index] = array[index];
        }
        delete[] array; <-- removal of 'array' it's ok but where you delete 'result' - memory allocated but never freed
        this->array = result;
    }
    array[size++] = temp;
}

如果您不能使用位于“内存”标头中的智能指针(std :: unique_ptr,std :: shared_ptr,std :: weak_ptr)(自c ++ 11标准起)-您可以创建自己的实现以避免这种情况。

template <typename T>
class smart_ptr
{
public:
    smart_ptr()
    {
        value = new T(0);
    }

    smart_ptr(T p_value)
        :value(new T(p_value))
    {}

    T operator*()
    {
        return *value;
    }

    T* operator->()
    {
        return value;
    }

    ~smart_ptr()
    {
        delete value;
    }
private:
    T *value;
};

当您使用它的代码超出功能范围时,将调用'smart_ptr'类的析构函数-不会导致内存泄漏。但是,这太简单了-我仍然建议使用内存标头中的“原始”智能指针。这样会更安全:)