longjmp issue

C

CHU Run-min

Is it safe to call longjmp from a window procedure to jump to WinMain?


The jump is from wndproc to WinMain , through DispatchMessage.
I'm worried about is there some memory leaks or resource leaks occured
in DispatchMessage.
 
M

Michael Mair

CHU said:
Is it safe to call longjmp from a window procedure to jump to WinMain?

The jump is from wndproc to WinMain , through DispatchMessage.
I'm worried about is there some memory leaks or resource leaks occured
in DispatchMessage.

Sorry, this sounds like gibberish round here; a newsgroup
appropriate to your implementation (platform, os, compiler, ...)
may be a better place to ask for specifics.

What can be told about setjmp/longjmp from the standard
point of view:

,----
| 7.13 Nonlocal jumps <setjmp.h>
|
| 1 The header <setjmp.h> defines the macro setjmp,
| and declares one function and one type, for
| bypassing the normal function call and return
| discipline.207)
+---
| 207) These functions are useful for dealing with
| unusual conditions encountered in a low-level
| function of a program.
+---
|
|
| 2 The type declared is
| jmp_buf
| which is an array type suitable for holding the
| information needed to restore a calling environment.
| The environment of a call to the setjmp macro
| consists of information sufficient for a call to the
| longjmp function to return execution to the correct
| block and invocation of that block, were it called
| recursively. It does not include the state of the
| floating-point status flags, of open files, or of
| any other component of the abstract machine.
|
| 3 It is unspecified whether setjmp is a macro or an
| identifier declared with external linkage. If a macro
| definition is suppressed in order to access an actual
| function, or a program defines an external identifier
| with the name setjmp, the behavior is undefined.
|
`----

So, you can lose open files, have memory leaks even for
auto variables, etc. If this is safe enough for you...


Cheers
Michael
 
M

Malcolm

CHU Run-min said:
Is it safe to call longjmp from a window procedure to jump to WinMain?


The jump is from wndproc to WinMain , through DispatchMessage.
I'm worried about is there some memory leaks or resource leaks occured
in DispatchMessage.
Theoretically it should be OK - really you are asking whether it is OK to
call longjmp() from an indirectly-called routine. It is - setjmp() ought to
save the stack state, even if the subsequent call is via a pointer
However I wouldn't trust the Windows system not to mess up something along
the way - I don't think it is a particularly good idea.
 
J

Jordan Abel

Theoretically it should be OK - really you are asking whether it is OK to
call longjmp() from an indirectly-called routine. It is - setjmp() ought to
save the stack state, even if the subsequent call is via a pointer
However I wouldn't trust the Windows system not to mess up something along
the way - I don't think it is a particularly good idea.

The more on-topic issue is the fact that longjmp can cause resource
leaks - i.e. if a routine that is in the call chain which is "skipped"
by longjmp has allocated a resource, and intends to free it later in the
same block of code

jmp_buf j;

c() { longjmp(j,1); }
b() { void *p = malloc(8); c(); free(); }
a() { int r; if(!(r=setjmp(j))) { b(); } }
main() { a(); }

main calls a; a calls b; b calls malloc; b calls c; c jumps back to a;
p is never freed.

Memory is of course not the only resource that can be leaked - another
example in standard C is open files.
 
B

Ben Pfaff

Jordan Abel said:
The more on-topic issue is the fact that longjmp can cause resource
leaks - i.e. if a routine that is in the call chain which is "skipped"
by longjmp has allocated a resource, and intends to free it later in the
same block of code

jmp_buf j;

c() { longjmp(j,1); }
b() { void *p = malloc(8); c(); free(); }
a() { int r; if(!(r=setjmp(j))) { b(); } }
main() { a(); }

main calls a; a calls b; b calls malloc; b calls c; c jumps back to a;
p is never freed.

longjmp() works nicely with resource "pools", that is, data
structures used to collect references to allocated resources.
Pools require less painstaking labor than manual freeing, make it
easier to reliably release resources, can be implemented in
strictly compliant ANSI C, and because they enable almost
carefree use of longjmp(), they can simplify error handling a
great deal. I used to be wary, but I'm slowly becoming a fan of
longjmp() plus resource pools for error handling for these
reasons.
 
C

CBFalconer

Malcolm said:
Theoretically it should be OK - really you are asking whether it
is OK to call longjmp() from an indirectly-called routine. It is
- setjmp() ought to save the stack state, even if the subsequent
call is via a pointer However I wouldn't trust the Windows system
not to mess up something along the way - I don't think it is a
particularly good idea.

Please do not answer off-topic questions here, other than to
redirect them to a suitable newsgroup, if known. There is nobody
here to correct any possibly erroneious answers.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
 
M

Michael Mair

Ben said:
longjmp() works nicely with resource "pools", that is, data
structures used to collect references to allocated resources.
Pools require less painstaking labor than manual freeing, make it
easier to reliably release resources, can be implemented in
strictly compliant ANSI C, and because they enable almost
carefree use of longjmp(), they can simplify error handling a
great deal. I used to be wary, but I'm slowly becoming a fan of
longjmp() plus resource pools for error handling for these
reasons.

Could you please elaborate a little bit more on the techniques
used or point me toward a site or thread giving more detail?
At the moment I am not sure what exactly is covered in which
way by these "resource pools"; do you think of completely
carefree ressource handling (say, atexit() some kind of cleanup
function) or smart pointer like?


Cheers
Michael
 
B

Ben Pfaff

Michael Mair said:
Could you please elaborate a little bit more on the techniques
used or point me toward a site or thread giving more detail?
At the moment I am not sure what exactly is covered in which
way by these "resource pools"; do you think of completely
carefree ressource handling (say, atexit() some kind of cleanup
function) or smart pointer like?

Here's a link to a page that describes what I have in mind pretty
well. It doesn't talk about pools in the presence of longjmp(),
but I'm not sure that that is in widespread use:
http://svnbook.red-bean.com/en/1.1/ch08s05.html
 
M

Malcolm

CBFalconer said:
Please do not answer off-topic questions here, other than to
redirect them to a suitable newsgroup, if known. There is nobody
here to correct any possibly erroneious answers.
A question doesn't become non-topical just because Mr Gates' operating
system is mentioned.
Bascially he asked, should an indirect call through a third party library
mess up longjmp? Perfectly on-topic.
 
K

Kenny McCormack

Please do not answer off-topic questions here, other than to redirect
them to a suitable newsgroup, if known. There is nobody here to correct
any possibly erroneious answers.
A question doesn't become non-topical just because Mr Gates' operating
system is mentioned. Bascially he asked, should an indirect call through
a third party library mess up longjmp? Perfectly on-topic. [/QUOTE]

You are asking for logic from people who are deep into a mystical religion.
 
V

Vladimir S. Oka

Malcolm said:
A question doesn't become non-topical just because Mr Gates' operating
system is mentioned.
Bascially he asked, should an indirect call through a third party
library mess up longjmp? Perfectly on-topic.

Are you suggesting that what actually happens in that third-party
library has no bearing on the question (and the answer)?

Vladimir
 
M

Michael Mair

Vladimir said:
Are you suggesting that what actually happens in that third-party
library has no bearing on the question (and the answer)?

In order to determine the general workings and effects of
setjmp()/longjmp() as guaranteed by the standard: Yes.
This much is on topic and deserves an answer. There may be
enough reasons for the OP to base his decision on this.
Connecting this knowledge to the actual implementation and
third-party libraries might have better been accompanied
by a direction to the appropriate newsgroup but as long as
this does not degrade into a completely off-topic
discussion...

Cheers
Michael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
474,175
Messages
2,570,942
Members
47,489
Latest member
BrigidaD91

Latest Threads

Top