Recent

Author Topic: Assigned function returns action violation, why  (Read 7361 times)

cpalx

  • Hero Member
  • *****
  • Posts: 753
Assigned function returns action violation, why
« on: July 22, 2010, 07:12:31 pm »
Hello

When then Assigned function returns  action violation.

I am  doing this.

I have a two public variables pointing to a class (the class works perfecly)
public
FullPanel : TFullPanelData;

in the unit i made this:
procedure ...
if assigned(FullPanel) then  //This line display Action violation
  FreeAndNil(FullPanel)
FullPanel := TfullpanelData.create();

any idea?

 

JohnvdWaeter

  • Full Member
  • ***
  • Posts: 185
    • http://www.jvdw.nl
Re: Assigned function returns action violation, why
« Reply #1 on: July 22, 2010, 07:19:26 pm »
Maybe like this:

if FullPanel <> nil then 
  FreeAndNil(FullPanel)

hth,
John

cpalx

  • Hero Member
  • *****
  • Posts: 753
Re: Assigned function returns action violation, why
« Reply #2 on: July 22, 2010, 07:24:07 pm »
i tried too, but same error... Access violation

JohnvdWaeter

  • Full Member
  • ***
  • Posts: 185
    • http://www.jvdw.nl
Re: Assigned function returns action violation, why
« Reply #3 on: July 22, 2010, 07:37:55 pm »
Must be working, I use it all the time.

Hard to see in your code-snippet, but are the public variabele FullPanel and the procedure ... in which you refer to it members of the same class?

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4541
  • I like bugs.
Re: Assigned function returns action violation, why
« Reply #4 on: July 22, 2010, 07:49:38 pm »
It means there is garbage in the variable because it is not initialized to nil or the object it refers to is already freed somewhere.

Initialize it to nil and debug where it is assigned and freed.

Juha
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 10553
  • Debugger - SynEdit - and more
    • wiki
Re: Assigned function returns action violation, why
« Reply #5 on: July 22, 2010, 07:54:51 pm »
public
FullPanel : TFullPanelData;

procedure ...
if assigned(FullPanel) then  //This line display Action violation
  FreeAndNil(FullPanel)
FullPanel := TfullpanelData.create();

2 possibilities:
1) the line information s wrong, and the access violation happens in FreeAndNil, rather than assigned => in which case, maybe the member-variable "FullPanel" did contain trash (anything but nil) despite no object is existing.

2) If it really is the line
if assigned(FullPanel)
FullPannel is a member variable of the object that the current procedure belongs to.
If this object is nil (or trash) then accesing any variable on the object may give you an access violation.
Note: even if the object is nil => you can still call the procedure, as long as it isn't virtual/overridden; nor doesn't access self or member-variables.


cpalx

  • Hero Member
  • *****
  • Posts: 753
Re: Assigned function returns action violation, why
« Reply #6 on: July 22, 2010, 08:15:24 pm »
i explain better my situacion

first, i had two class, Tfullpanel and OtherUnit, when i use OtherUnit to instance Tfullpanel works perfectly.

Now i have three units: Tfullpnale, NewUnit, OtherUnit

newUnit instance Tfullpanel, but i have two publics variables. When i try to do something like this:

PublicVar := Tfullpanel.create  // -> display action violation

why cannot be assigned???????


if a made just
Tfullpanel.create, works ...

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 10553
  • Debugger - SynEdit - and more
    • wiki
Re: Assigned function returns action violation, why
« Reply #7 on: July 22, 2010, 09:21:11 pm »
last question differs from first...

Also to clarify:
"public variable" => part of some object
"global variable" => not part of an object

Code: [Select]
type
  TFoo = class
  public
    SomeVar: TObject;
    procedure SomeProc;
  end;

implementation
procedure TFoo.SomeProc;
begin
  SomeVar := TObject.create; // or anything else...
end;

....
procedure SomewhereElse
var F: TFoo;
begin
  F:= nil;
  F.SomeProc;
end;

no calling F.SomeProc can be done, even so F is nil.

but if F is nil you can not access F.SomeVar => that will crash

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4541
  • I like bugs.
Re: Assigned function returns action violation, why
« Reply #8 on: July 22, 2010, 09:39:48 pm »
You need
Code: [Select]
TFoo = class
  FullPanel : TFullPanelData;
  ...
end;

and then
Code: [Select]
myFoo:=TFoo.Create;

then you can access
Code: [Select]
myFoo.FullPanel := nil;

Juha
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

cpalx

  • Hero Member
  • *****
  • Posts: 753
Re: Assigned function returns action violation, why
« Reply #9 on: July 22, 2010, 09:49:17 pm »
Thank you.

 

TinyPortal © 2005-2018