Reputation: 171
if(!projectiles.empty()){
sort(projectiles.rbegin(), projectiles.rend()); //Occasionally I get bad sort error. No idea why.
}
projectiles is an std::vector full of projectile structs. These are added each frame if a "fire" command is executed, and removed each frame automatically if they time out.
In projectile:
bool operator < (const projectile& proj) const
{
return (D3DXVec3Dot(&pos, p_camera.GetWorldAhead()) < D3DXVec3Dot(&proj.pos, p_camera.GetWorldAhead()));
}
...pos is a D3DXVECTOR3 with the projectile's position in 3D space - that works (the sorting is for depth checks while alpha blending). For those wondering, D3DXVec3Dot returns a float. But occasionally, especially with lots of projectiles on the screen, it throws:
Debug Assertion Failed!
Program: C:\Windows\system32\MSVCP110D.dll File: e:\applications\vc\include\algorithm Line: 3566
Expression: invalid operator<
For information on how your program can cause an assertion failure, see the Visual C++ documentation on asserts.
Not sure why. Any help is much appreciated.
Upvotes: 0
Views: 2503
Reputation: 525
You can also add simple check if your vector is already sorted before calling sort
. It helped me in my case.
Upvotes: 0
Reputation: 171
I resolved the issue by saving the distance for each struct in its own variable once at the start of the frame, then comparing those - that way, there's no chance for race conditions to show up. Thanks for the tips, guys. :)
Upvotes: 0
Reputation: 129314
Not a proper answer, I know, but it was getting too long for a comment.
The error message indicates that the compare isn't consistent - the sort
function expects same values to keep being sorted in the same way each time. If it detects that two compares of the same value gives opposite results, it will throw this error. I suspect either your comparisons are wrong, or the internal calculations give different results at different times (for example, projectiles are being moved).
Given that you only get it sometimes, it indicates either a race-condition, or a small calculation error that leads to unstable results.
And verify that while you are sorting, the camera or the projectiles are not moving - if either or both are moving during the sort, you will never be able to solve this problem.
Upvotes: 1