Solved

Constructor Crash - Delphi 5

Posted on 2004-10-13
2
1,221 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
  • 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

707 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now