Solved

Constructor Crash - Delphi 5

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

911 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

19 Experts available now in Live!

Get 1:1 Help Now