Solved

Constructor Crash - Delphi 5

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

829 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