PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

eVC3 - No Device Installed?

No ideas?

The good news is I think I found the problem on the device, and it doesn't look like an iterator problem as I had expected. So I won't need remote debugging to find the problem.

The bad news is that the problem is quite weird. I'm still looking at the code wondering how it could possibly do what it is doing. (And seemingly only on the device.) So maybe remote debugging will be necessary.

Help is appreciated.
I'd start by making sure your cache is big enough. Weird, unexplainable bugs are usually because you're out of cache space, or are writing to memory you shouldn't be writing to.

BTW, sorry to interrupt that conversation you were having with yourself. :)

Dan East
mlepage wrote:I've been developing using eVC3, and I've never been able to make it talk to my iPAQ.
...


Is your device using WM2003?

I have a 1940 with WM2003 and I had some problems to connect it too with eVC3. In fact I haven't been able to :)
The explanation somebody told me is that you can't use a WM2003 device with eVC3 to upload/debug. The explanation didn't convince me, however I haven't been able to debug with it. No problem with eVC4 thought (but you'll have problems with PPC2002 and PPC2000 I think)

Don't take this message as true, just a personal experience which something similar which probably won't help you :)
I've heard there are problems with eVC3 and Windows Mobile 2003, but I am using eVC3 with an iPAQ 3970 which remains at Pocket PC 2002.

Dan, what do you mean by cache size? Are you talking about free program space on the device, or the swap file on desktop Windows, or something else?
I've had problems getting the debug stuff to work...

I was using a "connect as guest", but switched to the sync option and things seem to have improved.

It's seems like active sync is not connected even though it says it is.

Is there a "stay connected" option somewhere? (I'd look, but I'm not at the right computer).
My actual crash is complicated. :-(

It's either a compiler error or a very subtle coding bug. It's one of those problems where adding print statements around the code makes the problem go away, and I'm just setting a couple of values in an array. Everything seems to be in range, and if I remove the print statements it fails, otherwise it's OK. (Oh, and it works fine on the PC.)
Okay, I meant to say stack, not cache. Sorry. :)

I have also seen those really, really odd errors of the exact type you describe. The app crashes, but then when some sort of diagnostic call is placed in the code it starts working. Most recently I saw that behavior when out of memory (heap). Are you allocating large temporary buffers, doing some work with them, then freeing them?

Dan East
mlepage wrote:I've heard there are problems with eVC3 and Windows Mobile 2003, but I am using eVC3 with an iPAQ 3970 which remains at Pocket PC 2002.


I supposed that was not the problem, that why I didn't answer the thread in the first time ;)

good luck with the bug, this kind is my second most hated kind of bug :)
(being the first one absolutelly random bugs which appear once every week)
my advice would be look for unitialized data or buffer overruns, usually in the same or previous function it's giving the problem
The data is initialized. It is a vector of vector of int which is initialized to 4x8 all -1.

Rows 2 and 3 are supposed to always be -1, and they are before a few lines of code in a switch statement are executed. After the switch, a value becomes changed in those lines. The lines in the switch for that case are hardcoded to only touch rows 0 and 1. The other index (0 to 7) is not hardcoded but it is within range. There is a break in each case.

The data is only really touched there. So because my app is single threaded, and it is fine before the lines and not fine after, we know the problem is in those lines. And yet, visually checking the lines reveals no way it could cause the problem.

It happens only on release builds on the device. If I produce a build hardcoded to only take a particular path through the switch statement, one build for each case, they all work fine. If I add a print statement at the end of the case, it works fine.

This leads me to believe that the optimizer is doing something wacky when optimizing the switching statement. I will try other things such as changing it to an if-else statement.

In my experience, optimizer bugs are like this. I had a lot on the DEC Alpha compiler, eventually we had to just disable the optimizer as it was too flaky.
OK if I change the switch statement to an if-else-if statement the optimization problem remains. If I change it to just if statements (no elses) the problem disappears.

The value being switched on is constant and mutually exclusive, so only one case is ever done, even in the multiple if statements.
How about posting the code so we can take a look at it?
Isn't there a #pragma to disable optimizations selectivly?
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.
Unfortunately I am unable to read code on this forum since the last update. All I see is "nbsp; ..." tags.
First thing I noticed was <vector>. I'm a newbie here at Pocket PC programming, so I guess I'll just go ahead and ask the dumb question; I thought STL wasn't supported? If you're using the STL port, then shouldn't that be "vector.h" ?
I now see why you get corruption:

Code:

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;


When you get to this point, nCol is set to 8 and you basically access a vector outside it's range (0..7). This is where your corruption comes from.

Don't be so quick to blame the optimizer... I have yet to see a case where it fails (not saying it's impossible, just highly unlikely).
wyrd wrote:First thing I noticed was <vector>. I'm a newbie here at Pocket PC programming, so I guess I'll just go ahead and ask the dumb question; I thought STL wasn't supported? If you're using the STL port, then shouldn't that be "vector.h" ?


STLport is the only option I know of (and that I also use). In which case it is still <vector>... Why should it be <vector.h> ?
Sorry Thierry, look at the code again. nCol never goes out of range.

The case is the path through the switch. It varies from 0 to 6, but only cases 5 and 6 do anything in this version.

The column varies from 0 to 7. It never goes out of range, because the nextCol function wraps from 7 back down to 0.

I don't see any place where nCol can go beyond 7.

Don't be so quick to blame my code when the optimizer is at fault. :-)

In actual fact, I've seen optimizer bugs on several platforms over 3 years of porting code to 5 flavours of Unix. As I mentioned, the DEC Alpha optimizer, on 64 bit machines, was extremely buggy. So I have seen a variety of optimizer bugs before.

That said, I am not quick to blame the optimizer at all. It never occurred to me that this was an optimizer bug at first. My first hunch was it was an iterator invalidation problem in another area of my code. But when I saw the view display things in places they shouldn't be, I knew the problem was here (where they are created). I went over the code visually and couldn't see the problem, so I began adding log output to track it down. (I couldn't debug because I can't debug remotely and it doesn't appear in debug builds.) Once the log output removed the problem, I knew I had an optimizer error situation. I've seen it many times before.
Doesn't nCol have to equal 8 before it can exit the for loop?

Pam
mlepage wrote:Sorry Thierry, look at the code again. nCol never goes out of range.


Yes it does.


The column varies from 0 to 7. It never goes out of range, because the nextCol function wraps from 7 back down to 0. I don't see any place where nCol can go beyond 7.


It goes up to 8 in the inner for() loop and then the first line under "case 5:" uses it. There is no nextcol() in between.


Don't be so quick to blame my code when the optimizer is at fault. :-)


Who told ya I did it quickly? =)
mlepage wrote:In actual fact, I've seen optimizer bugs on several platforms over 3 years of porting code to 5 flavours of Unix. As I mentioned, the DEC Alpha optimizer, on 64 bit machines, was extremely buggy. So I have seen a variety of optimizer bugs before.


I wouldn't know about other compilers as my experience is pretty much limited to the Microsoft ones. Let me rephrase what I said: I have yet to see a genuine optimizer issue with any Microsoft compiler (although I know they do exist).
Thierry, the nCol in that loop is local to that loop, so it won't effect the function-level nCol.

Chris is taking a look at the html code that wraps my syntactic coloring output. Hopefully he'll have that fixed in a few minutes.

Dan East
Also, for CMyApplication::nextCol, this is nicer:

return (knCol+1) & 7;

Dan East
Dan East wrote:Thierry, the nCol in that loop is local to that loop, so it won't effect the function-level nCol.

Chris is taking a look at the html code that wraps my syntactic coloring output. Hopefully he'll have that fixed in a few minutes.


I blame it on the syntactic coloring =) Can't read it properly.
Dan East wrote:Also, for CMyApplication::nextCol, this is nicer:

return (knCol+1)&0x7f;

Dan East


Nicer how? Quicker?

How about for prevCol (wrapping from 0 to 7)?

This code isn't my biggest performance problem, since it's in the model (which is relatively small and simple) and not the view (which is relatively complex). Still, if it performs better, I'll take it. :-)

Thierry et al: Yes those nCol are local to the loop. Because eVC3 doesn't enforce that, I add the extra braces to create a local scope. Sorry for any confusion that may have caused.
Thierry wrote:

I wouldn't know about other compilers as my experience is pretty much limited to the Microsoft ones. Let me rephrase what I said: I have yet to see a genuine optimizer issue with any Microsoft compiler (although I know they do exist).


Welcome to your first Microsoft optimizer error. :-)

I can provide the modified (from a fresh copy) minimal sample if you want to play with it yourself. I know it's not feasible to copy/paste from the code blocks at this time as they are broken.
mlepage wrote:I can provide the modified (from a fresh copy) minimal sample if you want to play with it yourself. I know it's not feasible to copy/paste from the code blocks at this time as they are broken.


I'd like that as I am myself using stlport a lot and issues like this one concern me more then a little.
Yeah, it's faster, but my earlier post is wrong, and I'll modify it to correct it. The constant should be just 7 instead of 0x7f. That would work for decrementing too:

return (knCol-1)&7;

You can AND in place of modulus by some number that is base 2. Just bitmask by n-1. For example

n % 8 == n & 7;
n % 256 == n & 255;

What you have is better than modulus because division is very slow, but the AND trick is fastest.

As for the optimizer error, try not inlining nextCol and see if it works (or turn it into a macro instead).

Dan East
mlepage wrote:
Code:

#define RAND_INT(x) ((double(rand())/RAND_MAX) * x)


Notice the RAND_INT macro uses rand to choose a number from 0 to (x-1).


Doesn't your RAND_INT macro return something between 0 and x?

As far as I know, rand() can return RAND_MAX in which case you would get 0-8 for nCol (and not 0-7).
Thierry's right:

RAND_MAX
The constant RAND_MAX is the maximum value that can be returned by the rand function. RAND_MAX is defined as the value 0x7fff.


Having to use floating point math to generate a random int is not good performance-wise. You only need to go to that trouble if you want to return random numbers greater than RAND_MAX.

To return a number n that is 0 <= n < max <= (RAND_MAX+1) use this:

#define RAND_INT(max) (rand() % (max))

If max is base 2 then this is faster:

#define RAND_INT(max) (rand() & ((max)-1))

Also, your existing RAND_INT macro can fail in other cases. For example RAND_INT(4+4) will not work properly. You always need to put all macro parameters in parenthesis, as I did above with max.

Dan East
OK, regarding the RAND_INT macro...

I was aware that it could fail under extreme circumstances, both when a strange argument (e.g. 4+4) is used, and when RAND_MAX is returned.

Regarding the first case, it's true that you need to protect arguments and understand how macros evaluate arguments. I didn't here, because I only used the macro here to switch between the built-in random number generator and my own, for debugging this problem.

Regarding the second case, that formula is only used with the built-in random number generator; my own protects against that case. It's all in Stroustrup3e section 22.7. (If you wade through his later code, you'll see it's linear congruential, and it protects against that case.)

Stroustrup gives that formula as yielding adequate results under most circumstances, and my bug isn't that I'm suddenly hitting RAND_MAX within a few calls to rand(). (Consider, if so, why it wouldn't happen in debug builds?) I wonder if Stroustrup may address that failing of that formula in later printings (I have the first printing, I was keenly awaiting that book).

BTW you don't want to simply add 1 to your max compenstate for that failing because that will throw off your uniform random distribution.

The traditional reason for using a formula such as this, and not modulus, is that with linear congruential sequences (which is what rand uses to generate pseudorandom numbers) have not-as-random lower bits (which is what modulus uses). For more info, web search for the online copy of Numerical Recipes in C Second Edition, there is a chapter on random number generation that's very interesting (to nerds like us).

So you are both right, but I am aware of these issues and they exist only in this code for reproducing the problem.

For a limited time, you can get a copy of the project here: http://www.scalenesoftware.com/Minimal- ... -Error.zip
OK regarding the bit-and trick, I see exactly what it's doing, it's just a modulus because it's a power of 2 and we are using binary.

In this project, I only use a few random numbers now and then, so performance isn't an issue. But I'll switch nextCol to it anyways.
Assuming RAND_MAX is always 0x7fff, then try this:

#define RAND_INT(max) ((rand() * (max))>>15)

It can still return 0 <= n <= max, but at least it is all integer math and maintains your linearitarian stuff. :) max has to be <= 65535

Dan East
RAND_INT doesn't exist in my code, I use my own random number generator. It uses the same basic floating-point method, but protects against the case where RAND_MAX is returned.

I only had RAND_INT to switch to the built-in random number generator to debug this problem. BTW switching random number generators had no effect on the optimzer error.

I am not worried about random number generation performance, I use them extremely rarely. At present, I generate about 6 random numbers every 6 seconds or so. If my XScale can't cope with that I'm throwing it in the diaper pail. :-)
You expect a processor to come up with one random number a second? Problem solved, hire me, and I'll yell out numbers for you.
BTW, sorry to interrupt that conversation you were having with yourself.

Dan East


I agree ! LOL
imho the best way to know if it's a compiler fault is just looking at the code generated.
If the code generated is wrong, it'll be shown clearly. If not you'll find clues why it's not doing what it was supposed to do.

I could help if I'd understand the stl but it'd be easy for somebody with arm assembly & stl skills
denthorq wrote:
BTW, sorry to interrupt that conversation you were having with yourself.

Dan East


I agree ! LOL


Yeah, I log the details here. Even if I solve the problem all by myself, at least there's a permanent record of it. Less of a conversation and more of a journal.