Is it necessary to set position of a stream? lazarus / 21-General ---------------------------------------------------------------------- egsuh · 2025-04-14 01:57 I've been using following structure for long time, without any problem, but recently the operations are not stable. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure UploadADataSet(tblname, cond:string; DataSet:TDataSet); var    AStrm: TMemoryStream; begin     AStrm := TMemoryStream.Create;     (DataSet as TCustomBufDataSet).SaveToStream(AStrm);      with SQLQuery1 do begin           SQL.Text := 'select * from ' + tblname + ' where ' + cond;           Open;          Close;            AStrm.Position := 0;  // is this necessary?           LoadFromStream(AStrm);            ApplyUpdates;     end;     AStrm.Free; end;          This may look strange, but I found this is quite neat method to copy / and update a subset of whole table. I may simply call "ApplyUpdates". In the past, this approach worked fine, without specifying "Astrm.Position := 0". But suddenly this operates unstable --- sometimes works, and sometimes raise exceptions. So I added "Astrm.Position := 0" and have to watch for some time. ---------------------------------------------------------------------- H₂SO₄ · 2025-04-14 05:20 The documentation doesn't say anything about seeking, and searching  bufdataset.pas  for  "Seek"  and  "Position"  only yields results for local variables (not interesting) or when using a  TDataPacketFormat  other than  dfDefault  (which doesn't apply here), so I would say it's necessary. (Personally I'd also find it conceptually more correct for procedures operating on streams to not care about absolute position) ---------------------------------------------------------------------- Thaddy · 2025-04-14 06:58 No, not needed. LoadFromStream does that for you. LoadFromStream assumes you want the whole stream. The case is different if you want to read partial with read semantics: in that case the stream pointer from savetostream is not reset and you need to do that manually. savetostream leaves the stream pointer at eof. ---------------------------------------------------------------------- TRon · 2025-04-14 15:39 Quote from: Thaddy on April 14, 2025, 08:58:59 amNo, not needed. LoadFromStream does that for you. What does loadfromstream do for you ? If implied that it starts reading a stream from position 0 then then that statement is false advertising. Perhaps it does for this particular case but it is nowhere documented nor implied. ---------------------------------------------------------------------- Thaddy · 2025-04-14 16:13 Indeed, not on all loadfromstream occurances. ---------------------------------------------------------------------- TRon · 2025-04-14 16:33 What's bothersome here: the inconsistency throughout existing implementations (I've been burned by that many times because of making the wrong assumption). Note that I haven't looked at this particular implementation (but any programmer using it actually should or at least make a simple test for verification). edit: revised answer so that it (still) makes sense as response.