![]() |
|
|
|
#1 |
|
Posts: n/a
|
Hi,
I wonder what is the difference between ending the main() with and without exit(0)? My code is like this flow, class ClassA{ ClassB* cb; int toExit(); public: ~ClassA(); ... constructors and all other API... } ClassA::~ClassA(){ this->toExit(); } ClassA::toExit(){ if (cb != NULL){ delete cb; cb = NULL; } } int main(int argc, char** argv){ ClassA ca; ... some operation ... return 0; } Then the program exits with a core dump, in debugger I can see that the core dump comes from "}" after "return 0;" in main() and "delete cb" in ClassA::toExit(). In purify it says some FMR, Free Memory Read errors. Then I replace "return 0" with "exit(0)" in main(), everything is fine: there is no core dump and purify FMR errors are gone! What's the trick here? Thanks. |
|
|
|
#2 |
|
Posts: n/a
|
John Black wrote:
> I wonder what is the difference between ending the main() with and > without exit(0)? > > My code is like this flow, > > class ClassA{ > ClassB* cb; > > int toExit(); > > public: > ~ClassA(); > ... constructors and all other API... > > } What does your constructor do to cb? Does it ever point to a 'new'ed heap object? > > ClassA::~ClassA(){ > this->toExit(); > } > > ClassA::toExit(){ > if (cb != NULL){ > delete cb; > cb = NULL; > } > } > > int main(int argc, char** argv){ > ClassA ca; > > ... some operation ... > > return 0; > } > > Then the program exits with a core dump, in debugger I can see that > the core dump comes from "}" after "return 0;" in main() and "delete cb" > in ClassA::toExit(). > > In purify it says some FMR, Free Memory Read errors. > > Then I replace "return 0" with "exit(0)" in main(), everything is > fine: there is no core dump and purify FMR errors are gone! Right. exit() is not magic, it is C, without (much) C++ awareness. It bypasses the destructor for ca, so that never tries to delete cb. After you fix your bug, never call exit() again. All C++ programs should find their way back to main()'s closing bracket. -- Phlip http://industrialxp.org/community/bi...UserInterfaces |
|
|
|
#3 |
|
Posts: n/a
|
"John Black" <> wrote in message
news:... > Hi, > I wonder what is the difference between ending the main() with and > without exit(0)? > > Then the program exits with a core dump, in debugger I can see that > the core dump comes from "}" after "return 0;" in main() and "delete cb" > in ClassA::toExit(). First of all, the dump is obviously because cb was never allocated but still deleted. If you're going to use NULL to check whether it's allocated or not, you should make sure it's also actually allocated. > Then I replace "return 0" with "exit(0)" in main(), everything is > fine: there is no core dump and purify FMR errors are gone! Indeed. This is because exit() is a function that never returns. As such, main() never reaches the end, and thus doesn't unwind its stack. As such, the ClassA destructor is not called, and the offending code is never reached. -- Unforgiven |
|
|
|
#4 |
|
Posts: n/a
|
"John Black" <> wrote in message news:... > Hi, > I wonder what is the difference between ending the main() with and > without exit(0)? > > My code is like this flow, > > class ClassA{ > ClassB* cb; > > int toExit(); > > public: > ~ClassA(); > ... constructors and all other API... > > } > > ClassA::~ClassA(){ > this->toExit(); > } > > ClassA::toExit(){ > if (cb != NULL){ You don't need to check for NULL here. Calling delete on a pointer whose value is NULL has no effect (good OR bad). BUT!... you need to either set cb to NULL in your constructor, or make sure it is assigned a valid value (using new) before this function is ever called. But why do you have a "toExit" function at all? Why not just delete the object inside your destructor? (If you're going to be creating and deleting the object more than once, you might want to rename it to something more meaningful, such as "DeleteB".) > delete cb; > cb = NULL; > } > } > > int main(int argc, char** argv){ > ClassA ca; > > ... some operation ... > > return 0; > } > > Then the program exits with a core dump, in debugger I can see that > the core dump comes from "}" after "return 0;" in main() and "delete cb" > in ClassA::toExit(). > > In purify it says some FMR, Free Memory Read errors. > > Then I replace "return 0" with "exit(0)" in main(), everything is > fine: there is no core dump and purify FMR errors are gone! Don't cal exit. It's not solving your problem, just hiding it! > > What's the trick here? > > Thanks. -Howard |
|
|
|
#5 |
|
Posts: n/a
|
"John Black" <> wrote in message news:... > Hi, > I wonder what is the difference between ending the main() with and > without exit(0)? > [snip] Behavior of the program with return, exit () and abort () is not the same one. Look at http://groups.google.com/groups?selm...1.dejanews.com -- Alex Vinokur http://mathforum.org/library/view/10978.html http://sourceforge.net/users/alexvn |
|
![]() |
| Thread Tools | Search this Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| DVD Verdict reviews: DAIMAJIN / RETURN OF DAIMAJIN / WRATH OF DAIMAJIN and more! | DVD Verdict | DVD Video | 0 | 05-24-2005 08:13 AM |
| DVD Verdict reviews: THE BLUE LAGOON AND RETURN TO THE BLUE LAGOON DOUBLE FEATURE and more! | DVD Verdict | DVD Video | 0 | 03-18-2005 08:14 AM |
| DVD Verdict reviews: THE LORD OF THE RINGS: THE RETURN OF THE KING: SPECIAL EXTENDED EDITION and more! | DVD Verdict | DVD Video | 0 | 01-25-2005 09:34 AM |
| DVD Verdict reviews: THE LORD OF THE RINGS: THE RETURN OF THE KING and more! | DVD Verdict | DVD Video | 0 | 06-07-2004 09:05 AM |
| DVD Verdict reviews: BARBIE OF SWAN LAKE, THE RETURN OF SWAMP THING, and more! | DVD Verdict | DVD Video | 0 | 11-22-2003 09:04 AM |