Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Create Open Dialog

Posted on 2007-11-16
8
Medium Priority
?
291 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 19

Accepted Solution

by:
MerijnB earned 500 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

618 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