Re: Sending records with Indy 10 : Problem with String

Giganews Newsgroups
Subject: Re: Sending records with Indy 10 : Problem with String
Posted by:  Remy Lebeau (Indy Team) (no.spam@no.spam.com)
Date: Mon, 6 Nov 2006

"Christian rodriguez" <cdrodrigu…@ambimetrics.com> wrote in message
news:5427526D330EE340cdrodrigu…@ambimetrics.com...

> In Indy 10 I have the following code:

You cannot send records that contain AnsiString values (or any other
dynamically-allocated type).  String values have to be sent individually.

> then, when I try to access de name field there is a access violation
error.

As it should be.  You are not taking the AnsiString's dynamic nature into
account at all.  An AnsiString is actually just a pointer to a separate
memory block that holds the actual characters.  While sending the record the
way you are, you are sending the pointer value itself, not the characters
that are being pointed to.  The reader then ends up receiving a pointer
value that is outside of its valid address space.  That is why it gets the
AV.

In order to send a record that contains strings, you have to send the
strings separately.  That way, the actual characters are sent, not the
pointer that is pointing to them.  For example:

    --- Sender ---

    var
        ori: eje;
    begin
        with AContext.Connection.IOHandler do
        begin
            Write(ori.num);
            Write(ori.num2);
            Write(Length(ori.name));
            Write(ori.name);
        end;
    end;

    --- Receiver ---

    var
        dest: eje;
    begin
        with AContext.CanalComandos.IOHandler do
        begin
            dest.num := ReadInteger;
            dest.num2 := ReadInteger;
            dest.name := ReadString(ReadInteger);
        end;
    end;

> If I use a record without a string field the transmission is successful.

As it should be, because the record no longer contains dynamically allocated
data, so there is no errors in sending the data.

> I´m having also problems sending and receiving Objects, is
> there any consideration to take into account???

You can't send objects at all.  You would have to copy their data into a
formatted buffer or stream, and then send/receive that instead.

Gambit

Replies

None

In response to

Sending records with Indy 10 : Problem with String posted by Christian rodriguez on Sat, 4 Nov 2006