-2

the following code

  var aIniFile: TIniFile;


  procedure ASpecialEvent (aIniFile: TIniFile );
  begin
  ....
  if (aIniFile <> nil) then
  begin
    if fileexists(aIniFile.filename) then   //  Error is here ....
    begin
       ...... 
    end;
  end; 

  end;


  //   mainformcode  
  //   i Need to call this procedure even the Tini file is not yet created 


  ASpecialEvent(ainifile);

fails with an Access Violation. The infile is not Nil but I can not access the the filename property. How to check more efficient if the file is valid or not?

user1769184
  • 1,571
  • 1
  • 19
  • 44
  • Don't you forget to do `aIniFile := TIniFile.Create('FileName.ini')`? If `aIniFile` is local var it will not be equal `nil` before you create object. – kot-da-vinci Mar 18 '14 at 13:05

1 Answers1

1

The explanation for this is one of the following:

  1. aIniFile has not been initialised and so has an ill-defined value.
  2. aIniFile has been initialised, but refers to an object that has already been destroyed.
  3. aIniFile is a member of a record or class, and the implicit Self pointer is invalid. This option seems rather unlikely.

It's impossible to diagnose more specifically than that, but one of the above is the reason for your error.

Judging from the comments, it looks like you have not initialised the local variable aIniFile. In which case item 1 applies. I suspect that you are expecting that local variables are automatically initialised. That is not the case: Are delphi variables initialized with a value by default?

If you wish for nil to mean that the object has not been instantiated, you will have to explicitly initialise aIniFile to nil.

Community
  • 1
  • 1
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • It seems to me that `aIniFile` is a local var and object is not created. – kot-da-vinci Mar 18 '14 at 13:11
  • @user2819514 That would be item 1, but the other two are also possible, give the `....` in the Q – David Heffernan Mar 18 '14 at 13:15
  • @Heffernan It's not my question :) – kot-da-vinci Mar 18 '14 at 14:08
  • I did not Forget the TInifile.Create, I want to call a Procedure even the Parameter not yet an assigend ini file .... – user1769184 Mar 18 '14 at 14:39
  • @user1769184 OK, then it is item 1 from the list – David Heffernan Mar 18 '14 at 14:41
  • why does then not the (<> Nil) if Statement prevent me from executing the inifile.filename check ? – user1769184 Mar 18 '14 at 15:01
  • Because you have not initialised the local variable and it can have any value. A Delphi class is not a managed type and so a variable that holds a reference to that variable is not initialised. Did you read the linked question? Did you turn on compiler warnings? The compiler will warn you of the problem. – David Heffernan Mar 18 '14 at 15:05
  • it is not the value (filename) I'm suffering, the problem is that I can not detect inside my procedure that TInifile.create(...) has not yet been executed and therefore is does not make sense to run the second if Statement in the question. – user1769184 Mar 18 '14 at 16:37
  • I know. I've been trying to tell how to solve the problem. You have to initialise `aIniFile` to `nil`. At the start of the function. Let me ask you a question. Do you understand what I mean when I tell you that `aIniFile` has not been initialised? – David Heffernan Mar 18 '14 at 16:56