PocketMatrix forums

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

Exception 0x80000002

I am having troubles with my game running on Dell Axim X5 devices, getting access violations like 0x80000002 address:0002042c. The address changes. Done some research, and it seems the error is related to dword boundaries or stack sizes. Has anyone delt with this before?

My stack settings are currently:
Reserve: 0x10000, Commit: 0x1000
I don't really understand what they mean, but the above address may indicate that the stack is too small, although I'm not convinced about that because I would expect to get a stack error instead. What if I just double to 0x20000 etc?

If the problem is dword boundary related where do I start looking in my code for the problem? For instance, only dword data types? Does signed/unsigned make a difference? Is it only certain types of memory access?

One thing that may be related is my generic list class that I inherit from to build list objects uses HeapAlloc() - could that have something to do with it?

I don't have an X5 to test on.
It is my understanding that 0x80000002 is only for data type misalignment. Here is what that means...

a 32 bit access (read or write) must occur on an address where the lower 2 bits are 0.
a 16 bit access (read or write) must occur on an address where the lowest bit is 0.

ie;
Code:

*(long *)0xa0003=0; //fault
*(long *)0xa0002=0; //fault
*(long *)0xa0001=0; //fault
*(long *)0xa0000=0; //ok

*(short *)0xa0001=0; //fault
*(short *)0xa0000=0; //ok

it doesn't matter if it is unsigned or signed.

A typical screw up is something like this...

char buf[1000];
unsigned long *src;

src=(unsigned long *)&buf[3];
*src=0; //fault

I don't know of any reason why you would get this problem on a Dell vs. another PPC. You might check your frame buffer access code as the Dell does have a different memory layout vs. some other devices.

One tricky thing to watch out for... I've seen a misalignment error before than was actually just a bad pointer. My 'access' code was fine, it just got a bogus pointer. Example... a memory alloc failure caused a piece of code to short-circuit, not setting another pointer. When the pointer was later used, it was garbage and just happened to be an 'odd' value that triggered the misalignment exception. While it *was* an alignment exception, it was really just a pointer that didn't get initialized properly.

hope that helps!

cheers,

rcp
We are 'in the same boat' in that we have similar errors and are using the Axim but knowing that I have cleared previous problems it's likely not to be the Axim.

The earlier problems were likely to be as described above. Memory allocation forcing a false allignment error.

A possible clue was that one earlier error of this type went away when I changed some global variables to be local.

nb: In my case the PC build always works only the PPC one gives errors

Question: how does one find out if a NEW fails ??
if(sizeof(your.exe)>64KB)
{
0002042c is within your application
}
else
{
0002042c is probably the heap (possibly the stack)
}

Reserve: 0x10000, Commit: 0x1000


My guess would be that the "reserve" is the amount of virtual memory to allocate for the stack, and the "commit" is how much to start the application off with (i.e. a 4KB stack in this case). Check the help if you want more than a guess :) (I don't have it installed here).

<a href="http://support.microsoft.com/default.aspx?scid=kb;%5BLN%5D;326162">This</a> and <a href="http://support.microsoft.com/default.aspx?kbid=326164">this</a> may also be useful information for you.

nb: In my case the PC build always works only the PPC one gives errors


The X86 handles unaligned data access. ARMs don't (usually).
Thanks for all your help. One suspicious part of my code I found that could be related is:

Code:

#pragma pack(push,_loader_types)
#pragma pack(1)
//... data types that will be used to read from a file, eg:
typedef struct tag_TLight
{
TFixedPoint Ambience;
_TVector Position;

} _TLight;
#pragma pack(pop,_loader_types)


I was only guessing how to use pragma pack, but have since found that it probably should be used like:

Code:

#pragma pack(1)
//...
#pragma pack()


In the first example above would that actually reset the byte alignment to the default, or did I change it to align on byte and not set it back correctly? I hope so, because that could explain my problem.
Packing was discussed <a href="http://www.pocketmatrix.com/forums/viewtopic.php?t=15381">here</a>.

Packing to one byte is probably what's screwing you up. If you have a structure like this:

Code:
struct broken {
unsigned char x;
unsigned short y;
unsigned char z;
}


packed to one byte, then it will take only 4 bytes in memory (actually 8 by default, see later) but accessing the short in the middle will fail with the alignment error. In that case packing to two bytes would make the problem go away.

In this example you could also just rearrange the elements in the structure:
Code:
struct not_broken {
unsigned short y;
unsigned char x;
unsigned char z;
}


... that'd work fine as the short is at an even offset (zero).

A structure like this:
Code:
struct broken2 {
unsigned short x;
unsigned long y;
unsigned short z;
}


would need packing to 4 bytes, otherwise y is not 4-byte aligned and can't be correctly loaded by the bus. Again, rearranging (y,x,z) it would fix it without messing around with packing.

Note that as mentioned in the other thread, the structures themselves are padded to 8-byte boundaries by default anyway so unless you change that you're not losing any more memory to the alignment. I.e. the broken structure is 4 bytes above but it's packed to 8 as a unit. Modifying it to fix the alignment only adds 1 byte after x, and as it's less than the 8 it'll use, you lose nothing.
Thanks refractor. So, it doesn't just apply to dwords - when making structures it's important to have the items aligned on the same boundary as the length of the item - ie. short aligned to 2 bytes, char to 1 byte (although doesn't matter), int to 4 bytes?

The reason why I wanted everything bunched up is because I wanted the file size as small as possible, but given the above that doesn't seem possible (unless the items naturally lend themselves to a re-arrangement suiting correct boundaries as in your example).

I'll update my game and see if that works. Thanks everyone for your help.
What I've started doing is using two different structs for the same data. One is for file serialization, the other is the "working" struct used programatically to actually access the data. So after I read in the packed file struct, I then fill in the working struct from the file data. You can write your own "safe" routines to access misaligned data to get to and from the file structs (just copy everying one byte at a time).

This has three advantages. It allows you to pack data tight to reduce the size of the data files. The endianess of your data files can be different from your working structs (which has to match the CPU's endianess). Finally and most importantly, it allows you to make any future modifications you need to your working struct without breaking compatibility with your existing data files. Say for example you find you can optimize your working struct by changing the order of the elements (so elements used together can all fit in a cache line at once), you are free to rearrange them and it has no affect on your file system.

Dan East
Dan, thanks for those comments - that makes sense.

By the way, I've fixed the problem now, and here are some comments that will hopefully help others who run into the same problem:

Dell Axims are very sensitive devices, but it's not that it's their fault my game didn't work....

There were two problems:
- Data alignment issues
- Memory leaks

Any memory leak will cause problems on the Axim, but almost all other devices will handle it without any trouble if the memory leak occurs in the HeapAlloc() area. The memory leak was from my not knowing exactly how virtual functions work when called from destructors - I had to do a manual work-around in the inheriting class.

I had created a base list management class that allocates in blocks using HeapAlloc and has basic list functionality as follows:

Code:

class TList
{
int Add(void* ptr);
virtual BOOL Remove(int Index);
void* Item(int Index);
virtual void Clear();
//...
};


and then all of my lists inherit from TList eg:

Code:

class TRegionList : public TList
{
int Add(TRegion *o);
virtual BOOL Remove(int Index);
virtual void Clear();
};


in the above example the virtual function Remove() frees the memory associated with the TRegion item:

Code:

BOOL TRegionList::Remove(int Index)
{
//free object
TRegion *o = (TRegion*)TList::Item(Index);
delete o;

//remove from list
return TList::Remove(Index);
}


Where I was running into trouble was in the destructor of the base TList class I call Clear():

Code:

~TList()
{
Clear();
HeapFree(GetProcessHeap(), 0, FItems);
}


And I assumed that as Clear() was a virtual function it would call the appropriate Clear() eg. TRegionList::Clear() which does the correct freeing of memory. But what I found is that it actually calls the TList::Clear() instead - which was meaning every item in all of my list structures weren't cleaning up after themselves.

This doesn't make sense, as I have other examples where virtual functions behave exactly as expected - it seems that it doesn't get it right only in the destructor. Can anyone explain this? (I can post full code if that will help)

The 80000002 exception is supposed to be related to data alignment, but I think it must also get raised on the Axims in the event of memory leaks.

However, I adjusted my structures so they were aligned correctly (before seeing Dan's post) so I just added some padding from the Delphi app which I use to generate the data file, and removed the #pragma pack(1) and allowed it to use default packing.

Once again, thanks for all the help.
By the way, my work around was to manually call Clear() in the destructor of the inheriting classes eg:

Code:

~TRegionList()
{
Clear();
}
That is normal behavior. Say you have class C derived from class B derived from class A.

When you destroy the object, the following destructors will be called in order:
C::~C()
B::~B()
A::~A()

By the time you reach ~A(), your object isn't a B or C anymore, it is just an A. So there is no virtual function to call except A::Method().

To prevent problem like this, it is good practice to never call a virtual function in a constructor or a destructor.
Makes perfect sense Thierry. I feel a bit stupid now. :oops: