473,326 Members | 2,076 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

longjmp issue

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.

Feb 5 '06 #1
12 1624
CHU Run-min wrote:
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
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Feb 5 '06 #2
"CHU Run-min" <ch*******@gmail.com> wrote

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.
Feb 5 '06 #3
On 2006-02-05, Malcolm <re*******@btinternet.com> wrote:
"CHU Run-min" <ch*******@gmail.com> wrote

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.


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.
Feb 5 '06 #4
Jordan Abel <ra*******@gmail.com> writes:
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.
--
"Your correction is 100% correct and 0% helpful. Well done!"
--Richard Heathfield
Feb 5 '06 #5
Malcolm wrote:
"CHU Run-min" <ch*******@gmail.com> wrote

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.


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/>
Feb 5 '06 #6
Ben Pfaff wrote:
Jordan Abel <ra*******@gmail.com> writes:
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.


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
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Feb 5 '06 #7

"CBFalconer" <cb********@yahoo.com> wrote

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.


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.
Feb 5 '06 #8
Michael Mair <Mi**********@invalid.invalid> writes:
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
--
"I should killfile you where you stand, worthless human." --Kaz
Feb 5 '06 #9
In article <ds*********@nwrdmz03.dmz.ncs.ea.ibs-infra.bt.com>,
Malcolm <re*******@btinternet.com> wrote:
....
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.


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

Feb 5 '06 #10
Ben Pfaff wrote:
Michael Mair <Mi**********@invalid.invalid> writes:
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


Thank you very much!
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Feb 5 '06 #11
Malcolm wrote:
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

--
"No, `Eureka' is Greek for `This bath is too hot.'"
-- Dr. Who

Feb 5 '06 #12
Vladimir S. Oka wrote:
Malcolm wrote:
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)?


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
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Feb 6 '06 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Thomas Baruchel | last post by:
Hi, wondering about: func1: setjmp() ; func2(); func2: {FILE *f; f = fopen(); func3(); fclose(f)} func3 : if() longjmp; else return; Note that FILE *fis a local variable in func2.
22
by: Nimmi Srivastav | last post by:
Can someone kindly clarify the distinction between long jumps and gotos? Why is one frowned upon and not the other? Is there really any situation where use of longjmp becomes inevitable? A...
12
by: Michael B Allen | last post by:
Should setjmp/longjmp really be used in a fast mundane ANSI C piece of code? Or is it frowned apon like goto? I have a need but I don't want to use something that is costly, isn't supported...
2
by: Ravi Uday | last post by:
Hi, Can anyone explain me why do we use setjmp and longjump functions. I read through the manual pages/doc but wasnt able to get a clear picture of the same. Any small example illustrating...
4
by: Jrferguson | last post by:
I have a C program that I am trying to port to a Motorola 68k based system. It makes use of setjmp and longjmp which are not supported by my C compiler. I understand the general principle behind...
2
by: Jerald Fijerald | last post by:
Hello. I'm trying to find an elegant way to longjmp to the callee (aka "generators for C") So far I've come to this experimental program (gcc only as it uses __builtin_frame_address, although...
8
by: Zheng Da | last post by:
I wrote a simple one as follow: typedef struct __myjmp_buf { int efp; int epc; }myjmp_buf; int mysetjmp(myjmp_buf env) {
6
by: Clausfor | last post by:
Hello, I have a problem with restoring variables in the setjmp/longjmp functions: K&R2 for longjmp says: "Accessible objects have the same value they had when longjmp was called, except for...
3
by: no_click_there | last post by:
Hi, I'm learning to use the setjmp/longjmp functions and still can't really grasp what's going on here. I'd basically like to be able to jump back and forth from (or to) two given functions. ...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.