Page 1 of 1

Programming: Exception thrown at impossible line

Posted: Tue Sep 20, 2005 12:17 pm
by ^rooker
[PROBLEM]
This problem occured when writing in Delphi.

Here's my example code:

Code: Select all

type
  TImpField = class(TObject)
    ...
    ...
  end;
  PImpField = ^TImpField;

============================================
procedure initFields;
var
  pItem: PImpField;

begin
    ImportList := TList.Create;
    pItem^ := TImpField.Create;
    if (pItem <> nil) then
    begin
        pItem^.Table := 'Project';
        pItem^.Name  := 'Name';
        pItem^.Value := 'Testing';
        ImportList.Add(pItem);
    end;
end;
"pItem^" seems to be instantiated correctly, because its values "Table, Name, ..." are written correctly (checked by evaluating the variable in the IDE...)

BUT: as soon as the end of procedure "initFields" is reached, an access violation is thrown - and to make things REALLY strange: The error-pointer sits on the last "end;" of the procedure.


[SOLUTION]
pItem was not instantiated correctly.
Replacing

Code: Select all

begin
    ImportList := TList.Create;
    pItem^ := TImpField.Create;
with

Code: Select all

begin
    ImportList := TList.Create;
    [b]new(pItem);[/b]
    pItem^ := TImpField.Create;

solved it.