// exemplary data definition:
// pmydata=^tmydata
// tmydata=record
// astring:Shortstring;
// id:byte
// end;
procedure test_access_existing_share;
Var
Mydata_in1, mydata_in2: Pmydata;
mydata_seq1, mydata_seq2: Pmydata;
Mydata_out: TMyData;
filehandle: thandle;
share: TMMFWriter;
begin
share := TMMFWriter.Create;
filehandle := share.Createsharedmemspace(Privatespaceid, 5000);
// assigns new space and saves it to access in a dictionary
Assert(share.Shares[Privatespaceid]^ = filehandle);
// OK
// add first element
New(Mydata_in1);
// pointer to record
Mydata_in1^.Create(33);
share.WriteToMMF(filehandle, Mydata_in1, Sizeof(TMyData));
Mydata_out := share.ReadFromMMF<TMyData>(filehandle);
Assert(Mydata_out.id = 33);
// OK
// add 2 elements sequentially:
// first element
New(mydata_seq1);
mydata_seq1^.Create(34, '
');
Assert(mydata_seq1^.id = 34);
share.WriteToMMF(filehandle, mydata_seq1, Sizeof(TMyData));
// get again
Mydata_out := share.ReadFromMMF<TMyData>(filehandle, 0);
Assert(Mydata_out.id = 34);
// second element
New(mydata_seq2);
mydata_seq2^.Create(35);
Assert(mydata_seq2^.id = 35);
//Problemstelle (mit manuellem offset: +sizeof(tmydata)):
{ try to add second element at the end, by setting offset=sizeof(tmydata).
adding fails, wrong offset / wrong writing access?
raising custom exception "Error writing to shared memory" @copymemory
share.WriteToMMF(filehandle, mydata_seq2, Sizeof(TMyData), Sizeof(TMyData));//hier
// !!
// test get sequential elements, but only gets last added:
// !!!
// Mydata_out := share.ReadFromMMF<TMyData>(filehandle, Sizeof(TMyData));
// Assert(Mydata_out.id = 35);
}
// clean up
share.Closeshare(Privatespaceid);
Dispose(Mydata_in1);
Dispose(mydata_seq1);
Dispose(mydata_seq2);
share.Free;
end;