Solved

access violation freeing object

Posted on 2002-06-06
9
315 Views
Last Modified: 2010-04-04
I get an EAccess Violation and Invalid pointer operation error when trying to free an object,
I suspect that this error may be caused by the pointer parameters (TObjects) which member
functions receive. Anyway I don't want to do without them. How can I fix this behavior?

Sample code:

//this is the class whose objects can't be destroyed
type
 TControlsGrid = class(TObject)
 private
  barra: array[1..2] of array of integer;
  alsada,amplada,separacio,posicio: Integer;
  procedure RLeft(camp: TControl);
  procedure RWidth(camp: TControl;percentatge: Single);
 public
  procedure Posiciona(camp: TControl;percentatge: Single);
  constructor Create(taula: TDBCtrlGrid);
 end;

constructor TControlsGrid.Create(taula: TDBCtrlGrid);
begin
 inherited Create;
 posicio:=0;
 separacio:=2;
 amplada:=taula.PanelWidth;
 alsada:=taula.PanelHeight;
end;

//this is some member function which receives a pointer parameter
procedure TControlsGrid.Posiciona(camp: TControl;percentatge: Single);
begin
 posicio:=posicio+1;
 SetLength(barra[1],posicio);
 SetLength(barra[2],posicio);
 RLeft(camp);
 RWidth(camp,percentatge);
 camp.Top:=separacio;
 camp.Height:=alsada-(separacio*2);
end;
0
Comment
Question by:Kurtis
  • 4
  • 4
9 Comments
 

Author Comment

by:Kurtis
ID: 7060137
It also could be caused by the dynamic array barra.
Which I resize using SetLength. I have recently attempted to free them with a destructor:

destructor TControlsGrid.Destroy;
begin
 SetLength(barra[1],0); //Access violation here
 SetLength(barra[2],0);
 inherited Destroy;
end;

...but it failed. :(
0
 
LVL 12

Expert Comment

by:Lee_Nover
ID: 7060144
on destruction try setting the array to 0-length

it seems you also do something with the components in the RLeft and RWidth procedures
can you paste those also ?
0
 
LVL 12

Expert Comment

by:Lee_Nover
ID: 7060148
hehe :)
really would need more insight
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Author Comment

by:Kurtis
ID: 7060180
Ok, here you got them. RLeft & RWidth are used by another
procedure called Posiciona:

//gives format to TControl of a DBCtrlGrid
procedure TControlsGrid.Posiciona(camp: TControl;percentatge: Single);
begin
 posicio:=posicio+1;
 SetLength(barra[1],posicio); //barra[1] = lefts
 SetLength(barra[2],posicio); //barra[2] = widths
 RLeft(camp);
 RWidth(camp,percentatge);
 camp.Top:=separacio;
 camp.Height:=alsada-(separacio*2);
end;

//Calculates Left property of TControl using array info
procedure TControlsGrid.RLeft(camp: TControl);
begin
 if posicio=1 then
 begin
  barra[1,posicio]:=separacio;
 end else begin
  barra[1,posicio]:=separacio+barra[2,posicio-1];
 end;
 camp.Left:=barra[1,posicio];
end;

//Calculates Width property of TControl using array info
procedure TControlsGrid.RWidth(camp: TControl;percentatge: Single);
var
 ampladaMaxCamp,ampladaCamp: Integer;
begin
 ampladaCamp:=trunc((percentatge/100)*amplada);
 ampladaMaxCamp:=(amplada-(barra[1,posicio]+separacio));
 if ampladaCamp>ampladaMaxCamp then ampladaCamp:=ampladaMaxCamp;
 camp.Width:=ampladaCamp;
end;
0
 
LVL 12

Accepted Solution

by:
Lee_Nover earned 130 total points
ID: 7060441
weird

SetLength(barra[1],posicio); this reallocates the array to the length of posicio
so the array will be [0..posicio-1]

but in RLeft you're addressing it like :
barra[1,posicio]:=separacio;

and that's wrong
you're out of bounds !

you should use :
barra[1,posicio-1]:=separacio;
or
SetLength(barra[1],posicio+1);

because dynamic arrays are always 0 based !
0
 
LVL 12

Expert Comment

by:Lee_Nover
ID: 7060443
oh about that Invalid pointer error yuo're getting
you've overwritten some data that belongs to some pointer when using barra[1,posicio]:=separacio;
0
 
LVL 20

Expert Comment

by:Madshi
ID: 7061201
Lee has it. Writing over the bounds of dynamic arrays most of the time ends up in wild exceptions, you have to be very careful there. But if you are, dynamic arrays are a quite fine thing...
0
 

Author Comment

by:Kurtis
ID: 7062536
OK, I'm going to check it...
0
 

Author Comment

by:Kurtis
ID: 7062559
You are right Nover, thank you very much for your interest! :)
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

860 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