Solved

Error with pointers, constructor and memory

Posted on 2004-10-16
5
242 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Mydac connection data base issue 3 158
Connection between libmysql.dll and MySQL Versions 7 99
error 1.1 400 Bad request idhttp delphi 18 92
Dev Express grid collapse 2 45
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…

832 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