Re: Strange EAccessViolation which I cannot figure out...
by Ikke » Thu, 10 May 2007 18:56:08 GMT
Tom de Neef" < XXXX@XXXXX.COM > wrote in
news:4640b9a6$0$331$ XXXX@XXXXX.COM :
Hi Tom,
I've made some changes to the code, but the error still remains. I'll
document below what I have changed.
---
constructor TFileInfo.Create(const fname : String;
const fsize : Int64);
begin
inherited;
self.fname := fname;
self.fsize := fsize;
dbls := nil;
end;
---
Never mind the other variables, I left them out in the previous post. Two
problems with this constructor, however:
1. If I add inherited, I no longer can compile this. The error I receive
is 'E2008 - Incompatible types'.
2. Instead of directly creating a new TObjectList, I have chosen to
initialise dbls to nil. Am I correct in assuming that an empty
TObjectList takes up memory space? If so, then I'd prefer the nil
approach, because there will be a lot of TFileInfo objects, yet only a
few will have the need for this dbls TObjectList.
Since I'm not sure whether or not dbls is used, and is or isn't nil, I've
changed this to:
---
procedure TFileInfo.free;
begin
if not (dbls = nil) then
begin
dbls.free;
end;
inherited;
end;
---
This part remains as it was, no changes needed...
I've used ShowMessage to be notified what's going on here. Here are the
changes:
---
function TFileInfo.getDoubles() : TObjectList;
begin
if (dbls = nil) then
begin
ShowMessage('Dbls is nil');
end
else
begin
ShowMessage(IntToStr(dbls.Count));
end;
result := dbls;
end;
---
Also, I added the IntToStr, as you have suggested (this was indeed a
mistake).
The message 'Dbls is nil' does not pop up (as expected with my testcase),
but I still get an error when the else part is executed. If I break the
program, the line indicated is indeed the ShowMessage in this else part.
The error I receive is:
"First chance exception at $7C4EA4E1. Exception class EAccessViolation
with message 'Access violation at address 00460A19 in module 'DFR.exe'.
Read of address 706A2E39'. Process DFR.exe (1408)"
Yesterday I've already replied very briefly to this, but in short I can
verify that AddDoubles gets called prior to the GetDoubles call.
The fact that the 'else' part is executed in GetDoubles verifies this...
Yes, I still get the error... And alas, I still can't figure out why :(
Thanks for the good advice, I'll make sure to add decent
constructors/destructors in future.
Ikke