简体   繁体   English

C++ 错误:如何解决 malloc:对象的 *** 错误...被释放的指针未分配

[英]C++ error: How to solve malloc: *** error for object ... pointer being freed was not allocated

Why am I getting this error after I compile:为什么我编译后会收到此错误:

Program1(49296,0x7fff74b72000) malloc: *** error for object 0x7f9222500040: pointer being freed was not allocated Program1(49296,0x7fff74b72000) malloc: *** 对象 0x7f9222500040 的错误:被释放的指针未分配

Is there a way to resolve it?有办法解决吗? I only get it when I do recursion, but if I take out this line:我只有在递归时才能得到它,但是如果我取出这一行:

if(counter == G.nameSet.size())
{  
  explore(G, *adjPtr);
}

...It then works ...然后就可以了

void explore(Graph & G, Node & foo)
{
  G.nameSet.insert(foo.name());
  set <string> tempNameSet;

  list <Node> adjacentList = G.getAdjNodes(foo); 

  int y = adjacentList.size();
  list<Node>::iterator adjPtr = adjacentList.begin();

  for(int i=0; i < y;  i++ )
  {
tempNameSet = G.nameSet;

set<string>::iterator nSetPtr = G.nameSet.begin();
int counter = 0;
for (int j = 0; j < G.nameSet.size(); j++)
{ 
  if(*nSetPtr != adjPtr->name())
    counter++; 

  if(tempNameSet.size() > 1)
    nSetPtr  = tempNameSet.erase(nSetPtr);
}

if(counter == G.nameSet.size())
{  
  explore(G, *adjPtr);  //even when I plug in foo instead of *adjPtr, i get the same error
}

if(adjacentList.size() > 1)
  adjPtr  = adjacentList.erase(adjPtr);

  }
}

It's hard because that function isn't calling free directly.这很难,因为该函数没有直接调用 free。 At a guess, it's crashing in erase(adjPtr).据猜测,它在擦除(adjPtr)中崩溃了。 To verify that is the case, put a diagnostic printf before and after the call, and the crash should happen between the calls.要验证是否是这种情况,请在调用前后放置诊断 printf,并且崩溃应该发生在调用之间。 Probably adjPtr is going empty and that causes erase to fail, but exactly where the logic error is is quite hard to tell.可能 adjPtr 变空并导致擦除失败,但逻辑错误的确切位置很难说。

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

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