<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40"><head><meta http-equiv=Content-Type content="text/html; charset=iso-8859-1"><meta name=Generator content="Microsoft Word 14 (filtered medium)"><style><!--
/* Font Definitions */
@font-face
        {font-family:"MS Mincho";
        panose-1:2 2 6 9 4 2 5 8 3 4;}
@font-face
        {font-family:"MS Mincho";
        panose-1:2 2 6 9 4 2 5 8 3 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
        {font-family:Tahoma;
        panose-1:2 11 6 4 3 5 4 4 2 4;}
@font-face
        {font-family:Webdings;
        panose-1:5 3 1 2 1 5 9 6 7 3;}
@font-face
        {font-family:"\@MS Mincho";
        panose-1:2 2 6 9 4 2 5 8 3 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0cm;
        margin-bottom:.0001pt;
        font-size:11.0pt;
        font-family:"Calibri","sans-serif";}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:purple;
        text-decoration:underline;}
p.MsoAcetate, li.MsoAcetate, div.MsoAcetate
        {mso-style-priority:99;
        mso-style-link:"Balloon Text Char";
        margin:0cm;
        margin-bottom:.0001pt;
        font-size:8.0pt;
        font-family:"Tahoma","sans-serif";}
span.EmailStyle17
        {mso-style-type:personal;
        font-family:"Calibri","sans-serif";
        color:#1F497D;}
span.EmailStyle18
        {mso-style-type:personal;
        font-family:"Calibri","sans-serif";
        color:#1F497D;}
span.EmailStyle19
        {mso-style-type:personal-reply;
        font-family:"Calibri","sans-serif";
        color:#1F497D;}
span.BalloonTextChar
        {mso-style-name:"Balloon Text Char";
        mso-style-priority:99;
        mso-style-link:"Balloon Text";
        font-family:"Tahoma","sans-serif";}
.MsoChpDefault
        {mso-style-type:export-only;
        font-size:10.0pt;}
@page WordSection1
        {size:612.0pt 792.0pt;
        margin:72.0pt 72.0pt 72.0pt 72.0pt;}
div.WordSection1
        {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]--></head><body lang=EN-US link=blue vlink=purple><div class=WordSection1><p class=MsoNormal><span style='color:#1F497D'>Just noticed this one here:<o:p></o:p></span></p><p class=MsoNormal><span style='color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal><span style='color:#1F497D'>>>There is a IOHandler.Write function that supports TStream but the memory stream is being appended to in a separate thread so I can’t go changing the Position I want to access the data from.<o:p></o:p></span></p><p class=MsoNormal><span style='color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal><span style='color:#1F497D'>You are aware that tmemorystream isn’t 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. <o:p></o:p></span></p><div><p class=MsoNormal><span style='font-family:"Arial","sans-serif";color:#1F497D'><br><br><o:p></o:p></span></p><p class=MsoNormal style='margin-bottom:12.0pt'><span style='font-family:"Arial","sans-serif";color:black'>Kind regards,<br><br></span><span style='color:#1F497D'><o:p></o:p></span></p><p class=MsoNormal><b><span style='font-family:"Arial","sans-serif";color:black'>Stefan Müller</span></b><span style='font-family:"Arial","sans-serif";color:black'>,<br>R&D Manager</span><span style='font-family:"Arial","sans-serif";color:#1F497D'><br><br></span><b><span style='font-family:"Arial","sans-serif";color:white;background:red'>ORCL</span></b><span style='color:#1F497D'> </span><b><span style='font-family:"Arial","sans-serif";color:black'>Toolbox Ltd.</span></b><span style='color:#1F497D'> <br></span><span style='font-family:"Arial","sans-serif";color:black'>Auckland, New Zealand</span><span style='color:#1F497D'> <o:p></o:p></span></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span style='font-size:9.0pt;font-family:Webdings;color:#777777'><br>P</span><span style='font-size:9.0pt;font-family:"Arial","sans-serif";color:#777777'> Please consider the environment before printing this email<br><br>This message is intended for the addressee named above and may contain privileged or confidential information.<br>If you are not the intended recipient of this message you must not use, copy, distribute or disclose it to anyone.<o:p></o:p></span></p></div><p class=MsoNormal><span style='color:#1F497D'><o:p> </o:p></span></p><div><div style='border:none;border-top:solid #B5C4DF 1.0pt;padding:3.0pt 0cm 0cm 0cm'><p class=MsoNormal><b><span style='font-size:10.0pt;font-family:"Tahoma","sans-serif"'>From:</span></b><span style='font-size:10.0pt;font-family:"Tahoma","sans-serif"'> delphi-bounces@listserver.123.net.nz [mailto:delphi-bounces@listserver.123.net.nz] <b>On Behalf Of </b>Stefan Mueller<br><b>Sent:</b> Sunday, 5 November 2017 3:25 p.m.<br><b>To:</b> 'NZ Borland Developers Group - Delphi List'<br><b>Subject:</b> Re: [DUG] Pointers help<o:p></o:p></span></p></div></div><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal><span style='color:#1F497D'>Ross,<o:p></o:p></span></p><p class=MsoNormal><span style='color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal><span style='color:#1F497D'>“array of bytes” isn’t 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 isn’t 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;<o:p></o:p></span></p><div><p class=MsoNormal><span style='font-family:"Arial","sans-serif";color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-family:"Arial","sans-serif";color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal style='margin-bottom:12.0pt'><span style='font-family:"Arial","sans-serif";color:black'>Kind regards,</span><span style='color:#1F497D'><o:p></o:p></span></p><p class=MsoNormal><b><span style='font-family:"Arial","sans-serif";color:black'>Stefan Müller</span></b><span style='font-family:"Arial","sans-serif";color:black'>,<br>R&D Manager</span><span style='font-family:"Arial","sans-serif";color:#1F497D'><br><br></span><b><span style='font-family:"Arial","sans-serif";color:white;background:red'>ORCL</span></b><span style='color:#1F497D'> </span><b><span style='font-family:"Arial","sans-serif";color:black'>Toolbox Ltd.</span></b><span style='color:#1F497D'> <br></span><span style='font-family:"Arial","sans-serif";color:black'>Auckland, New Zealand</span><span style='color:#1F497D'> <o:p></o:p></span></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span style='font-size:9.0pt;font-family:Webdings;color:#777777'><br>P</span><span style='font-size:9.0pt;font-family:"Arial","sans-serif";color:#777777'> Please consider the environment before printing this email<br><br>This message is intended for the addressee named above and may contain privileged or confidential information.<br>If you are not the intended recipient of this message you must not use, copy, distribute or disclose it to anyone.<o:p></o:p></span></p></div><p class=MsoNormal><span style='color:#1F497D'><o:p> </o:p></span></p><div><div style='border:none;border-top:solid #B5C4DF 1.0pt;padding:3.0pt 0cm 0cm 0cm'><p class=MsoNormal><b><span style='font-size:10.0pt;font-family:"Tahoma","sans-serif"'>From:</span></b><span style='font-size:10.0pt;font-family:"Tahoma","sans-serif"'> <a href="mailto:delphi-bounces@listserver.123.net.nz">delphi-bounces@listserver.123.net.nz</a> [<a href="mailto:delphi-bounces@listserver.123.net.nz">mailto:delphi-bounces@listserver.123.net.nz</a>] <b>On Behalf Of </b>Ross Levis<br><b>Sent:</b> Sunday, 5 November 2017 2:19 p.m.<br><b>To:</b> <a href="mailto:delphi@delphi.org.nz">delphi@delphi.org.nz</a><br><b>Subject:</b> [DUG] Pointers help<o:p></o:p></span></p></div></div><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal><span lang=EN-NZ style='font-size:12.0pt;color:#1F497D'>I’m 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.<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-NZ style='font-size:12.0pt;color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-NZ style='font-size:12.0pt;color:#1F497D'>This works...<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-NZ style='font-size:12.0pt;color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-NZ style='font-size:12.0pt'>AudioMS: TMemoryStream;<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-NZ style='font-size:12.0pt'>Buffer: TIdBytes; // array of byte<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-NZ style='font-size:12.0pt'>MemPos: PByte;<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-NZ style='font-size:12.0pt'>....<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-NZ style='font-size:12.0pt'>MemPos := AudioMS.Memory;<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-NZ style='font-size:12.0pt'>Inc(MemPos,FromPos);<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-NZ style='font-size:12.0pt'>ToPos := Player.AudioMS.Position;<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-NZ style='font-size:12.0pt'>SetLength(Buffer,ToPos-FromPos);<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-NZ style='font-size:12.0pt'>Move(MemPos^,Buffer[0],Length(Buffer));<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-NZ style='font-size:12.0pt'>IOHandler.WriteDirect(Buffer);<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-NZ style='font-size:12.0pt'>FromPos := ToPos;<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-NZ style='font-size:12.0pt;color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-NZ style='font-size:12.0pt;color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-NZ style='font-size:12.0pt;color:#1F497D'>But there has to be a better way of writing direct from the AudioMS.Memory and getting rid of the Move operation.<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-NZ style='font-size:12.0pt;color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-NZ style='font-size:12.0pt;color:#1F497D'>I’ve tried the following but produces pointer errors or access violations.<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-NZ style='font-size:12.0pt;color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-NZ style='font-size:12.0pt'>IOHandler.WriteDirect(TIdBytes(MemPos),ToPos-FromPos);<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-NZ style='font-size:12.0pt;color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-NZ style='font-size:12.0pt;color:#1F497D'>Also tried...<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-NZ style='font-size:12.0pt'>IOHandler.WriteDirect(TIdBytes(MemPos^),ToPos-FromPos);<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-NZ style='font-size:12.0pt;color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-NZ style='font-size:12.0pt;color:#1F497D'>There is a IOHandler.Write function that supports TStream but the memory stream is being appended to in a separate thread so I can’t go changing the Position I want to access the data from. <o:p></o:p></span></p><p class=MsoNormal><span lang=EN-NZ style='font-size:12.0pt;color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-NZ style='font-size:12.0pt;color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-NZ style='font-size:12.0pt;color:#1F497D'>How should it be done?<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-NZ style='font-size:12.0pt;color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-NZ style='font-size:12.0pt;color:#1F497D'>Cheers,<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-NZ style='font-size:12.0pt;color:#1F497D'>Ross.<o:p></o:p></span></p></div></body></html>