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. 4 5286
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
"John Black" <bl***@eed.co m> wrote in message
news:41******** *******@eed.com ... 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
"John Black" <bl***@eed.co m> wrote in message
news:41******** *******@eed.com ... 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Maximus |
last post by:
Hi,
I was just wondering, is it good to use return without arguments in a void
function as following:
void SetMapLayer()
{
if( !Map ) return;
layer = LAYER_MAP;
}
|
by: fwee |
last post by:
What is the difference between using exit(n) and return n in main(), if
any? I know that exit(n) is a function that exits and return n exits via a
return value, but is there any reason to specify one rather than the
other? Thanks!
|
by: QQ |
last post by:
I know there are many functions that I can exit the program such as
return 0,
exit(0), exit(1),_EXIT(0) ....
What are the difference between them?
Thanks a lot!
|
by: jwaixs |
last post by:
Hello,
I was wondering, what's the difference between exit and return in the
main() function? For me they both look the same, or aren't they? And if
they aren't, which should I use in which situation?
Also I was wondering if it whould be wise to combine the standard
status with return.
exp:
|
by: lovecreatesbeauty |
last post by:
Hello experts,
Is the following code snippet legal? If it is, how can exit() do the
keyword return a favor and give a return value to the main function?
Can a function call (or only this exit(n)) statement provide both
function call and return features of the C programming language?
/* headers omitted */
int main (void)
| |
by: johnnyG |
last post by:
Greetings,
Since Main() is a subroutine in VB.NET console apps and not a function, is
there a way to return a value to a script ro other "caller" for a VB.NET
console .exe?
sub main()
like
if file.exists(myFile)
return 1 'file downloaded
else
return 0 'file not downloaded
|
by: tshad |
last post by:
Is there any difference between Return and Exit Sub?
I have some code that uses both when I have an error in my Sub.
Thanks,
Tom
|
by: Pedro Pinto |
last post by:
Hi there once more........
Instead of showing all the code my problem is simple.
I've tried to create this function:
char temp(char *string){
alterString(string);
return string;
|
by: jayapal |
last post by:
hi all,
what is the differrence b/w the usage or return and the exit in the C
programming..
thanks,
jay
|
by: =?Utf-8?B?Um9nZXIgVHJhbmNoZXo=?= |
last post by:
Hello,
I have a question about the infamous GOTO statement and the way to return a
result from a sub:
I have a sub that has to make some calls to external COM methods, and
because these methods can fail I have to check them to be running ok, like
this:
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |