zloop1982 / bwapi

Automatically exported from code.google.com/p/bwapi
0 stars 0 forks source link

Crash when iterating through units from Broodwar->self()->getUnits() #92

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Add the following code to the onStart() method of the ExampleAIModule
  for(std::set<Unit*>::iterator i=Broodwar->self()->getUnits().begin();
i!=Broodwar->self()->getUnits().end(); i++) {
    if((*i)==NULL) continue;
  }

2. start any map(tested with AstralBalance and BGH)

What version of the product are you using? On what operating system?
ExampleAIModule from Beta2 and BWAPI from r1420

Please provide any additional information below.
Originally I had logging statements in there(using the logger from Util)
and what I saw was that the crash was happening on the 3rd or 4th iteration
of the loop.

I also tried moving the loop into the onFrame method but got the same error.

Original issue reported on code.google.com by iulia...@gmail.com on 15 Nov 2009 at 11:07

GoogleCodeExporter commented 9 years ago
try

for each (BWAPI::Unit* i in Broodwar->self()->getUnits())
{ }

The null check should not be necessary, and the iterator seems to be the cause 
of the
crash??

Original comment by AHeinerm on 15 Nov 2009 at 4:36

GoogleCodeExporter commented 9 years ago
Try this too:

std::set<Unit*> myunits = Broodwar->self()->getUnits();
for(std::set<Unit*>::iterator i = myunits.begin(); i != myunits.end(); i++)
{  }

Original comment by AHeinerm on 15 Nov 2009 at 4:52

GoogleCodeExporter commented 9 years ago
Right now Broodwar->self()->getUnits() returns a new set of units, so checking 
for
the end of the loop with "i!=Broodwar->self()->getUnits().end()" will generate 
and
return a new set of units every time since you're calling self()->getUnits().

Original comment by lowerlo...@gmail.com on 15 Nov 2009 at 7:29

GoogleCodeExporter commented 9 years ago
Using the code in comment2 fixed the issue. 
I added some debug statements to PlayerImpl::getUnits() and it was not 
regenerating
the cache each time which, if it had, would have explained the problem.

Original comment by iulia...@gmail.com on 15 Nov 2009 at 7:57

GoogleCodeExporter commented 9 years ago
In r1427 I changed Player::getUnits() and similar functions in Game so they 
return
references to sets, rather than actual sets of units. I also updated the user
documentation to reflect these changes.

Original comment by lowerlo...@gmail.com on 15 Nov 2009 at 8:41