Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi property stackoverflow error

Tags:

delphi

My Property Class:

unit SubImage;

interface

type
TSubImage = class
private
  { private declarations }
  function getHeight: Integer;
  function getWidth: Integer;
  procedure setHeight(const Value: Integer);
  procedure setWidth(const Value: Integer);
protected
  { protected declarations }
public
  { public declarations }
  property width : Integer read getWidth write setWidth;
  property height : Integer read getHeight write setHeight;
published
  { published declarations }
end;
implementation

{ TSubImage }

function TSubImage.getHeight: Integer;
begin
  Result:= height;
end;

function TSubImage.getWidth: Integer;
begin
  Result:= width;
end;

procedure TSubImage.setHeight(const Value: Integer);
begin
  height:= Value;
end;

procedure TSubImage.setWidth(const Value: Integer);
begin
  width:= Value;
end;

end.

Assignment:

objSubImg.width:= imgOverview.width;
objSubImg.height:= imgOverview.heigh

Interesting Error:

stackoverflow at xxxxxx

I am learning to properties in Delphi. I created a class, but it gives an error. I couldn't understand, where is my mistake?

Also i dont understand why we use property instead of setter/getter methods. Anyway can someone help me, how can i fix this code ?

I can not set property value.

like image 817
Dauezevy Avatar asked Jan 16 '15 14:01

Dauezevy


People also ask

What is stackoverflow error c#?

A StackOverflowException is thrown when the execution stack overflows because it contains too many nested method calls. For example, suppose you have an app as follows: C# Copy. using System; namespace temp { class Program { static void Main(string[] args) { Main(args); // Oops, this recursion won't stop. } } }

Can you catch a StackOverflowException?

NET Framework 2.0, you can't catch a StackOverflowException object with a try / catch block, and the corresponding process is terminated by default. Consequently, you should write your code to detect and prevent a stack overflow.

What causes stack overflow c#?

What causes stack overflow? One of the most common causes of a stack overflow is the recursive function, a type of function that repeatedly calls itself in an attempt to carry out specific logic. Each time the function calls itself, it uses up more of the stack memory.

What is stack overflow exception java?

StackOverflowError is an error which Java doesn't allow to catch, for instance, stack running out of space, as it's one of the most common runtime errors one can encounter.


1 Answers

This is a non terminating recursion. The getter looks like this:

function TSubImage.getHeight: Integer;
begin
  Result := height;
end;

But height is the property. So the compiler re-writes this as:

function TSubImage.getHeight: Integer;
begin
  Result := getHeight;
end;

And that's a non-terminating recursion. Hence the stack overflow.

You need to declare fields to store the values:

type
  TSubImage = class
  private
    FHeight: Integer;
    FWidth: Integer;
    function getHeight: Integer;
    function getWidth: Integer;
    procedure setHeight(const Value: Integer);
    procedure setWidth(const Value: Integer);
  public
    property width: Integer read getWidth write setWidth;
    property height: Integer read getHeight write setHeight;
  end;

And then get and set the values:

function TSubImage.getHeight: Integer;
begin
  Result:= FHeight;
end;

procedure TSubImage.setHeight(const Value: Integer);
begin
  FHeight:= Value;
end;

And likewise for the other property.

In this simple example you don't need to use getter and setter functions. You could declare the properties like this:

property width: Integer read FWidth write FWidth;
property height: Integer read FHeight write FHeight;

But I guess you know that and are exploring how getter/setter functions work.

As for why we use properties rather than getter and setter functions, that comes down to clarity and readability of code. You can replace properties with getter and setter functions. After all, that's all that the compiler does. But it is often clearer to write:

h := obj.Height;
obj.Height := h*2;

than

h := obj.GetHeight;
obj.SetHeight(h*2);
like image 99
David Heffernan Avatar answered Sep 28 '22 03:09

David Heffernan