Board index » delphi » How do I read data from a formatted file into my objects

How do I read data from a formatted file into my objects

I have submitted this for a friend.
Please use his mail-address if you want to correspond directly :-)
I will compile any responces to this post for him.
                /torben

============================================================

My problem is I want to read data from a formatted file into my
objects. My objects are derived from TPersistent, which has no
inherent methods for retrieving or storing data.

I've solved the problem by simple typecasting. For that purpose I've
made a class called TObjectStream, which is derived from TMemory-
Stream. The class has a method for reading data into an object:

procedure TObjectStream.ReadData(var Buffer: Pointer; Count: Word);
begin
  ReadBuffer(Buffer^, Count);     // Call inherited method
end;

The method is called like this:
  ReadData( Pointer(MyObject), MyObject.InstanceSize );

The procedure, which is rather dirty, is like this:
1) Create an ObjectStream object and read the data from the file into
   it using the LoadFromFile method.
2) Create a MyObject object and then call ObjectStream.ReadData using
   MyObject as parameter. This overwrites the fields of MyObject.

For some reason using ReadData causes an access violation exception,
when I try to destroy MyObject. I don't understand it, because the
adress of MyObject hasn't changed from when it was created, and I
know the right amount of data has been read into MyObject.

Can anyone please tell me what is wrong with the ReadData method?
I've tried several other ways of typecasting, but it always results
in the same error.

I have problems reading news, so please mail me directly.

    Troels Jakobsen
    tj2...@hdc.hha.dk

=========================================================

 

Re:How do I read data from a formatted file into my objects


Quote
sagi...@daimi.aau.dk wrote:
>My problem is I want to read data from a formatted file into my
>objects. My objects are derived from TPersistent, which has no
>inherent methods for retrieving or storing data.
>I've solved the problem by simple typecasting. For that purpose I've
>made a class called TObjectStream, which is derived from TMemory-
>Stream. The class has a method for reading data into an object:
>procedure TObjectStream.ReadData(var Buffer: Pointer; Count: Word);
>begin
>  ReadBuffer(Buffer^, Count);     // Call inherited method
>end;
>The method is called like this:
>  ReadData( Pointer(MyObject), MyObject.InstanceSize );
>The procedure, which is rather dirty, is like this:
>1) Create an ObjectStream object and read the data from the file into
>   it using the LoadFromFile method.
>2) Create a MyObject object and then call ObjectStream.ReadData using
>   MyObject as parameter. This overwrites the fields of MyObject.
>For some reason using ReadData causes an access violation exception,
>when I try to destroy MyObject. I don't understand it, because the
>adress of MyObject hasn't changed from when it was created, and I
>know the right amount of data has been read into MyObject.
>Can anyone please tell me what is wrong with the ReadData method?
>I've tried several other ways of typecasting, but it always results
>in the same error.

IMO, the reason this approach fails is that the bytes representing a
Delphi object do not only contain the data fields of the object (which
conceivably could be stored and retrieved in this way), but also will
contain some housekeeping information such as the virtual method table
address, information about runtime type information,  and possibly
other things. As these housekeeping values will not necessarily be
valid when they are read back there could very well be an access
violation. The conclusion is: don't use this quick-and-dirty method to
store objects. You should write methods to store and retrieve the
object fields directly.

Hope this helps

David

David A. Schweizer
iec ProGAMMA
d.a.schwei...@gamma.rug.nl

Other Threads