1

I developed an application in VCL which is using VCL Themes. This application requires TPageControl and inner(child) forms in it.

Each child form has same way in OnClose: Parent.Destroy;

 MsgResp := MessageDlg('Closing info....', mtWarning, [mbYes, mbNo, mbCancel], 0);
  case MsgResp of
    mrYes:
      begin
        DoSomething; {Save something}
        Parent.Destroy;
      end; 
    mrNo:
      begin
        Parent.Destroy;
      end;
    mrCancel:
      begin
        Exit;
      end;
 end;

If I set a theme to application like Sapphire Kamri (or something else), I get access violation error in destroying parent component. But if I use default style (Windows), this code works fine.

Abdullah Ilgaz
  • 719
  • 1
  • 17
  • 39
  • 2
    As an aside, we seldom call `Destroy` on an object other than `Self`. Instead we use `Free` which first checks whether it not the subject is `nil`. – David Heffernan Feb 21 '18 at 07:33

1 Answers1

7

This is entirely to be expected. Your code is just as broken without VCL styles, but you get away with it.

The problem is the calls to Parent.Destroy. When these occur, the parent is destroyed, and so are all of its children, including the control that owns the code seen in the question. When the call to Parent.Destroy returns, execution continues in methods of objects that have been destroyed. That is the source of the runtime error.

You need to schedule the destruction to happen after the OnClose event handler has completed. The VCL Release method exists for this very purpose.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490