删除时C ++ valgrind内存丢失?

时间:2015-12-10 18:47:15

标签: c++ memory-leaks valgrind dynamic-memory-allocation delete-operator

根据valgrind的说法,我有一些严重的内存泄漏错误。奇怪的是,当我从visual studio运行它时,我的程序并没有崩溃。但是,当我尝试在linux上编译时,我得到一个段错误,所以我猜错了。

我注释掉了大部分代码,只留下了new和delete []部分(注意,没有新语句,valgrind没有任何问题)但奇怪的是,在这个测试版本中,我实际上在/ new之后删除了[] /,而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 = '\0'; //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)

2 个答案:

答案 0 :(得分:3)

new应与delete匹配,new[]应与delete[]匹配。否则它是未定义的行为。您似乎正在通过delete[]分配的内存new

答案 1 :(得分:2)

你走了:

myitem = new Perishable();//new keyword
delete[] myitem;

您正在呼叫delete[]以获取未分配new[]的内容。这是未定义的行为,valgrind在抱怨中是正确的。