Skip to content
Advertisement

Get Access violation when I run my pascal program, how to solve? (With free pascal)

This is PointClass.pas

{$mode objfpc} // Directive to be used for defining classes
{$m+}          // Directive to be used for using constructor

UNIT PointClass ;
INTERFACE
TYPE Point = CLASS(TObject)
    PRIVATE
        x : INTEGER ;
        y : INTEGER ;
    PUBLIC          
        (* Setter *)
        PROCEDURE setX (ix : INTEGER) ;
        PROCEDURE setY (iy : INTEGER) ;
        PROCEDURE setPoint (ix, iy : INTEGER) ;

        (* Getter *)
        FUNCTION getX : INTEGER ;
        FUNCTION getY : INTEGER ;
END;

IMPLEMENTATION   
USES Classes, SysUtils ;

PROCEDURE Point.setX (ix : INTEGER) ;
BEGIN
    x := ix ; {line 26}
END ;

PROCEDURE Point.setY (iy : INTEGER) ;
BEGIN
    y := iy ;
END ;

PROCEDURE Point.setPoint (ix, iy : INTEGER) ;
BEGIN
    x := ix ;
    y := iy ;
END ;

FUNCTION Point.getX : INTEGER ;
BEGIN
    getX := x ;
END ;

FUNCTION Point.getY : INTEGER ;
BEGIN
    getY := y ;
END ;
END.

and main.pas

PROGRAM TESTSHAPE ;
USES PointClass ;
VAR
    p1 : Point ;

BEGIN
    p1.Create ;

    p1.setX (2) ; {line 9}
    p1.setY (1) ;
    WRITELN ('X is ', p1.getX, ' and Y is ', p1.getY) ;

    p1.setPoint (23, 2) ;
    WRITELN ('X is ', p1.getX, ' and Y is ', p1.getY) ;
END.

I compile with

fpc -Tlinux -Criot -gl main.pas


This can pass the compiler, but when I run program, its said :

An unhandled exception occurred at $0000000000422AC5 :
EAccessViolation : Access violation
$0000000000422AC5 line 26 of PointClass.pas
$00000000004001DA line 9 of main.pas

I try so hard, and search a lot, but still can’t solve this problem.
(Sorry for my bad English!)

Advertisement

Answer

I found it. main.pas is wrong :

p1.Create ; {Wrong}

The correct line is :

p1 := Point.Create ;
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement