简体   繁体   中英

pointer being freed was not allocated error?

I have seen many posts for this error. But I'm not reserving memory dynamically or doing anything in destructor: This program is SSJF algorithm for selecting cylinder in operating system.

I have a simple class called IO:

class IO
{
    public:
            IO();
            IO(int,int);
            void setIO(int,int);
            ~IO();
            int trackNo;
            int arrival;
            int start;
            int end;
            bool finished;
};

Here is the implementation of the class::

IO::IO(int arr, int tNum)
{
    this->arrival = arr;
    this->trackNo = tNum;
    this->start = 0;
    this->end = 0;
}    
IO::IO()
{

}

IO::~IO()
{

}    
void IO::setIO(int t1, int t2)
{
    this->trackNo = t1;
    this->arrival = t2;
}

And finally here is part of main program:

list<IO> myList;
....
myList.push_back(tmpIO); //Add to the list
...
list<IO> wt_list;

And later I'm trying to do some operations. I have deleted some of the part which is not related.

    //list<IO>::iterator itMin;
    while(myList.size()>0)
    {
        //If it is the first input just get it
        if(f)
        {

            IO selected = myList.front();
            curr_time += selected.arrival + selected.trackNo;
            f=false;
            cout << selected.arrival<<endl;
            lastPos = selected.trackNo;
            myList.pop_front();

        }
        //Check if there is any item to add to queue
        while(myList.front().arrival <  curr_time)
        {
            wt_list.push_back(myList.front());
             myList.pop_front(); //Error is coming from this line
        }

        while(wt_list.size()>0)
        {

        }

Error message:

malloc: * error for object 0x10f68b3e0: pointer being freed was not allocated * set a breakpoint in malloc_error_break to debug

Anyone can help me and explain why I get this error and how can I skip it?

The simplest code I can come up with to reproduce this error looks like this:

#include <list>

int main()
{
    std::list<int> mylist;
    mylist.pop_front();
}

I can prevent the error by doing:

#include <list>

int main()
{
    std::list<int> mylist;
    if (!mylist.empty())
    {
        mylist.pop_front();
    }
}

You're calling:

myList.pop_front();

...within a while -loop, which in turn is within a while -loop that also calls myList.pop_front() .

I can only suggest that you debug your code to see how many times pop_front() is invoked for mylist . My money is on it being more than mylist.size() times, hence my question in the comments (with new emphasis):

How many items are in myList when the error is thrown ?

Perhaps the simplest fix will be to replace...

    //Check if there is any item to add to queue
    while(myList.front().arrival <  curr_time)
    {
        wt_list.push_back(myList.front());
         myList.pop_front(); //Error is coming from this line
    }

    while(wt_list.size()>0)
    {

    }

...with...

    while (!mylist.empty() && myList.front().arrival < curr_time)
    {
        wt_list.push_back(myList.front());
        myList.pop_front();
    }

    while (!wt_list.empty())
    {
    }

...but it's hard to tell from the snippet you've provided.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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