Jump to content
Sign in to follow this  
Skrim

aDialog created inside a Try/Finally, bad practise?

Recommended Posts

try
    aDialog:=TMyDialog.Create(self);
   ...

   ...
finally
    aDialog.Free;
end;

 

I keep getting warnings about a dialog created inside a Try/Finally, why is it bad practise?

 

Share this post


Link to post

Because aDialog is not assigned when an exception is raised inside TMyDialog.Create. That causes Free called for an uninitialized variable inside the finally block.

 

A workaround would be to set aDialog to nil before the try, but the cleaner way is to move the Create outside the try-finally.

  • Like 1
  • Thanks 1

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×