I have a question regarding memory deallocation and exceptions. when I use delete to delete an object created on the heap . If an exception occurs before this delete is the memory going to leak or this delete is going to execute ?
I have a question regarding memory deallocation and exceptions. when I use delete to
Share
This depends on where that
deleteis. If it’s inside thecatchthat catches the exception, it might invoke.If it’s after the
catchthat catches the exception and thatcatchdoesn’t return from the function (i.e. allows execution flow to proceed after thecatchblock) then thedeletemight be called.If the
deleteis not in acatchblock or after acatchblock that allows execution to proceed then thedeletewill not call.As you may imagine, in the two first cases above the
deletewill not be invoked if there is a throw before thedelete: