0

While trying to transfer an Imap TIdMessage from a Thread to the MainThread, I discovered that saving it to Stream and then loading it back does not work.

Log('Msg.MessageParts.Count='+Msg.MessageParts.Count.ToString,d_4); // 3 parts
AStream := TMemoryStream.Create;
MSG.SaveToStream(AStream);

MSG := TIdMessage.Create(nil);
MSG.LoadFromStream(ASTream);
Log('After Load: Msg.MessageParts.Count='+Msg.MessageParts.Count.ToString,d_4);// Zero parts

Is there something I can do about this? I need a copy of the entire TIdMessage.

4
  • 4
    Are you seeking AStream.Position back to 0 before calling Msg.LoadFromStream()? Also, why not just transfer the whole TIdMessage object as-is instead of a copy of its data via TMemoryStream? Sep 23 at 0:42
  • Oh no.. I made the same mistake again! I am going to hang it on the wall: Put the bloody Stream back to zero! Regarding transfering the actual TIdMessage from the Thread where its was created to the main thread: I got exceptions trying to manipulate MessageParts afterwards. But this may also have been caused by me not setting Streams to zero. .. Thanks Remy.
    – Bart Kindt
    Sep 23 at 3:28
  • Nothing in TIdMessage is tied to the thread that creates it. So you probably just have a bug in your code that tries to use TIdMessage across threads. Feel free to post a new question about that, if you want. Sep 23 at 5:50
  • You are right, it was an internal bug. I just stuffed up the resetting of the Stream counter in the attempt to resolve the original issue I had..
    – Bart Kindt
    Sep 23 at 9:59

1 Answer 1

2

As Remy Lebeau commented, I needed to set AStream.Position back to zero before calling Msg.LoadFromStream().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.