Vector out of bounds in windows update

If you found a bug in our library or on our website, please report it in this section. In this forum you can also make concrete suggestions or feature requests.

Moderators: CEGUI MVP, CEGUI Team

earthsruler
Quite a regular
Quite a regular
Posts: 74
Joined: Mon Apr 28, 2008 23:21
Location: Australia

Vector out of bounds in windows update

Postby earthsruler » Thu Jul 17, 2008 00:14

Hey all.

I am destroying windows while CEGUI is firing update events. This was resulting in a crash from vector out of bounds in:

Code: Select all

void Window::update(float elapsed);


The cause is that the size of the vector is being cached, therefore if the size is reduced there will be a crash.

Code: Select all

size_t vectSize = getChildCount();
for (size_t i = 0; i < vectSize; ++i)
   d_children[i]->update(elapsed);


The solution to this is to not cache the variable and call getChildCount() from within the for loop declaration as follows.

Code: Select all

for (size_t i = 0; i < getChildCount(); ++i)
   d_children[i]->update(elapsed);


No more crashes, and with the inline any suitable compiler option selected should be a minimal performance overhead.

ER.

User avatar
CrazyEddie
CEGUI Project Lead
Posts: 6760
Joined: Wed Jan 12, 2005 12:06
Location: England
Contact:

Postby CrazyEddie » Thu Jul 17, 2008 08:28

Thanks for the report. It's now on the list of things to fix ;)

Even with the suggested fix it's still potentially dangerous to be manipulating the collection being iterated over.

I guess the totally correct solution is to iterate over a copy of the original collection, perhaps checking each Window to see if it has been 'destroyed' - the obvious issue with this solution is that it would be much more expensive.

CE.


Return to “Bug Reports, Suggestions, Feature Requests”

Who is online

Users browsing this forum: No registered users and 0 guests