Recent

Author Topic: Why this "if not Assigned(FList) then" causes an AV??  (Read 4492 times)

garlar27

  • Hero Member
  • *****
  • Posts: 652
Why this "if not Assigned(FList) then" causes an AV??
« on: October 19, 2010, 11:45:59 am »
Hi,
   I made a class with an TFPList object in it. But when i try to access it I got an Access Violation.
   This is how I use it:
Code: Pascal  [Select][+][-]
  1. TMyClass = class(TObject)
  2.    ...
  3.    FList: TFPList;
  4.    ...
  5. end;
  6.  
  7. constructor TMyClass.Create;
  8. begin
  9.    FLista := nil;
  10.    FLista := TFPList.Create;
  11. end;
  12.  
  13. function TMyClass.Add(.....): Integer;
  14. begin
  15.    if not Assigned(FLista) then begin // Added because the last line caused an AV but now the AV occurs Here!!!
  16.       FLista := TFPList.Create;         // Added because the last line caused an AV
  17.    end;                                       // Added because the last line caused an AV
  18.  
  19.    Result := FList.Add(......); // originally this caused the error
  20. end;
  21.  

Does anybody knows whats wrong?

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1933
Re: Why this "if not Assigned(FList) then" causes an AV??
« Reply #1 on: October 19, 2010, 11:53:10 am »
Does anybody knows whats wrong?

You have FList and FLista ?
I can only see declaration of FList.

garlar27

  • Hero Member
  • *****
  • Posts: 652
Re: Why this "if not Assigned(FList) then" causes an AV??
« Reply #2 on: October 19, 2010, 12:23:14 pm »
Thanks for the quick response!!! But You caught me wen I as trying to remove this thread.

Sorry I've made a mistake I've tried to change FLista to FList to make it more readable and I forgot to change those names  :-[. And other mistake I did in my code does not appears here I called the Add method without creating MyObject :-[ :-[ :-[ but the weird thing is that execution went through many lines of code before reaching the line FList.Add!!!
Code: Pascal  [Select][+][-]
  1. TMyClass = class(TObject)  
  2.    ...  
  3.    FList: TFPList;  
  4.    ...  
  5. end;  
  6.    
  7. constructor TMyClass.Create;  
  8. begin  
  9.    FList := nil;  
  10.    FList := TFPList.Create;  
  11. end;  
  12.    
  13. function TMyClass.Add(.....): Integer;  
  14. begin  
  15.    if not Assigned(FList) then begin // Added because the last line caused an AV but now the AV occurs Here!!!  
  16.       FList := TFPList.Create;      // Added because the last line caused an AV  
  17.    end;                                // Added because the last line caused an AV  
  18.       New(UnDato);
  19.       UnDato^.Ubicacion := UnaUbicacion;
  20.       UnDato^.Orden := UnOrden;
  21.       UnDato^.TipoDato := UnTipoDeDatoFijo;
  22.       UnDato^.Texto := UnTexto;//}
  23.  
  24.    Result := FList.Add(UnDato); // originally this caused the error
  25.  

I would have expected the AV a lot earlier: when I call MyObject.Add()

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(...);
  2. begin
  3. ...
  4. MyObject.Add(.....);
  5. ...
  6. end;
  7.  

This is not the first time it happened to me.
« Last Edit: October 19, 2010, 12:27:48 pm by garlar27 »

BlueIcaro

  • Hero Member
  • *****
  • Posts: 834
    • Blog personal
Re: Why this "if not Assigned(FList) then" causes an AV??
« Reply #3 on: October 19, 2010, 01:42:58 pm »
Hello, did you create your object before use it?

Like this

Quote

Var
 MyObject : TMyClass;
Begin
 MyObject := TMyClass.Create; //Create your object called MyObject, class : TMyClass

 MyObject.Add (Somthing); //Then you can use it



/BlueIcaro

 

TinyPortal © 2005-2018