Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

access violation freeing object

Posted on 2002-06-06
9
Medium Priority
?
320 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

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…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

704 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