Programming: Exception thrown at impossible line

Linux howto's, compile information, information on whatever we learned on working with linux, MACOs and - of course - Products of the big evil....
Post Reply
User avatar
^rooker
Site Admin
Posts: 1483
Joined: Fri Aug 29, 2003 8:39 pm

Programming: Exception thrown at impossible line

Post 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.
Post Reply