【发布时间】:2018-04-07 21:08:26
【问题描述】:
也许这不是一个好问题,但我有点绝望。我有内存泄漏,但我不知道如何克服它。 使用 valgrind 查找:
- 总堆使用量:3 次分配,2 次释放,73,804 字节分配
- 仍可访问:72,704 字节,1 个块
但我找不到我在代码中丢失“删除”的位置;也许有人可以帮助我
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,但仍有内存泄漏
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)
【问题讨论】:
-
使用调试信息 (
-g) 编译您的代码,valgrind 会告诉您数据的确切分配位置。 -
开始使用
std::unique_ptr,不再担心内存泄漏。 -
这是我的学生项目,我不能在那里使用 std::unique_ptr :(
-
如果您尝试使用
TVector远程执行任何复杂的操作,您可能会遇到问题,因为您没有实现自己的复制构造函数或复制赋值运算符。见rule of 0/3/5。 -
@Jully 然后从重新实现您自己的开始——它不会过于复杂,并且可以防止很多这样的问题。
标签: c++ memory-leaks