[英]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. 但是我不是动态保留内存,也不是在析构函数中做任何事情:该程序是SSJF算法,用于在操作系统中选择柱面。
I have a simple class called IO: 我有一个称为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 malloc: *对象0x10f68b3e0错误:未分配释放的指针*在malloc_error_break中设置一个断点进行调试
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()
. ...在while
循环内,而反过来又在while
myList.pop_front()
内while
该myList.pop_front()
还调用myList.pop_front()
。
I can only suggest that you debug your code to see how many times pop_front()
is invoked for mylist
. 我只能建议您调试代码以查看对mylist
调用pop_front()
次数。 My money is on it being more than mylist.size()
times, hence my question in the comments (with new emphasis): 我的钱超过了mylist.size()
倍, 因此我在评论中提出的问题 (具有新的重点):
How many items are in
myList
when the error is thrown ? 引发错误时 ,myList
有多少个项目?
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. ...但是很难从您提供的摘录中分辨出来。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.