longjmp issue

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • CHU Run-min

    #1

    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 .

  • Michael Mair

    #2
    Re: longjmp issue

    CHU Run-min wrote:[color=blue]
    > 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 .[/color]

    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.

    Comment

    • Malcolm

      #3
      Re: longjmp issue

      "CHU Run-min" <churunmin@gmai l.com> wrote[color=blue]
      >
      > 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 .
      >[/color]
      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.


      Comment

      • Jordan Abel

        #4
        Re: longjmp issue

        On 2006-02-05, Malcolm <regniztar@btin ternet.com> wrote:[color=blue]
        > "CHU Run-min" <churunmin@gmai l.com> wrote[color=green]
        >>
        >> 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 .
        >>[/color]
        > 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.[/color]

        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.

        Comment

        • Ben Pfaff

          #5
          Re: longjmp issue

          Jordan Abel <random832@gmai l.com> writes:
          [color=blue]
          > 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.[/color]

          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

          Comment

          • CBFalconer

            #6
            Re: longjmp issue

            Malcolm wrote:[color=blue]
            > "CHU Run-min" <churunmin@gmai l.com> wrote[color=green]
            >>
            >> 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 .[/color]
            >
            > 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.[/color]

            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.c om, 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/googlegroupsrep ly/>


            Comment

            • Michael Mair

              #7
              Re: longjmp issue

              Ben Pfaff wrote:[color=blue]
              > Jordan Abel <random832@gmai l.com> writes:
              >[color=green]
              >>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.[/color]
              >
              > 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.[/color]

              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.

              Comment

              • Malcolm

                #8
                Re: longjmp issue


                "CBFalconer " <cbfalconer@yah oo.com> wrote[color=blue][color=green][color=darkred]
                >>>
                >>> 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 .[/color]
                >>
                >> 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.[/color]
                >
                > 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.
                >[/color]
                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.


                Comment

                • Ben Pfaff

                  #9
                  Re: longjmp issue

                  Michael Mair <Michael.Mair@i nvalid.invalid> writes:
                  [color=blue]
                  > 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?[/color]

                  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:

                  --
                  "I should killfile you where you stand, worthless human." --Kaz

                  Comment

                  • Kenny McCormack

                    #10
                    Re: longjmp issue

                    In article <ds5s9j$ua$3@nw rdmz03.dmz.ncs. ea.ibs-infra.bt.com>,
                    Malcolm <regniztar@btin ternet.com> wrote:
                    ....[color=blue][color=green]
                    >> 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.
                    >>[/color]
                    >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.[/color]

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

                    Comment

                    • Michael Mair

                      #11
                      Re: longjmp issue

                      Ben Pfaff wrote:[color=blue]
                      > Michael Mair <Michael.Mair@i nvalid.invalid> writes:
                      >[color=green]
                      >>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?[/color]
                      >
                      > 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[/color]

                      Thank you very much!


                      Cheers
                      Michael
                      --
                      E-Mail: Mine is an /at/ gmx /dot/ de address.

                      Comment

                      • Vladimir S. Oka

                        #12
                        Re: longjmp issue

                        Malcolm wrote:[color=blue]
                        > 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.[/color]

                        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

                        Comment

                        • Michael Mair

                          #13
                          Re: longjmp issue

                          Vladimir S. Oka wrote:[color=blue]
                          > Malcolm wrote:
                          >[color=green]
                          >>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.[/color]
                          >
                          > Are you suggesting that what actually happens in that third-party
                          > library has no bearing on the question (and the answer)?[/color]

                          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.

                          Comment

                          Working...