Solved

Error with pointers, constructor and memory

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
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.

708 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