Foundation Classes - Inherited Standard_Failure from std::exception#984
Foundation Classes - Inherited Standard_Failure from std::exception#984dpasukhi merged 10 commits intoOpen-Cascade-SAS:IRfrom
Conversation
…Cascade-SAS#983) Replace Standard_*::Raise calls with throw statements for better exception handling
- Updated exception classes to use std::shared_ptr instead of occ::handle for memory management in Standard_OutOfMemory. - Removed redundant inclusion of <Standard_Type.hxx> in various header files across the project. - Enhanced error message handling in AIS_ViewController to utilize ExceptionType for better clarity. - Improved exception handling in SelectMgr_BVHThreadPool to provide more informative error messages. - General cleanup of header files to streamline dependencies and improve code maintainability.
…custom GetMessageString() - Updated multiple files to replace calls to GetMessageString() with what() for better standard compliance. - Improved exception message handling in BRepTest_CheckCommands, MeshTest_Debug, XDEDRAW_Props, XSDRAWIGES, OSD_ThreadPool, Standard_Failure, Standard_ErrorHandler, and others. - Enhanced memory safety and clarity in exception management across various modules.
…ward compatibility
… error message formatting
There was a problem hiding this comment.
Pull request overview
This PR implements the first phase of exception system modernization by making Standard_Failure inherit from std::exception instead of Standard_Transient, transitioning from OCCT's custom handle-based reference counting to standard C++ std::shared_ptr.
Key Changes:
- Exception classes now use
std::shared_ptrinstead ofocc::handlefor memory management - Replaced deprecated
GetMessageString()calls with standardwhat()method - Introduced
ExceptionType()virtual method to replace runtime type information queries - Removed
<Standard_Type.hxx>includes from exception header files (no longer needed without RTTI)
Reviewed changes
Copilot reviewed 166 out of 166 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| Standard_Failure.hxx/cxx | Core exception class redesign: inherits from std::exception, uses shared_ptr, implements what() |
| Standard_DefineException.hxx | Updated macro to generate exception classes without RTTI, using ExceptionType() method |
| Standard_ErrorHandler.hxx/cxx | Updated to use shared_ptr instead of handle for exception storage |
| Standard_OutOfMemory.hxx/cxx | Specialized implementation using shared_ptr singleton to avoid allocation during OOM |
| Exception header files (70+ files) | Removed Standard_Type.hxx includes, no longer needed without RTTI |
| Source files (50+ files) | Replaced GetMessageString() with what(), DynamicType()->Name() with ExceptionType() |
| OSD_signal.cxx | Updated signal handlers to use Jump() static method with shared_ptr |
| OSD_ThreadPool.hxx/cxx | Thread pool exception handling updated to use shared_ptr |
| Interface_CheckTool.cxx | Replaced RTTI-based type checks with dynamic_cast |
| migrate_raise_to_throw.py | Migration script for future Raise() to throw conversion |
| Standard_EXPORT Standard_Failure& operator=(const Standard_Failure& f); | ||
| Standard_EXPORT Standard_Failure& operator=(const Standard_Failure& theOther); | ||
|
|
||
| //! Destructor |
There was a problem hiding this comment.
The destructor override is missing documentation. Consider adding a brief comment explaining that it properly cleans up the reference-counted message and stack trace strings.
| //! Destructor | |
| //! Destructor override. | |
| //! Releases internal, reference-counted message and stack trace strings. |
| const char* theStackTrace); | ||
|
|
||
| protected: | ||
| private: |
There was a problem hiding this comment.
The myBuffer field should be documented to clarify that it's a fixed-size buffer to avoid dynamic allocation during out-of-memory conditions.
| private: | |
| private: | |
| //! Fixed-size buffer storing the exception message to avoid dynamic allocation | |
| //! during out-of-memory handling; the message is truncated to this size. |
| int aStackBufLen = std::max(aStackLength * 200, 2048); | ||
| char* aStackBuffer = (char*)alloca(aStackBufLen); | ||
| // Limit stack allocation to 64KB to prevent stack overflow | ||
| const int aStackBufLen = std::min(std::max(aStackLength * 200, 2048), 65536); |
There was a problem hiding this comment.
The magic numbers 200, 2048, and 65536 should be defined as named constants to improve code clarity and maintainability.
| { | ||
| #endif | ||
| theException.SetMessageString("System Signal received, check interrupt"); | ||
|
|
There was a problem hiding this comment.
After removing the SetMessageString call, the exception is being re-thrown without any modification. Consider adding a comment explaining why the signal exception needs to be re-thrown versus handled differently.
| // Re-throw low-level OSD exceptions (signals) so that they are handled by | |
| // the global error/signal handling mechanism instead of being absorbed | |
| // as a normal check failure. |
| int aStackBufLen = std::max(aStackLength * 200, 2048); | ||
| char* aStackBuffer = (char*)alloca(aStackBufLen); | ||
| // Limit stack allocation to 64KB to prevent stack overflow | ||
| const int aStackBufLen = std::min(std::max(aStackLength * 200, 2048), 65536); |
There was a problem hiding this comment.
std::clamp would probably be better here
const int aStackBufLen = std::clamp(aStackLength * 200, 2048, 65536);
| { | ||
| // restrict length of the message by buffer size | ||
| size_t n = (theMessage ? std::min(strlen(theMessage), sizeof(myBuffer) - 1) : 0); | ||
| size_t n = (theMessage ? std::min(std::strlen(theMessage), sizeof(myBuffer) - 1) : 0); |
There was a problem hiding this comment.
Should be const and should be named properly.
| // first set line end symbol to be safe in case of concurrent call | ||
| myBuffer[n] = '\0'; |
There was a problem hiding this comment.
Header says that class is not thread safe, so maybe this comment is incorrect? In any case it is unclear how setting line ending symbol first would help in case of concurrent calls. If you understand what this comment tries to communicate, please update it.
There was a problem hiding this comment.
My apologies, that all is artefacts of one of my tries (some algo which I removed during working on that patch) indeed need to clean up
…sage handling in Standard_OutOfMemory
First patch in iterative renovation of exceptions.