Actually 'Game Recording' mode of Bandicam already has the auto stop function when the recording target is lost.
In most cases it works correctly, however, there are a few cases where the problem might occur as Bandicam cannot receive the information that the game had been closed.
You can try Bandifix if your output type is .AVI, but unfortunately we cannot solve the problem for broken .MP4 files at this moment.
We will try to find the solution for your issue in the future.
At this moment, we recommend you use the Auto Complete Recording function to save files safely when you record for a long time.