PDA

View Full Version : how to debug access violation?



noeska
26-12-2008, 08:59 AM
Delphi2005 Pro on Windows XP SP3
Access Violation Exception after call to free

I am getting an access violation (some number) in an application i am developeing with delphi2005pro.
The problem the access violation is raised after freeandnill(someobjec) is called.

This is the situation. In an onclick event for an button an object is created, used an finaly freeandnil is called. Only after the freeandnill is called the access violation is thrown.
Also i tried stepping through the code all the way butt all went well and after the freeandnill call the exception is there again. Now how do i debug that?

I do suspect something is going wrong in the object. But i am unable to debug it for now.

Any pointers to where i should look debug it in another way?

The offending class/object is the vfs i am developing.

Thanks for your help in advance.

Chesso
26-12-2008, 10:47 AM
Well I am quite rusty and more of a trial and error debugger myself :P.

But the first thing I would check is that the object is not being referenced in any way after or potentially during your free and nil call.

Also you tried commenting out 1 statement or the other and seeing what sort of effect it has (I don't think you really need to nil these days do you?).

But if you can free and comment out nil and no error, well, do you really need to nil in that case lol.

noeska
26-12-2008, 10:55 AM
The calling part looks like this:

procedure TForm1.bDynWriteClick(Sender: TObject);
var
MyStream: TVirtualFileStream;
begin
MyStream:= TVirtualFileStream.Create(eMemoName.Text, fmCreate, TestDataFile);
mTest.Lines.SaveToStream(MyStream);
FreeAndNil(MyStream);
lTest.Caption := 'OK';
end;
Even lTest.Captions is executed and then on end; the access violation error is shown. So no more calling to MyStream after the free.

noeska
26-12-2008, 12:38 PM
Solved.

The offending line was:
procedure TVirtualFileSystem.GetBlockId(var existingblock: Boolean; iEndBlock: Integer; var blocklistsize: Integer; var BlockList: TBlockList; i: Integer; var b: Int64; var nb: Int64);
Where i did not make blocklistsize var.
As a result of that i and blocklistsize went out of sync. And when writing outside of BlockList it destroys the object based on TVirtualFileSystem.
Wow this was a hard one to debug. Better take more care in the future when using refactoring. And for now give i a more meaningfull name.

Chesso
27-12-2008, 07:31 AM
Haha yep, the deeper you go, the harder it is to get back out again lol.