I have something like this:
void Test(void)
{
char errorMessage[256];
spintf(errorMessage,... blablabla);
throw new CustomException(errorMessage);
}
Will this be a memory leak because errorMessage will be not freed? Or will this cause an exception when accessing the message of the exception inside a try{}catch because the errorMessage has been freed when going out from the function¿?
Thanks in advance.
The memory of
errorMessagewill already be freed when accessed by the catch handler. However, you could just copy it into astd::stringinCustomException‘s constructor.A memory leak, on the other hand, could be caused by the exception itself, since you put it on the heap. This is not necessary.