PocketMatrix forums

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

Stack

I've had more problems with the stack than probably any other area of Windows CE. Most of the time for small apps the stack is not an issue. However for more complex apps that consume more of the stack real headaches can occur. One of the biggest issues is that you have to specify the stack size manually. If you get to the point your stack is not large enough, then some of the most bizarre behavior occurs during runtime that is extremely hard to diagnose. With VC++ 6.0 it is smart enough to figure out how big the stack needs to be on its own.

Now I seem to be having another problem. It seems if the stack size I specify is too large, then I have bizarre runtime problems even though I'm not running out of space. I'm trying to use a 2 MB stack. My hunch is the the OS cannot give me 2 MB stack space, yet my app is using all of the 2 MB even though I don't use that much at one time. So when it accesses past what the OS allocated weird things happen. Does that sound realistic?

Anyone know of what the maximum stack size is for CE? Anyone know of a method to somehow have CE gracefully report when the app runs out of stack space?

Any insight would be greatly appreciated.

Dan East
I'm not sure the direct answer to your question Dan, but I recall something about 32MB total for all stack space across all processes. Is it possible that your 2MB allocation is overrunning THAT limit, even if 2MB would otherwise be OK?

As for gracefully reporting stack out of space, I'm very surprised it's not throwing a system access violation. I believe that's what's supposed to happen when stack space is exceeded (I'm trying to remember some details that I caught some months ago looking through TechNet docs, so I may be way off in this). I know the OS should terminate your app, I'd bet that's happening, I'm just wondering if there's a way to trap the fault, maybe even with an external app. At least then you'd know it was a stack out of space issue.
I've had this problem as well.

I had an array of wchar_t [10*1024] and my app would randomly crash. I changed the declaration from:

wchar_t buffer[10*1024]; to

malloc (sizeof(wchar_t) * 10 * 1024)

which seems to have solved the problem.
fzammetti wrote:
As for gracefully reporting stack out of space, I'm very surprised it's not throwing a system access violation. I believe that's what's supposed to happen when stack space is exceeded


I've never seen it gracefully handle running out of stack space. Now I guess I'm talking about two things here. One is if your app is set for a certain amount of stack space, and that cannot be allocated when the thread is created. I would certainly hope the OS would report that. The other elusive problem is when your app has say a 1 MB stack, and it steps into a function that takes it over that limit. In that case weird things can start happening. Like global vars being overwritten, etc. Again, it's the type of bizarre behavior that is really hard to debug. So I'm to the point that I automatically check the stack size anytime I see something I cannot account for otherwise.

Dan East
I've encountered similar problems a few times (2-3) and each time I would suspect a stack overflow. Each time it turned out that the problem was something else: I was trashing the stack by exceeding array boundaries.

For example, declaring an array of 256 chars in a function and writting past that array. Sometimes the program would keep running for a while and sudently behave weirdly and a stack trace would return garbage.

Stack overflows do exist, but I haven't experienced one since the 16-bits DOS days.
I agree with Thierry, many of the issue I have had which seemed to be memory allocation issues turned out to be other things. In my case allignment caused by using __int8 to save space for small variables. Now I never leave odd bytes around in variable definitions.
Datatype misalignment issues can be very common - Warlords II had a ton of them crop up. And they cause some of the goofiest bugs and crash issues you could ever imagine.

Sometimes you get lucky and eVC will tell you there is a Datatype Alignment error (if you are debugging directly on the device) but for the most part you'll need to dissect your structs and either align them by padding or #pragma pack'ing them.

I can't say for sure that allocating a 2MB block of memory won't fail (as I've never tried it), but if new or free doesn't fail then its a pretty good bet its allocating it properly. Warlords II has some pretty healthy chunks of memory allocated at startup, but certainly not 2MB continguous.

Dan, you're not allocating this as a global variable are you? I found serious problems using large data chunks as globals on the PPC and they involved running into memory limitations on the devices (I don't remember offhand what the problem was, but I may have written it down somewhere - I'll look today).
I guess the issue lays in the way WindowsCE manages its memory. Each user process has a max of 32MB (excluding the new dll? region added in 2003), and each block is first broken down into 1MB then 64KB or 4KB regions in the page table. Now what likely MS has done is to allocate a single 1MB page for your application's stack. (I'm not sure what section of the virtual address space at this moment. Anyone feel like posting any value of the stack pointer?) Also to add to the mess of it, MS uses a full decending stack.. ie. going from higher to lower addresses in memory, so if you go below the 1MB page boundary you are either overwriting another programs data or your own.

How to fix this.
a) allocate your own stack ( optionally placing a protected page before it to trigger a page-fault/access-violdation when the boundary is crossed. )
b) be carefull about what you put on the stack ie. huge arrays, massively recursive functions. The last thing you want to do is put large data structures on the stack, as you will get very little performance benifit.
Also to add to the mess of it, MS uses a full decending stack..


Actually that's sensible. If you put the stack at the end of your process address space and have it move down through memory, it's far easier to manage than having both your stack and your heap memory moving up through memory space.