Solved

access violation freeing object

Posted on 2002-06-06
9
312 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

757 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

19 Experts available now in Live!

Get 1:1 Help Now