[DUG] Pointers help
Stefan Mueller
muellers at orcl-toolbox.com
Sun Nov 5 16:13:05 NZDT 2017
Just noticed this one here:
>>There is a IOHandler.Write function that supports TStream but the memory
stream is being appended to in a separate thread so I cant go changing the
Position I want to access the data from.
You are aware that tmemorystream isnt threadsafe, right? You will need to
use a TCriticalSection to lock concurrent writing and reading threads
if
you only have 2 locations in your code (one read, one write) then you can
wrap them in tcriticalsection there .. if you have more locations than I
suggest you think about what is called the RCU-pattern (read-copy-update)
for passing data between threads in a safe manner.
Kind regards,
Stefan Müller,
R&D Manager
ORCL Toolbox Ltd.
Auckland, New Zealand
P Please consider the environment before printing this email
This message is intended for the addressee named above and may contain
privileged or confidential information.
If you are not the intended recipient of this message you must not use,
copy, distribute or disclose it to anyone.
From: delphi-bounces at listserver.123.net.nz
[mailto:delphi-bounces at listserver.123.net.nz] On Behalf Of Stefan Mueller
Sent: Sunday, 5 November 2017 3:25 p.m.
To: 'NZ Borland Developers Group - Delphi List'
Subject: Re: [DUG] Pointers help
Ross,
array of bytes isnt the same as a pointer (nor is the same as a static
array with a fixed length). Dynamic arrays are a managed data structure in
Delphi that contain information such as length and a reference count (so
Delphi can manage memory and free them if no longer used). Converting data
from a memory-pointer to a dynamic array isnt possible by typecasting
alone, you will need to copy the data as in your existing code Indies
idGlobal.pas contains a helper function which does the same as your code:
function RawToBytes(const AValue; const ASize: Integer): TIdBytes;
Kind regards,
Stefan Müller,
R&D Manager
ORCL Toolbox Ltd.
Auckland, New Zealand
P Please consider the environment before printing this email
This message is intended for the addressee named above and may contain
privileged or confidential information.
If you are not the intended recipient of this message you must not use,
copy, distribute or disclose it to anyone.
From: delphi-bounces at listserver.123.net.nz
[mailto:delphi-bounces at listserver.123.net.nz] On Behalf Of Ross Levis
Sent: Sunday, 5 November 2017 2:19 p.m.
To: delphi at delphi.org.nz
Subject: [DUG] Pointers help
Im writing data to an Indy IOHandler by moving the data from a
TMemoryStream.Memory to a TIDBytes (array of byte) before writing it as I
cannot find how to do it otherwise.
This works...
AudioMS: TMemoryStream;
Buffer: TIdBytes; // array of byte
MemPos: PByte;
....
MemPos := AudioMS.Memory;
Inc(MemPos,FromPos);
ToPos := Player.AudioMS.Position;
SetLength(Buffer,ToPos-FromPos);
Move(MemPos^,Buffer[0],Length(Buffer));
IOHandler.WriteDirect(Buffer);
FromPos := ToPos;
But there has to be a better way of writing direct from the AudioMS.Memory
and getting rid of the Move operation.
Ive tried the following but produces pointer errors or access violations.
IOHandler.WriteDirect(TIdBytes(MemPos),ToPos-FromPos);
Also tried...
IOHandler.WriteDirect(TIdBytes(MemPos^),ToPos-FromPos);
There is a IOHandler.Write function that supports TStream but the memory
stream is being appended to in a separate thread so I cant go changing the
Position I want to access the data from.
How should it be done?
Cheers,
Ross.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://listserver.123.net.nz/pipermail/delphi/attachments/20171105/0c96d55d/attachment-0001.html
More information about the Delphi
mailing list