Solved

Create Open Dialog

Posted on 2007-11-16
8
287 Views
Last Modified: 2010-04-04
When I try to create a TOpenDialog I get an EAccessViolation Error. Here is my code where the error occurs:

var
  OpenDlg: TOpenDialog;
  filename: string;

procedure TForm1.FormCreate(Sender: TObject);
begin
  OpenDlg.Create(OpenDlg); //Access Violation
  if OpenDlg.Execute then
  begin
    filename := OpenDlg.FileName;
  end;

Could anyone tell me how to fix this?
0
Comment
Question by:ding-dong
  • 4
  • 3
8 Comments
 
LVL 19

Accepted Solution

by:
MerijnB earned 125 total points
ID: 20300348
procedure TForm1.FormCreate(Sender: TObject);
begin
  OpenDlg := TOpenDialog.Create(OpenDlg); //Access Violation gone
  try
    if OpenDlg.Execute then
    begin
      filename := OpenDlg.FileName;
    end;
  finally
    OpenDlg.Free();
  end;
end;
0
 
LVL 18

Expert Comment

by:Johnjces
ID: 20300369
Try this...

procedure TForm1.Button1Click(Sender: TObject);
begin
  OpenDlg := TOpenDialog.Create(self); //Access Violation
  if OpenDlg.Execute then
  begin
    Edit1.Text := OpenDlg.FileName;
  end;  
end;

John
0
 
LVL 18

Expert Comment

by:Johnjces
ID: 20300373
Woops... remove the comment //Access violation as it is not there.

John
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 19

Expert Comment

by:MerijnB
ID: 20300379
always refresh before you post a comment :)
0
 
LVL 18

Expert Comment

by:Johnjces
ID: 20300411
MerijnB

WoopS! Alright I see the problem!!!! In just before me!

Sorry about that! I see that happen every so often... first time for me!

;-) John

0
 
LVL 26

Expert Comment

by:Russell Libby
ID: 20301264
The following code:

  OpenDlg:=TOpenDialog.Create(OpenDlg);

Is going to be prone to problems, and should not be used. If OpenDlg is not cleared (left pointing to an already freed object; aka stale pointer), then an access violation WILL occur. Not a problem for the first time use when the var is global in scope, but the second time is an issue. This is also an issue if the var is local in scope (not guaranteed to be nil). In short, poor practice.

If you don't need an owner then use var:=TOpenDialog.Create(nil), or as John pointed out, use Self, which in this case refers to the object who's method your calling (TForm1).

Russell


0
 
LVL 19

Expert Comment

by:MerijnB
ID: 20301279
oops, sorry, that was a very big typo, should of course be: OpenDlg := TOpenDialog.Create(self);
0
 
LVL 19

Expert Comment

by:MerijnB
ID: 20301280
(glad you are paying attention :))
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Delphi: how to send PJL commands to printer 3 108
Reconfigure Delphi Install? 2 60
DBGrid or StringGrid ? 6 104
PHP preg_replace code convert to Delphi 14 66
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question