C++删除时内存丢失
C++ valgrind memory loss when deleting?
根据valgrind的说法,我有一些严重的内存泄漏错误。奇怪的是,当我在visual studio运行程序时,它不会崩溃。然而,当我尝试在linux上编译时,我会得到一个segfault,所以我猜是出了问题。
我注释掉了大部分代码,只留下了新的和删除的[]部分(注意,如果没有新的语句,valgrind没有任何问题),但奇怪的是,在这个测试版本中,我确实在删除新的之后的[]/,而valgrind仍然抱怨内存泄漏。
从理论上(据我所知),这不应该是可能的。我只是得到了一个假阳性,还是我忽略了什么?
代码:
void PosApp::loadRecs() {//.......................This function reads all of the perishable/nonperishable data
bool mycontinue = true;
fstream myfile;
myfile.open(_filename, ios::in);
if (!myfile.is_open()) {
myfile.clear();
myfile.close();
myfile.open(_filename, ios::out);
myfile.close();
mycontinue = false;
}
if (mycontinue) {
char buffer = ' '; //Valgrind didn't like it unassigned
int itemindex = 0;
Item* myitem;
while (!myfile.get(buffer).fail()) {// && itemindex <= _noOfItems - 1) {
std::cout << std::endl << "buff: " << buffer << std::endl;
if (buffer == 'N' || buffer == 'P') {
std::cout << std::endl;
std::cout << "The Deallocation:" << std::endl
<< "No of items -1: " << _noOfItems
<< " >= itemindex: " << itemindex << std::endl;
if (_noOfItems - 1 >= itemindex) { //unitialized valgrind error (conditional depends on unitialized values)
std::cout << std::endl << "going to deallocate name: " << _items[itemindex]->name() << " at index: " << itemindex << std::endl;
delete[] _items[itemindex]; //VALGRIND SAY CANT DO THIS invalid read of size 4
}
if (buffer == 'P') {
std::cout << std::endl << "going to allocate a perishable";
myitem = new Perishable();//new keyword
std::cout << std::endl << "DONE allocating a perishable" << std::endl;
delete[] myitem;
}
else if (buffer == 'N') {//else is extra safe
std::cout << std::endl << "going to allocate a nonperishable";
myitem = new NonPerishable();
std::cout << std::endl << "DONE allocating a nonperishable" << std::endl;
delete[] myitem;
}
//std::cout << std::endl << "Going to assign " << " at index: " << itemindex << std::endl;
//myfile.ignore();//.................ignore comma NOTE THIS IS IN WRONG SPOT?
//myitem->load(myfile);
//_items[itemindex] = myitem;
//std::cout << "Assigned name: " << _items[itemindex]->name() << " at index: " << itemindex << std::endl;
//itemindex++; //NoOfItems
}
}
myfile.close();
_noOfItems = itemindex;//inderect lost blocks
}
}
Valgrind输出:
== 20875 == HEAP SUMMARY :
== 20875 == in use at exit : 656 bytes in 5 blocks
== 20875 == total heap usage : 16 allocs, 16 frees, 10, 869 bytes allocated
== 20875 ==
== 20875 == Searching for pointers to 5 not- freed blocks
== 20875 == Checked 101, 796 bytes
== 20875 ==
== 20875 == 656 bytes in 5 blocks are definitely lost in loss record 1 of 1
== 20875 == at 0x4028C39: operator new(unsigned int) (in / usr / lib / valgrind / vgpreload_memcheck - x86 - linux.so)
== 20875 == by 0x804D6BB : sict::PosApp::run() (PosApp.cpp:403)
== 20875 == by 0x804C1A3 : main(milestone4.cpp:8)
== 20875 ==
== 20875 == LEAK SUMMARY :
== 20875 == definitely lost : 656 bytes in 5 blocks
== 20875 == indirectly lost : 0 bytes in 0 blocks
== 20875 == possibly lost : 0 bytes in 0 blocks
== 20875 == still reachable : 0 bytes in 0 blocks
== 20875 == suppressed : 0 bytes in 0 blocks
== 20875 ==
== 20875 == ERROR SUMMARY : 13 errors from 6 contexts(suppressed : 19 from 8)
== 20875 ==
== 20875 == 1 errors in context 1 of 6 :
== 20875 == Conditional jump or move depends on uninitialised value(s)
== 20875 == at 0x4017E9D : strlen(in / lib / ld - 2.14.1.so)
== 20875 == by 0x636F6C2E : ? ? ?
== 20875 == Uninitialised value was created by a stack allocation
== 20875 == at 0x40078F1 : _dl_init_paths(in / lib / ld - 2.14.1.so)
== 20875 ==
== 20875 ==
== 20875 == 1 errors in context 2 of 6 :
== 20875 == Conditional jump or move depends on uninitialised value(s)
== 20875 == at 0x4017E93 : strlen(in / lib / ld - 2.14.1.so)
== 20875 == by 0x636F6C2E : ? ? ?
== 20875 == Uninitialised value was created by a stack allocation
== 20875 == at 0x40078F1 : _dl_init_paths(in / lib / ld - 2.14.1.so)
== 20875 ==
== 20875 ==
== 20875 == 2 errors in context 3 of 6 :
== 20875 == Invalid read of size 4
== 20875 == at 0x804C70D : sict::PosApp::loadRecs() (PosApp.cpp:132)
== 20875 == by 0x804D6BB : sict::PosApp::run() (PosApp.cpp:403)
== 20875 == by 0x804C1A3 : main(milestone4.cpp:8)
== 20875 == Address 0x43021e4 is 4 bytes before a block of size 148 alloc'd
== 20875 == at 0x4028C39 : operator new(unsigned int) (in / usr / lib / valgrind / vgpreload_memcheck - x86 - linux.so)
== 20875 == by 0x804D6BB : sict::PosApp::run() (PosApp.cpp:403)
== 20875 == by 0x804C1A3 : main(milestone4.cpp:8)
== 20875 ==
== 20875 ==
== 20875 == 3 errors in context 4 of 6 :
== 20875 == Invalid read of size 4
== 20875 == at 0x804C7DB : sict::PosApp::loadRecs() (PosApp.cpp:139)
== 20875 == by 0x804D6BB : sict::PosApp::run() (PosApp.cpp:403)
== 20875 == by 0x804C1A3 : main(milestone4.cpp:8)
== 20875 == Address 0x4302ab4 is 4 bytes before a block of size 120 alloc'd
== 20875 == at 0x4028C39 : operator new(unsigned int) (in / usr / lib / valgrind / vgpreload_memcheck - x86 - linux.so)
== 20875 == by 0x804D6BB : sict::PosApp::run() (PosApp.cpp:403)
== 20875 == by 0x804C1A3 : main(milestone4.cpp:8)
== 20875 ==
== 20875 ==
== 20875 == 5 errors in context 5 of 6 :
== 20875 == Invalid free() / delete / delete[]
== 20875 == at 0x4027B13 : operator delete[](void*) (in / usr / lib / valgrind / vgpreload_memcheck - x86 - linux.so)
== 20875 == by 0x804D6BB : sict::PosApp::run() (PosApp.cpp:403)
== 20875 == by 0x804C1A3 : main(milestone4.cpp:8)
== 20875 == Address 0x43021e4 is 4 bytes before a block of size 148 alloc'd
== 20875 == at 0x4028C39 : operator new(unsigned int) (in / usr / lib / valgrind / vgpreload_memcheck - x86 - linux.so)
== 20875 == by 0x804D6BB : sict::PosApp::run() (PosApp.cpp:403)
== 20875 == by 0x804C1A3 : main(milestone4.cpp:8)
== 20875 ==
--20875--
--20875--used_suppression : 19 U1004 - ARM - _dl_relocate_object
== 20875 ==
== 20875 == ERROR SUMMARY : 13 errors from 6 contexts(suppressed : 19 from 8)
new
应与delete
匹配,new[]
应与delete[]
匹配。否则就是未定义的行为。您似乎正在对通过new
分配的内存执行delete[]
。
开始:
myitem = new Perishable();//new keyword
delete[] myitem;
您正在为未分配给new[]
的内容调用delete[]
。这是不明确的行为,勇敢地抱怨是正确的。
相关文章:
- 迭代时从向量和内存中删除对象
- 为什么示例代码访问IUnknown中已删除的内存
- 我们可以删除链表中静态内存中的节点吗
- 在c++中删除内存失败
- 如何在cpp中删除(免费)多维结构内存?
- 如何删除列出的"QGraphicsPathItem"对象以控制进程内存使用情况?
- 一种从内存中删除 UTF 字节的方法?
- 内存泄漏如何查找和删除
- 擦除是否删除 stl 无序列图元素使用的堆内存
- 无法删除布尔动态内存分配
- 删除类成员的动态分配内存的最佳方法是什么
- 如何控制或优化或删除或释放 UNION 中未使用的内存
- 避免在使用链接列表从 deque 中删除最后一个节点时出现内存泄漏
- 为什么删除分配的阵列会导致内存错误?
- 删除指针后内存泄漏
- 提升进程间共享内存删除、权限和输出文件
- 升级分解共享内存删除对象而无需销毁
- C++内存:删除未使用的布尔数组可以将结果从正确更改为错误
- 函数内的动态内存删除
- 内存删除的运行时检测