As described in comments you are attempting to create the object like this:
var
Stack: TStack;
....
Stack.Create;
This is a classic mistake, and one that we've all made. You are calling a method on an uninitialized instance variable.
In order to instantiate a class you need to write this:
Stack := TStack.Create;
On top of that I have the following comments:
- Use zero-based indexing for arrays. That's the convention everywhere in Delphi apart from anachronistic strings. And even that is changing in newer versions.
- Don't use static arrays for a stack unless you have a good reason for doing so. You'll just run the risk of running out of space. Or allocating more memory than you need. Use a dynamic array.
- Rather than a dynamic array, you could use
TList<T>
. - Even so, one wonders why you are making your own stack class when there is the perfectly good
TStack<T>
. - You store the address of a local variable in your stack. In
TStack.Add
you add@Frm
into the container. As soon asTStack.Add
returns,@Frm
is meaningless. That's becauseFrm
is a local variable whose life ends when the function that owns it returns. I think you want to take a copy ofFrm
.
Picking up item 5 in more detail, your record is declared like this:
Rec = Record
Form : ^TForm;
Maximized : Boolean;
Width,
Height,
left,
Top : Integer;
End;
It is a mistake to use ^TForm
. That is a pointer to a variable holding a pointer to an object. That's two levels of indirection, one too many. You must declare the Form
field to be of type TForm
. I suggest you revise the way Delphi object reference variables work. Delphi classes are what is known as reference types. A variable of type TMyClass
where TMyClass
is class(...)
is already a pointer. The language automatically de-references the pointer when you use the .
operator to access members.