memory leak with a thrown exception

R

rupert

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;
}
 
A

Alf P. Steinbach

* 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";[/QUOTE]

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;

[QUOTE]
}    
return EXIT_SUCCESS; 

}
 
R

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
 
A

Alf P. Steinbach

* 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).
 
R

rupert

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?....
 
B

BobR

Alf P. Steinbach wrote in message said:
* 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;[/QUOTE][/QUOTE]

OP: That should be 'std::cout<<std::flush;'.
[QUOTE][QUOTE]
}
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!)
 
B

BobR

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()?
 
R

rupert

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.
 
R

Roland Pibinger

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
 
V

VJ

rupert said:
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)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top