On 2010-12-02, Paul Rubin <no.em...@nospam.invalid> wrote: > Tim Harig <user...@ilthio.net> writes: >> I am not talking about what setjmp() has to do, I am talking about what >> *you* have to do after setjmp() returns. If you have allocated memory in >> intermediate functions and you don't have a reference to them outside of >> the functions that longjmp() bypasses from returning properly (and thus >> either not clearning data structures or returning a reference to those data >> structures as it normally would) then you have potential memory leaks, >> dangling pointers, etc. > > Sure, that's what the aux stack is for--you put any such references into > it, for the setjmp handler to find later. You do that BEFORE setjmp > returns, of course.
If you miss something, you are in trouble. There is a concept of variable life that is measured by how many lines separate the use of variable from its first use to its last. By using setjmp/longjmp, you effectively extend the life of these variables, potentially through several files, to at least as long as the jump. If there are several function calls in depth, there may be quite a lot of space that you have to check to make sure that you have not missed anything. >> I am not saying that this cannot be done. What I am saying is that it >> is inherently error prone. > > I suppose so, but so is everything else in C. On the overall scale of > C-related hazards, this particular one isn't so bad if you code in a > consistent style and are disciplined about recording the cleanups. > > You could also use something like an obstack, which is a stack allocated > on the heap, so it persists after the control stack returns, but you can > release the whole thing in one operation. By working the error back up through the call stack, you can keep track of the variables and allocations in each function isolated to that function. The smaller each function is, the easier and less error prone it will be to theck it is to check. That makes it much easier to make sure that you have not missed anything. Essentially, you can validate that each function correctly handles is allocations rather then having to validate the setjmp/longjmp structure as a whole. To use Joe Armstrong's phrase, "it makes the impossible merely difficult." Back to the topic, by using Python with its exceptions and garbage collection, all of this is a moot point. -- http://mail.python.org/mailman/listinfo/python-list