Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

access violation freeing object

Posted on 2002-06-06
9
Medium Priority
?
321 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 520 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: 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.

Question has a verified solution.

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

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

886 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