简体   繁体   中英

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

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. At a guess, it's crashing in erase(adjPtr). To verify that is the case, put a diagnostic printf before and after the call, and the crash should happen between the calls. Probably adjPtr is going empty and that causes erase to fail, but exactly where the logic error is is quite hard to tell.

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