By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,159 Members | 888 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,159 IT Pros & Developers. It's quick & easy.

memory leak with a thrown exception

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

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <iostream>
  3.  
  4.  
  5. int main(int argc, char *argv[]){
  6. try {
  7. throw 1;
  8. }
  9. catch(int) {
  10. cout<<"Catch\n";
  11. }
  12. return EXIT_SUCCESS;
  13. }
  14.  
  15.  
Nov 7 '06 #1
Share this Question
Share on Google+
10 Replies


P: n/a
* rupert:
hello; does anybody know why i'd get a memory loss at this point?
It happens to everybody. Just be honest about it.

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. int main(int argc, char *argv[]){
  3.     try {
  4.        throw 1;
  5.     }
  6.     catch(int) {
  7.        cout<<"Catch\n";
Expand|Select|Wrap|Line Numbers
  1.  
  2. Should not compile, should be
  3.  
  4. std::cout << "Catch\n";
  5.  
  6. To ensure that that output appears it would also be a good idea to do
  7.  
  8. std::cout << std::flush;
  9.  
  10. Which you can combine with the above by doing
  11.  
  12. std::cout << "Catch" << std::endl;
  13.  
  14.  
  15.         
  16.                     }
  17.     return EXIT_SUCCESS;
  18.  
  19. This constant is not guaranteed to be available by including <iostream>.
  20.  
  21.  
  22.         
  23.                 }
  24.  
  25.  

--
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?
Nov 7 '06 #2

P: n/a
Sorry correction;

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <map>
  3. #include <iostream>
  4.  
  5. using std::cout;
  6.  
  7. int main(int argc, char *argv[])
  8. {
  9. try
  10. {
  11. throw 1;
  12. }
  13. catch(int)
  14. {
  15. cout<<"Catch\n";
  16. //std::flush;
  17. }
  18. return 0;
  19. }
  20.  
  21.  
sorry try again. seems even with std::flush i still get a memory leak

Nov 7 '06 #3

P: n/a
* rupert:
Sorry correction;

Expand|Select|Wrap|Line Numbers
  1. #include <map>
  2. #include <iostream>
  3. using std::cout;
  4. int main(int argc, char *argv[])
  5. {
  6.     try
  7.     {
  8.        throw 1;
  9.     }
  10.     catch(int)
  11.     {
  12.       cout<<"Catch\n";
  13.      //std::flush;
  14.     }
  15.     return 0;
  16. }
  17.  

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?
Nov 7 '06 #4

P: n/a
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?....

Nov 8 '06 #5

P: n/a

Alf P. Steinbach wrote in message <4r************@mid.individual.net>...
>* rupert:
>Sorry correction;
Expand|Select|Wrap|Line Numbers
  1. #include <map>
  2. #include <iostream>
  3. using std::cout;
  4. int main(int argc, char *argv[]){
  5.     try{
  6.        throw 1;
  7.        }
  8.     catch(int){
  9.       cout<<"Catch\n";
  10.      //std::flush;
Expand|Select|Wrap|Line Numbers
  1.  
  2. OP: That should be 'std::cout<<std::flush;'.
  3.  
  4.         
  5.                         
  6.                 >    }
  7.     return 0;
  8. }
  9.  
  10.  
>>
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 <GR
POVrookie
Nov 8 '06 #6

P: n/a

rupert wrote in message
<11**********************@m73g2000cwd.googlegroups .com>...
>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
Nov 8 '06 #7

P: n/a
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.

Nov 8 '06 #8

P: n/a
Is there anything else in main()?
no that's it.

Nov 8 '06 #9

P: n/a
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
Nov 8 '06 #10

P: n/a
VJ
rupert wrote:
Sorry correction;

Expand|Select|Wrap|Line Numbers
  1. #include <map>
  2. #include <iostream>
  3. using std::cout;
  4. int main(int argc, char *argv[])
  5. {
  6.     try
  7.     {
  8.        throw 1;
  9.     }
  10.     catch(int)
  11.     {
  12.       cout<<"Catch\n";
  13.      //std::flush;
  14.     }
  15.     return 0;
  16. }
  17.  

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)
Nov 8 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.