简体   繁体   English

需要帮助找到记忆漏洞

[英]Need help finding memoryleak

I have been sitting for hours now, and it's driving me nuts. 我现在坐了好几个小时,这让我疯了。 Im new too c++ so if something else besides the memoryleaks is wrong I would love to know! 我是新c ++所以如果除了memoryleaks之外的其他东西是错的,我很想知道! :D :d

The thing is that if I make a new teacher/assistant/TA and print it I get: 问题是,如果我成为一名新教师/助理/ TA并打印出来,我会得到:

{212} normal block at 0x005694B0, 8 bytes long.
 Data: <  V     > BC 91 56 00 00 00 00 00 
{210} normal block at 0x005691B8, 40 bytes long.
 Data: <      V Oliver  > BC A4 16 00 B0 94 56 00 4F 6C 69 76 65 72 00 CD 
{209} normal block at 0x00568F30, 8 bytes long.
 Data: <  V     > 90 8E 56 00 00 00 00 00 
{208} normal block at 0x00569468, 8 bytes long.
 Data: <d V     > 64 8E 56 00 00 00 00 00 
{204} normal block at 0x00568E60, 76 bytes long.
 Data: <    h V elev    > D4 A6 16 00 68 94 56 00 65 6C 65 76 00 CD CD CD 

If I keep repeting it just stacks up, would appreciate all the help I can get! 如果我继续重复它只是堆积起来,将感谢我能得到的所有帮助! :D :d

    int main(){



    int choise, count=0;
    bool ta = false , teacher = false, assistant = false;
    choise = meny();

    Employee **work = NULL;
    Employee **swapper = NULL;
    Employment *tmp = NULL;

    string name, types, subject, temp, type;
    int birthyear, salary, workhours, points;
    bool boss = false, ansvarig, key=false, cert;

    while (choise != NULL){
        if (choise > 0){

            if (choise == 1){
                fflush(stdin);
                cout << endl << "Namn: ";
                getline(cin, name);
                cout << endl << "Fodelsear: ";
                cin >> birthyear;
                cin.ignore();
                cout << "\nTjänst: ";
                getline(cin, type);
                cout << "\nLon: ";
                cin >> salary;
                cin.ignore();
                cout << "\nChef: ";
                cin >> temp;
                cin.ignore();
                if (temp == "Ja" || temp == "ja"){
                    boss = true;
                }
                else
                    boss = false;
                cout << "\nTjanstgoringstid: ";
                cin >> workhours;
                cin.ignore();
                cout << "\nHuvudämne: ";
                cin >> subject;
                cin.ignore();
                cout << "\nProgramansvarig: ";
                cin >> temp;
                cout << "\n\n\n";
                cin.ignore();
                if (temp == "Ja" || temp == "ja")
                    ansvarig = true;
                else
                    ansvarig = false;



                count++;
                teacher = true;

            }
            else if (choise == 2){
                fflush(stdin);
                cout << endl << "Namn: ";
                getline(cin, name);

                cout << endl << "Fodelsear: ";
                cin >> birthyear;
                fflush(stdin);

                cout << "\nTjänst: ";
                getline(cin, type);

                cout << "\nLon: ";
                cin >> salary;
                fflush(stdin);

                cout << "\nHuvudämne: ";
                cin >> subject;
                fflush(stdin);

                cout << "Avklarade poang: ";
                cin >> points;
                fflush(stdin);
                count++;

                assistant = true;
            }
            else if (choise == 3){

                fflush(stdin);

                cout << endl << "Namn: ";
                getline(cin, name);

                cout << endl << "Fodelsear: ";
                cin >> birthyear;
                cin.ignore();

                cout << "\nTjänst: ";
                getline(cin, type);


                cout << "\nLon: ";
                cin >> salary;
                cin.ignore();

                cout << "\nTjanstgoringstid: ";
                cin >> workhours;
                cin.ignore();

                cout << "\nAttestratt: ";
                cin >> temp;
                cin.ignore();
                if (temp == "Ja" || temp == "ja"){
                    cert = true;
                }
                else
                    cert = false;

                cout << "\nHar huvudnyckel: ";
                cin >> temp;
                cin.ignore();
                if (temp == "Ja" || temp == "ja"){
                    cert = true;
                }
                else
                    cert = false;


                cout << "\n\n\n";




                count++;
                ta = true;
            }

            else if (choise == 4){
                for (int x = 0; x < (count); x++){
                    cout << work[x]->toString();
                }
            }
            if (choise != 0 && choise!=4){
                swapper = new Employee*[count];

                if (teacher == true){
                    tmp = new Teacher(type, boss, salary, workhours, subject, ansvarig);

                    swapper[count - 1] = new Employee(name, birthyear, tmp);

                    teacher = false;

                    //delete tmp;
                }
                else if (ta == true){
                    tmp = new TA(type, boss, salary, workhours, cert, key);

                    swapper[count - 1] = new Employee(name, birthyear, tmp);

                    assistant = false;

                //  delete tmp;
                }
                else if (assistant == true){
                    tmp = new Assistant(type, salary, subject, points);


                    swapper[count - 1] = new Employee(name, birthyear, tmp);

                //  delete tmp;


                }



                    for (int x = 0; x < count - 1; x++){
                    swapper[x] = work[x];
                    }



                delete[] work;



                work = swapper;



            }

                choise = meny();




        }
    }

    /*for (int x = 0; x < count; x++){
        delete work[x];
        }*/

    delete [] work;



    _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
    _CrtDumpMemoryLeaks();

    return 0;

}

You never delete your arrays. 你永远不会delete你的数组。 You should use vectors anyway. 无论如何你应该使用矢量。

The thing is basically that for every new you make, you need to have a delete when the object is no longer needed. 基本上,对于您制作的每个new内容,您都需要在不再需要该对象时进行delete Doing this deallocate the memory. 这样做可以释放内存。 If you don't, you keep pilling up amounts of allocated memories forever and ever. 如果你不这样做,你就会永远不断地增加分配的记忆。

Memory leaks occur whenever you dynamically allocate memory and do not release it. 只要您动态分配内存并且不释放内存,就会发生内存泄漏。 This means that for every new that appears in your program, a corresponding delete must exist or else you're leaking memory. 这意味着对于程序中出现的每个new ,必须存在相应的delete ,否则您将泄漏内存。

There are a lot of delete s that have been commented out in your code (eg, for tmp ). 在您的代码中已经注释了很多delete (例如,对于tmp )。 So, right off the bat, we know for sure those allocations are leaking. 所以,马上,我们肯定知道那些分配正在泄漏。

Likewise the memory you're allocating for swapper isn't getting deallocated (at least the first time through the loop). 同样,您为swapper分配的内存未被释放(至少第一次通过循环)。 Additionally, even though you invoke delete [] work , you aren't delete ing the individual elements in the array, which are also dynamically allocated. 此外,即使您调用delete [] work ,也不会delete数组中的各个元素,这些元素也是动态分配的。

And since all these allocations occur within a loop, it's no wonder that you have so many leaks! 由于所有这些分配都发生在一个循环中,因此难怪你有这么多泄漏!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM