I have a function that creates and insert some numbers in a vector.
if(Enemy2.dEnemy==true)
{
pt.y=4;
pt.x=90;
pt2.y=4;
pt2.x=125;
for(int i=0; i<6; i++)
{
Enemy2.vS1Enemy.push_back(pt);
Enemy2.vS2Enemy.push_back(pt2);
y-=70;
pt.y=y;
pt2.y=y;
}
Enemy2.dEnemy=false;
Enemy3.cEnemy=0;
}
It should insert 6 numbers in two vectors, the only problem is that it doesn’t – it actually inserts more.
I don’t think the snippet will run unless Enemy2.dEnemy == true, and it won’t stay true for ever.
The first time the snippet runs, then Enemy2.dEnemy is set to false and it shouldn’t run again.
I don’t set Enemy2.dEnemy to true anywhere except when the window is created.
If I insert a break point any where in the snippet, the program will work fine – it will insert ONLY 6 numbers in the two vectors.
Any ideas what’s wrong here?
ok so i did some debugging.
i found that Enemy2.dEnemy=false; is being skipped for some reason.
i tried to do this to see if it was.
if(Enemy2.dEnemy)
{
pt.y=4;
pt.x=90;
pt2.y=4;
pt2.x=125;
for(int i=0; i<6; i++)
{
Enemy2.vS1Enemy.push_back(pt);
Enemy2.vS2Enemy.push_back(pt2);
y-=70;
pt.y=y;
pt2.y=y;
}
TCHAR s[244];
Enemy2.dEnemy=false;
if(Enemy2.dEnemy)
{
MessageBox(hWnd, _T("0"), _T(""), MB_OK);
}
else
{
MessageBox(hWnd, _T("1"), _T(""), MB_OK);
}
Enemy3.cEnemy=0;
}
well the message box popped saying 1 and my code worked fine. it seems that Enemy2.dEnemy=false; doesn’t have time to run ;/
blahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblah!
ok i found where is the real problem which was causing to insert more than 6 numbers..
it was where i was asigning Enemy2.dEnemy=true;
if(Enemy2.e1)
{
Enemy2.now=time(NULL);
Enemy2.tEnemy=Enemy2.now+4;
Enemy2.e1=false;
}
if(Enemy2.tEnemy==time(NULL))
{
check=1;
Enemy2.aEnemy=0;
Enemy2.dEnemy=true;
}
the problem seems that the second if runs more than one time, which is weird!
First things first: get rid of that abominable
if (Enemy2.dEnemy == true)– it should be:(I also prefer to name my booleans as a readable sentence segments like
Enemy2.isABerserkerorEnemy3.hasHadLeftLegCutOffThreeInchesBelowTheKneebut that’s just personal preference).Other than that, the only thing I can suggest is a threading problem. There’s nothing wrong with that code per se, but there is a window in which two threads could enter the
ifstatement and both start pushing values into your vector.In other words, if thread 1 is doing the pushing when thread 2 encounters the
ifstatement, thread 2 will also start pushing values, since thread 1 has yet to setdEnemytotrue. And don’t think you can just move the assignment to the top of theifblock – that will reduce but not remove the window.My advice is to print out the contents of the vectors in the situation where they have more than six entries and that may give a clue as to what’s happened (post the output here if you wish).
Re your update that the second
ifbelow is running twice:If this code is executed twice in the same second (and that’s not beyond the bounds of possibility), the second
ifstatement will run twice.That’s because
time(NULL)give you the number of seconds since the epoch so, until that second is over, you may well be executing the contents of thatifthousands of times (or more).