Solved

Error with pointers, constructor and memory

Posted on 2004-10-16
5
241 Views
Last Modified: 2010-04-05
TCachedData = class(TObject)
  LatticeAddress : ^TLattice;
  MargolusArrayAddressA : ^TMargolusArray;
  MargolusArrayAddressB : ^TMargolusArray;
  fitness : Integer;
  constructor create();
end;

TLattice and TMargolusArray are complex objects.

TCachedDataArray = class(TObject)
  caches : array of TCachedData;
  constructor create(size : Integer);
end;


constructor TCachedData.create();
begin
  self.fitness := 0;
end;

constructor TCachedDataArray.create(size : Integer);
var
  Teller : Integer;
begin
  setlength(caches, size+1);
  for Teller := 0 to size do
  begin
    self.caches[Teller].Create();
  end;
end;

Why does this code give me an error?

LatticeCache : TCachedDataArray;
LatticeCache.create(10);

Delphi gives me an access violation at the 'self.fitness := 0;' in constructor TCachedData.create();
Help much appreciated!
0
Comment
Question by:reynaerde
5 Comments
 
LVL 27

Assisted Solution

by:kretzschmar
kretzschmar earned 20 total points
ID: 12327292
hint:

constructor TCachedData.create();
begin
  inherited; //add this line
  self.fitness := 0;
end;

meikl ;-)
0
 

Author Comment

by:reynaerde
ID: 12327376
Well, that doesn't seem to change anything.. still get the same error.
Ok, I must have been sleeping: instead of

-self.caches[Teller].Create();

I should have been using:

-self.caches[Teller] := TCachedData.create();

I don't really understand what the difference is between the two, is it possible to have this working using the first way
(i.e. self.caches[Teller].Create();)?
If you can explain it to me I'd be happy to give you the points :)

0
 
LVL 26

Accepted Solution

by:
Russell Libby earned 380 total points
ID: 12327895
The difference is this:

In the first case, you are trying to call an object instance's Create(...) constructor. But.... you don't have an object instance yet. No memory has been set aside yet because the class constructor has not been called, which leads to the access violation.

In the second case, you are calling the class constructor and assigning the created object instance to your object variable.

Just remember that when calling Create(...), you need to call the constructor of the CLASS, and not the OBJECT. And no, it is not possible to make this work by using the first case.

Regards,
Russell


 
0
 

Author Comment

by:reynaerde
ID: 12327910
Thanks :)
0
 
LVL 6

Expert Comment

by:vadim_ti
ID: 12328018
constructor is a class method you can call it with class name or with a variable of  a class-reference type

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

Suggested Solutions

Title # Comments Views Activity
Can't connect to Outlook via TOutlookApplication component 15 118
JSON on ISAPI REST Server using Delphi 2 229
Path  to current project in Delphi. 2 71
Multiple image collision 13 69
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…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
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…

930 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

9 Experts available now in Live!

Get 1:1 Help Now