Solved

Constructor Crash - Delphi 5

Posted on 2004-10-13
2
1,229 Views
Last Modified: 2012-06-27
Note: This is using Delphi 5 on Windows XP Professional

I'm new to class programming, I've previously just done windows interface stuff. However, on an interface I'm currently working on, I saw the opportunity for a class to really simplify things.

I wrote the class, and it compiles; however, I can't get Delphi to actually use it without giving a EAccessViolation exception. I got frustrated with this, and tried making a really simple little nonsense class to figure out what the problem is, and I still can't get it to work (same error).

I'm sure I'm just missing something fundamental, since I haven't done this before. However, I can't figure out what is going on, and I'd really appreciate the help.

Here's the little testing class I made for testing things out:

======================================

unit TestClass;

interface

type
    TTestClass = class(TObject)
    public
        PublicVar: integer;
        constructor Create(InitialVal: integer);
    end;

implementation

constructor TTestClass.Create(InitialVal: integer);
begin
    inherited Create();
    PublicVar := InitialVal;
end;
 
end.

======================================

To use it, I made a new project, then I have this code in the unit for the auto-generated form:

======================================

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
    TestClass;

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  TC: TTestClass;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
    TC.Create(23);
end;

end.

======================================

Tracing the error, it happens when the assignment statement in the constructor of TTestClass is called. The exact text of the message is "Project Project1.exe raised exception class EAccessViolation with message 'Access violation at address 0043E0CC in module 'Project1.exe', write of address 00000004'. Process stopped. Use Step or Run to continue."

Also, as a little side question:
I'm looking at upgrading from Delphi 5 to something newer. I was going to get that D7/D8 for the price of one combo deal that Borland was offering, however, Delphi 2005 just came out. Since this is just a personal workstation, I'm open to pretty much whatever I want to get, I don't have to worry about company standards or anything; my only real requirement is being able to develop for Win32. Any tips on whether I'd be better off with D7 or D2005 for Win32 development? (The main question is 425 points, this little sub-question is worth 75).

Thanks for the help!
0
Comment
Question by:cypherpunk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 7

Accepted Solution

by:
LRHGuy earned 500 total points
ID: 12300491
This:

procedure TForm1.FormCreate(Sender: TObject);
begin
    TC.Create(23);
end;

Should be:

TC:=TTestClass.Create(23);

0
 
LVL 7

Expert Comment

by:LRHGuy
ID: 12300514
I like D7, and would suggest it. More IDE functions, more gadgets, more more more. You won't be disappointed.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question