?
Solved

Error with pointers, constructor and memory

Posted on 2004-10-16
5
Medium Priority
?
249 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 27

Assisted Solution

by:kretzschmar
kretzschmar earned 80 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 1520 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

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…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

764 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