Solved

Create Open Dialog

Posted on 2007-11-16
8
280 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
Comment Utility
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
Comment Utility
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
Comment Utility
Woops... remove the comment //Access violation as it is not there.

John
0
 
LVL 19

Expert Comment

by:MerijnB
Comment Utility
always refresh before you post a comment :)
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 18

Expert Comment

by:Johnjces
Comment Utility
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
Comment Utility
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
Comment Utility
oops, sorry, that was a very big typo, should of course be: OpenDlg := TOpenDialog.Create(self);
0
 
LVL 19

Expert Comment

by:MerijnB
Comment Utility
(glad you are paying attention :))
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
This video discusses moving either the default database or any database to a new volume.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

762 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now