Exception handling.

Started by
27 comments, last by ANSI2000 21 years, 11 months ago
quote:Original post by Void

I disagree.

If the base ctor is completed and the dervied child ctor throws, the base dtor will be called. Only the dtor for the derived class will not be called.

Argh, I mixed up the problem with the solution.
This is the gotw.
The problem is when you don''t use RAII, and ''new'' multiple things in the derived ctor body - then there''s a potential resource leak. The missing syntax in MSVC is the ctor exception translator.

My apologizes, I''ll make a point of double checking my facts before I spout off non-sense again.
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Advertisement
I do call WinsockManager::getInstance()->cleanUP() once at the end...
It won''t get called if an exception is thrown, unless you are explicitly calling it in the catch.
Liked I said the only reason why I wanted to catch a cleanUp exception is to use my standard way of error handling and see what the WSA error was... I call cleanUp() once inside startUp() only if Winsock dont initialize, which will only throw and exception after only clean up was called... My question was that if clean up throws an exception before start up throws one will it be caught by the try catch block of the startUp() call...

Any how I call cleanUp once at the end in my services shutdown() method... You must call WSACleanup for every WSAStartup call, you can also call WSACleanup as many times as you wish, but eventually it will fail since all of winsock will have been cleaned up...
Either you didn''t paste the code correctly or something amiss but I don''t see a try/catch block inside startUp() that tries to suppress exceptions thrown by cleanUp(). And of course if cleanup() throws, this is never called.


// Make sure that Winsock is the requested version.
if(WSAData.wVersion != pVersion)
{
cleanUp();

// THIS MAY NEVER GET CALLED
throw WinsockException("Incompatible winsock version.", 0);
}


You are doing something similar to throwing in a dtor, which is non RAII.

Keep an instance of the WSAResource (see above post) as the first member variable in your WinsockManager. Then WSAStartup will be called first when you initialize the manager, and WSACleanup will be called last when you delete the manager.

The alternative is to not call a throwable Cleanup() but called WSACleanup() directly inside startup(). As I said, have faith that the cleanups calls do not fail unless you misused the setup.
Yes cleanUp does throw an exception I was wondering if it does throw the excetion will climb the stack and get caught byt the try ctach of startUp()

But I dont do any init in ctor and no cleanup in dtor, it''s a singleton... I do call cleanUp() at the end...

If the startUp() fails, cleanUp() gets called internally, so that is guarateed to free resources... and if my winsock init failed my program wont run it exits right away... I also mention that I make sure to call cleanUp() once at the end of my app... No matter what happened.
What Void means is that should treat startUp() as a ctor and cleanUp() as a dtor. So the same rules apply (never emit exceptions in dtors, don''t destroy the object if the ctor emits an exception).

-------homepage - email
Using RAII in this situtaion is to much maybe... I dont see why I would have to create a WSAResource and then declare it static. By declaring it static the ctor automatically is called, then if it fails throw an exception, then catch that exception in my WinsockManager handle then throw an exception for the WinsockManger for what?

Either that or I fail to see Void's point since I do not see the whole implementation of how he wood do it...

All I need is that I call
WinsockManager::getInstance()->startUp()
once at the begining...

And call WinsockManager::getInstance()->cleanUp() at the end regardless...

Also WinsockManger does not allocate any memmory etc... Maybe the WSA functions do, but who cares what they do internaly, you trust they work, when called...

[edited by - ANSI2000 on May 7, 2002 12:10:58 PM]
I gonna bite my tongue and ask "What do you need it to be a singleton in the first place?" Won''t a member variable or auto stack variable inside main works as well?

This topic is closed to new replies.

Advertisement