Note that there are some explanatory texts on larger screens.

plurals
  1. POAccessing a public field from another class failed with an access violation
    text
    copied!<p>I have multiple files. In the <code>main.pas</code> file I am passing the TImage instance present in the main form to the display class, which then sets it as a property of it's class. </p> <p>The image can be used without any problems inside the class, but I have access violation probelms in any other class, which tries to use this property, no matter what I do. </p> <p>Here are some code snippets to demonstrate the problem:</p> <h2>main.pas</h2> <pre><code>unit main; interface uses snake, display, ExtCtrls, Classes, Controls, Windows, Messages, SysUtils, Variants, Graphics, Forms, Dialogs, StdCtrls; type TfrmGameScreen = class(TForm) Image: TImage; procedure FormCreate(Sender: TObject); private { Private declarations } public { Public declarations } end; var frmGameScreen: TfrmGameScreen; snake: TSnake; display: TDisplay; implementation {$R *.dfm} procedure TfrmGameScreen.FormCreate(Sender: TObject); begin DoubleBuffered := True; display := TDisplay.create(Image); end; end. </code></pre> <hr> <h2>display.pas</h2> <pre><code>unit display; interface uses ExtCtrls, Graphics; type TDisplay = class public image: TImage; constructor create(img: TImage); end; TDraw = class(TDisplay) public procedure rectangle(x1, y1, x2, y2: integer); end; implementation constructor TDisplay.create(img: TImage); begin image := img; image.Canvas.Rectangle(100, 100, 150, 150); end; procedure TDraw.rectangle(x1, y1, x2, y2: integer); begin // image.canvas.rectangle(x1, y1, x2, y2); THIS IS WHERE THE ACCESS VIOLATION ARISES end; end. </code></pre> <p>snake.pas</p> <pre><code>unit snake; interface uses display, Dialogs, sysUtils, Graphics, ExtCtrls; type TBlock = record width, height: integer; end; TCoordinate = record x, y: integer; end; TVector = TCoordinate; TSnake = class public position: TCoordinate; direction: integer; block: TBlock; velocity: TVector; constructor create(initialPosition: TCoordinate); procedure draws(x, y: integer); procedure move(x, y: integer); end; var display: TDisplay; draw: TDraw; implementation constructor TSnake.create(initialPosition: TCoordinate); begin position.x := initialPosition.x; position.y := initialPosition.y; velocity.x := 3; velocity.y := 3; block.height := 50; block.width := 50; end; procedure TSnake.draws(x, y: integer); begin display.clear; draw.rectangle(1, 2, 3, 4); //image.canvas.Rectangle(100, 100, 150, 150); //display.canvas.rectangle(x, y, x + block.width, y + block.height); end; procedure TSnake.move(x, y: integer); begin draws(x, y); end; end. </code></pre> <p><img src="https://i.stack.imgur.com/c1dnE.png" alt="This is the access violation error, that I&#39;m getting"></p> <p>I am using the TDraw object in another file. </p> <p>My goal is to use the TImage instance from the main.pas file in all of my other units. It works fine in the class who's instance the image is set to, however any other class, that references the instance throws up an error upon execution. </p> <p>My question is: How can this be? Is there any way to overcome this? Is there a better solution?</p> <p>I would appreciate any help provided.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload