memory leak with a thrown exception

Discussion in 'C++' started by rupert, Nov 7, 2006.

  1. rupert

    rupert Guest

    hello; does anybody know why i'd get a memory loss at this point?

    Code:
    
    #include <iostream>
    
    
    int main(int argc, char *argv[]){
        try {
           throw 1;
        }
        catch(int) {
           cout<<"Catch\n";
        }    
        return EXIT_SUCCESS;
    }
    
    
    rupert, Nov 7, 2006
    #1
    1. Advertising

  2. * rupert:
    > hello; does anybody know why i'd get a memory loss at this point?


    It happens to everybody. Just be honest about it.


    >
    Code:
    > 
    > #include <iostream>
    > 
    > 
    > int main(int argc, char *argv[]){
    >     try {
    >        throw 1;
    >     }
    >     catch(int) {
    >        cout<<"Catch\n";[/color]
    
    Should not compile, should be
    
       std::cout << "Catch\n";
    
    To ensure that that output appears it would also be a good idea to do
    
       std::cout << std::flush;
    
    Which you can combine with the above by doing
    
       std::cout << "Catch" << std::endl;
    
    [color=blue]
    >     }    
    >     return EXIT_SUCCESS;[/color]
    
    This constant is not guaranteed to be available by including <iostream>.
    
    [color=blue]
    > }
    > 
    > 



    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Nov 7, 2006
    #2
    1. Advertising

  3. rupert

    rupert Guest

    Sorry correction;

    Code:
    
    #include <map>
    #include <iostream>
    
    using std::cout;
    
    int main(int argc, char *argv[])
    {
        try
        {
           throw 1;
        }
        catch(int)
        {
          cout<<"Catch\n";
         //std::flush;
        }
        return 0;
    }
    
    
    sorry try again. seems even with std::flush i still get a memory leak
    rupert, Nov 8, 2006
    #3
  4. * rupert:
    > Sorry correction;
    >
    >
    Code:
    > 
    > #include <map>
    > #include <iostream>
    > 
    > using std::cout;
    > 
    > int main(int argc, char *argv[])
    > {
    >     try
    >     {
    >        throw 1;
    >     }
    >     catch(int)
    >     {
    >       cout<<"Catch\n";
    >      //std::flush;
    >     }
    >     return 0;
    > }
    > 
    > 
    >
    > sorry try again. seems even with std::flush i still get a memory leak


    It's unclear what you mean by "memory leak".

    Do you mean that no output appears?

    Try running the program from the command line (if you haven't).

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Nov 8, 2006
    #4
  5. rupert

    rupert Guest

    Oh the program compiles and runs. however bcheck ./myProg displays one
    occurance of a memory leak. and can't figure out why; when there's no
    initialization of any "new" things nor pointers to dynamically
    allocated objects, simply a "throw" confusing eh?....
    rupert, Nov 8, 2006
    #5
  6. rupert

    BobR Guest

    Alf P. Steinbach wrote in message <>...
    >* rupert:
    >> Sorry correction;
    >>
    Code:
    >>
    >> #include <map>
    >> #include <iostream>
    >> using std::cout;
    >>
    >> int main(int argc, char *argv[]){
    >>     try{
    >>        throw 1;
    >>        }
    >>     catch(int){
    >>       cout<<"Catch\n";
    >>      //std::flush;[/color][/color]
    
    OP: That should be 'std::cout<<std::flush;'.
    [color=blue][color=green]
    >>     }
    >>     return 0;
    >> }
    >>
    >> 
    >>
    >> sorry try again. seems even with std::flush i still get a memory leak

    >
    >It's unclear what you mean by "memory leak".


    Fool, Alf! (<G>) Can't you see, it's right in front of you. He said, " **i**
    still get a memory leak" [1]. Didn't say anything about the program!
    (....or the std::map use.)

    [1] - I get that all the time with CRS and Sometimers disease! (....in fact,
    I think it's happening right now!)
    --
    Bob <G> R
    POVrookie
    BobR, Nov 8, 2006
    #6
  7. rupert

    BobR Guest

    rupert wrote in message
    <>...
    >Oh the program compiles and runs. however bcheck ./myProg displays one
    >occurance of a memory leak. and can't figure out why; when there's no
    >initialization of any "new" things nor pointers to dynamically
    >allocated objects, simply a "throw" confusing eh?....
    >


    May be a false positive. As soon as you throw, it exits the try{}
    (immediately, does not finish it), and starts looking for a handler. Maybe
    'bcheck' is keying off the missing closing brace (due to the throw).

    Is there anything else in main()?

    --
    Bob R
    POVrookie
    BobR, Nov 8, 2006
    #7
  8. rupert

    rupert Guest

    Easy bob, my question to was unclear to Alf, who's the only person
    helping me at the moment. It will teach me to be ask better questions.
    rupert, Nov 8, 2006
    #8
  9. rupert

    rupert Guest

    > Is there anything else in main()?
    no that's it.
    rupert, Nov 8, 2006
    #9
  10. On 7 Nov 2006 19:20:11 -0800, "rupert" wrote:
    >Oh the program compiles and runs. however bcheck ./myProg displays one
    >occurance of a memory leak. and can't figure out why; when there's no
    >initialization of any "new" things nor pointers to dynamically
    >allocated objects, simply a "throw" confusing eh?....


    The leak probably stems from iostreams. Try to leave out cout (don't
    #include <iostream>).

    Best wishes,
    Roland Pibinger
    Roland Pibinger, Nov 8, 2006
    #10
  11. rupert

    VJ Guest

    rupert wrote:
    > Sorry correction;
    >
    >
    Code:
    > 
    > #include <map>
    > #include <iostream>
    > 
    > using std::cout;
    > 
    > int main(int argc, char *argv[])
    > {
    >     try
    >     {
    >        throw 1;
    >     }
    >     catch(int)
    >     {
    >       cout<<"Catch\n";
    >      //std::flush;
    >     }
    >     return 0;
    > }
    > 
    > 
    >
    > sorry try again. seems even with std::flush i still get a memory leak
    >



    It might be a false report in your memory leak check program. I
    encountered such using valgrind in various cases (one case was a memory
    leak report when an exception throws)
    VJ, Nov 8, 2006
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Selen
    Replies:
    0
    Views:
    2,674
    Selen
    May 28, 2004
  2. =?Utf-8?B?VmFs?=
    Replies:
    0
    Views:
    3,120
    =?Utf-8?B?VmFs?=
    Jun 8, 2005
  3. Bikash
    Replies:
    5
    Views:
    431
    Daniel T.
    Jul 22, 2004
  4. jobs
    Replies:
    1
    Views:
    1,875
    Scott Roberts
    Nov 16, 2007
  5. Val
    Replies:
    0
    Views:
    173
Loading...

Share This Page