tags:

views:

71

answers:

1

Is there a way to escape the "one big stack" model of Win32 without crippling SEH? I'd like to be able to allocate stack frames on the heap, as a way to implement coroutines. However, my code is currently depending on SEH, and this article, a few pages down, says (relating to traversal of exception handlers, scanning, emphasis mine):

The OS is pretty paranoid about corrupt stacks during this chain traversal. It checks that all chain entries are within the bounds of the stack. (These bounds are also recorded in the TEB). The OS also checks that all entries are in ascending order on the stack. If you violate these rules, the OS will consider the stack to be corrupt and will be unable to process exceptions. This is one of the reasons that a Win32 application cannot break its stack into multiple disjoint segments as an innovative technique for dealing with stack overflow.

So basically, if an exception occurs while the current stack frame is outside the "one big stack", the process will terminate instantly. Not ideal behavior.

Has anyone been able to work around this issue and utilitze SEH with a disjoint stack in a native Win32 app? Also, are there any other Win32-specific "gotchas" with disjoint stacks?

+1  A: 

Fibers were specifically added to Win32 to support this. Start reading here... Check this cautionary blog post as well.

Hans Passant
I considered fibers as a solution, but unfortunately many of the caveats listed in Raymond's blog entry (especially the point about exceptions propogating outside the fiber) make them unusable for me. Also, from what I can tell, they serve a different purpose than coroutines (however similar it may seem), which is where all the limitations arise from. see: http://blogs.msdn.com/larryosterman/archive/2005/01/05/347314.aspx
zildjohn01
Fibers were specifically designed to support coroutines. There is no other mousetrap, maybe you should tweak your requirements to what fibers can give you.
Hans Passant