Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > memory leak with a thrown exception

Reply
Thread Tools

memory leak with a thrown exception

 
 
rupert
Guest
Posts: n/a
 
      11-07-2006
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;
}
 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      11-07-2006
* 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";
Code:
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;


>     }    
>     return EXIT_SUCCESS;

This constant is not guaranteed to be available by including <iostream>.


> }
> 
> 



--
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?
 
Reply With Quote
 
 
 
 
rupert
Guest
Posts: n/a
 
      11-08-2006
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

 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      11-08-2006
* 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?
 
Reply With Quote
 
rupert
Guest
Posts: n/a
 
      11-08-2006
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?....

 
Reply With Quote
 
BobR
Guest
Posts: n/a
 
      11-08-2006

Alf P. Steinbach wrote in message <(E-Mail Removed)>...
>* 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;
Code:
OP: That should be 'std::cout<<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".


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


 
Reply With Quote
 
BobR
Guest
Posts: n/a
 
      11-08-2006

rupert wrote in message
<(E-Mail Removed) .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


 
Reply With Quote
 
rupert
Guest
Posts: n/a
 
      11-08-2006
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.

 
Reply With Quote
 
rupert
Guest
Posts: n/a
 
      11-08-2006
> Is there anything else in main()?
no that's it.

 
Reply With Quote
 
Roland Pibinger
Guest
Posts: n/a
 
      11-08-2006
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Exception of type 'System.Web.HttpUnhandledException' wasthrown.Exception has been thrown by the target of an invocation.System.WebSystem.Exception jobs ASP .Net 1 11-16-2007 05:57 PM
Exception has been thrown by the target of invocation exception =?Utf-8?B?VmFs?= ASP .Net 0 06-08-2005 05:11 PM
Exception has been thrown by the target of invocation exception Val ASP .Net Security 0 06-08-2005 05:10 PM
Exception Handling & Memory Leak Bikash C++ 5 07-22-2004 12:23 PM
Exception of type System.Exception was thrown Selen ASP .Net 0 05-28-2004 07:19 AM



Advertisments