Solved

access violation freeing object

Posted on 2002-06-06
9
313 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Virtualstringtree carsh after invalidate for while 5 86
Path  to current project in Delphi. 2 71
Intraweb download file link ? 1 105
Delphi inherited method 6 60
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…
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…
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …
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 …

948 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now