I did even better, I took 20mins to reproduce the problem in a fresh copy of the minimal sample program from GapiDraw 2.05eval. To do so, make a fresh copy, and make the modifications below.
First, at the top of myapplication.h, include an STL file after GapiApplication.h:
- Code:
// TEST
#include <vector>
Next, at the bottom of class CMyApplication, add some private members:
- Code:
// TEST
private:
int nextCol(const int knCol) const;
void test();
std::vector<std::vector<int> > m_v;
bool bBad;
Finally, below class CMyApplication, add an inline definition of nextCol:
- Code:
// TEST
inline
int
CMyApplication::nextCol(
const int knCol) const
{
return knCol == 7 ? 0 : knCol + 1;
}
Now, make the following changes to myapplication.cpp. First, initialize the data members in the constructor:
- Code:
// TEST
m_v.resize(4, std::vector<int>(8, -1));
bBad = false;
Notice that the vector of vector of int, 4x8, is allocated and cleared to -1.
Next, change the code in ProcessFrame where it clears the back buffer. Now, it will call our test function, and use the colour red if corruption has occurred.
- Code:
// TEST
test();
if (bBad)
{
// Draw the back buffer red
backbuffer.FillRect(NULL, RGB(255, 0, 0), 0, NULL);
}
else
{
// Clear the back buffer
backbuffer.FillRect(NULL, RGB(128, 128, 128), 0, NULL);
}
Finally, add our test function to the bottom of the file.
- Code:
// TEST
#define RAND_INT(x) ((double(rand())/RAND_MAX) * x)
void CMyApplication::test()
{
int nCol = RAND_INT(8);
const int knCase = RAND_INT(7);
// Test for corruption before
{
for (int nRow = 2; nRow != 4; ++nRow)
{
for (int nCol = 0; nCol != 8; ++nCol)
{
if (m_v[nRow][nCol] != -1)
{
bBad = true;
}
}
}
}
// Switch that optimizer corrupts
switch (knCase)
{
case 5:
m_v[1][nCol] = (3);
nCol = nextCol(nCol);
m_v[0][nCol] = (3);
m_v[1][nCol] = (3);
nCol = nextCol(nCol);
m_v[0][nCol] = (3);
break;
case 6:
m_v[0][nCol] = (3);
m_v[1][nCol] = (3);
nCol = nextCol(nCol);
m_v[0][nCol] = (3);
m_v[1][nCol] = (3);
break;
}
// Test for corruption after
{
for (int nRow = 2; nRow != 4; ++nRow)
{
for (int nCol = 0; nCol != 8; ++nCol)
{
if (m_v[nRow][nCol] != -1)
{
bBad = true;
}
}
}
}
}
Notice the RAND_INT macro uses rand to choose a number from 0 to (x-1). We pick a column (which wraps using our nextCol function) and a case.
Then we do a test to ensure rows 2 and 3 are still all -1.
Then we do our switch statement. Only these two cases are necessary to trigger the optimizer problem. These two cases are the only place we ever set any values in our data. Those places are hardcoded to only set in rows 0 and 1. They use the column we chose, advancing it a few columns using our nextCol function. So our indices are in range.
Finally we do another test to ensure rows 2 and 3 are still all -1.
What you will find, running this program on your Pocket PC, built for release, is that the background starts grey but almost immediately switches to red. (Maybe reduce the FPS to see this more clearly.)
The data starts out fine. Then at one point, it passes the first test, sets the values using case 6, and then fails the test. Clearly, case 6 corrupted the data, and clearly it is not a coding error but an optimizer error.
It doesn't happen in debug builds for the Pocket PC, and it doesn't happen at all on the PC. Fiddling with the code can eliminate the error (e.g., using if statements instead of a switch, or changing the code to always use case 6).
Anyways, there you have it. I'm not too worried about this particular problem as I can solve it by using if statements. But it makes me wonder what other problems the optimizer has introduced into my release builds.