Everything is good.
In ยง15.1 / 3:
Throwing a copy of the exception initializes (8.5, 12.8) a temporary object called the exception object.
and ยง15.1 / 4:
Memory for the object of exclusion is allocated in unspecified, with the exception of cases specified in clause 3.7.4.1. If the handler completes the recovery, control is transferred to another handler for the same exception. the exception object is destroyed after the last active handler to exclude is excluded by any means other than reintronation ...
so after throw expression :
the expression will be copied (a new object will be created by the copy constructor), and you should not worry about the local object.
About msg and const char* you are worried about ... here is the Microsoft implementation:
exception::exception(const char * const & _What) : _Mywhat(NULL), _Mydofree(false) { _Copy_str(_What); //^^^^^^^^^^^^^^^^^ } void exception::_Copy_str(const char * _What) { if (_What != NULL) { const size_t _Buf_size = strlen(_What) + 1; _Mywhat = static_cast<char *>(malloc(_Buf_size)); if (_Mywhat != NULL) { _CRT_SECURE_STRCPY(const_cast<char *>(_Mywhat), _Buf_size, _What); //^^^^^^^^^^^^^^^^^^ _Mydofree = true; } } }
It copies _What not only saving the pointer.
deepmax
source share