C++删除时内存丢失

C++ valgrind memory loss when deleting?

本文关键字:内存 删除 C++      更新时间:2023-10-16

根据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[]。这是不明确的行为,勇敢地抱怨是正确的。