Jump to content

MessageDlg inside Try..Finally


dpfmp

Recommended Posts

Hi,

// test

procedure TMainForm.UniButton1Click(Sender: TObject);
var
  iArr: array of integer;
begin
   SetLength(iArr,2);
   try

     MessageDlg('Yes or No', mtConfirmation, mbYesNo,
     procedure (Sender: TComponent; Res: integer)
      begin
        case Res  of
          TModalResult(mrYes):  iArr[0] := 0;   // <-- AV
          TModalResult(mrNo):   iArr[1] := 1;  // <-- AV
        end
      end);

   finally
     SetLength(iArr,0);
   end;
end;

 

The code reaches the "Finally" block before MessageDlg shows up and causes an AV since the array was freed.  What if we need to change a dynamically created structure ( memory table, array etc ) inside a "try..finally" by calling a function in the MessageDlg callback that would modify the structure?  

Is this the wrong approach?

Thank you,

Denis Prince 

D10.1 UniGUI 1.50.0.1484

Link to comment
Share on other sites

Hi Sherzod,

 

EnableSynchronousOperations true

 

Not blocking the thread

=================================================

procedure TMainForm.UniButton1Click(Sender: TObject);
var
  iArr: array of integer;
begin
   SetLength(iArr,2);
   try
     MessageDlg('Yes or No', mtConfirmation, mbYesNo,
       procedure (Sender: TComponent; Res: integer)
       begin
         case Res  of
           TModalResult(mrYes):  iArr[0] := 0;    // <-- AV
           TModalResult(mrNo):   iArr[1] := 1;    // <-- AV
        end
      end);

   finally
     SetLength(iArr,0);
   end;
end;

 

Blocking the thread  (this works fine in my test)

================================================

procedure TMainForm.UniButton2Click(Sender: TObject);
var
  iArr: array of integer;
  Res: TModalResult;
begin
  SetLength(iArr,2);
  try

    Res := MessageDlg('Yes or No', mtConfirmation, mbYesNo);
    case Res of
      mrYes:  iArr[0] := 0;
      mrNo:   iArr[1] := 1;
    end

  finally
    SetLength(iArr,0);
  end;
end;

Do we use the callback in MessageDlg when we don't want to block the thread?

 

Thank you again for your help,

Denis Prince

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...