[DUG] StrCopy problem

Bevan Edwards bevan at achieve.co.nz
Mon May 13 09:17:29 NZST 2013


Hi Ross,

That's interesting, because I would have thought that using 
StrCopy(Title, '12345') in this situation would have resulted in the 
same problem - the compiler must allocate the string differently in this 
case ;-)

Regards,

Bevan


On 13/05/2013 2:31 a.m., Ross Levis wrote:
>
> In the end I did this...
>
> var TitleStr: string='The DLL Title';
>
> var Title: pChar;
>
> Title := @TitleStr[1];
>
> I could then use CopyStr elsewhere.
>
> Cheers.
>
> *From:*delphi-bounces at listserver.123.net.nz 
> [mailto:delphi-bounces at listserver.123.net.nz] *On Behalf Of *Bevan Edwards
> *Sent:* Sunday, 12 May 2013 7:52 PM
> *To:* NZ Borland Developers Group - Delphi List
> *Subject:* Re: [DUG] StrCopy problem
>
> Hi Ross,
>
> You don't say which version of Delphi you're using, but I assume it's 
> pre-Unicode support, since you use PChar instead of PAnsiChar or 
> PWideChar.
>
> Out of curiosity I built a small test application and traced through 
> the source code for this snippet of code using the CPU window.  What I 
> noticed is that the address of the two strings assigned to 'a' and 'b' 
> are part of program memory.  More specifically, Delphi appears to 
> store these strings immediately after the "return" opcode for the 
> procedure and simply puts those addresses into 'a' and 'b'.  Since 
> this area of memory is protected while the application is running, 
> that's why you get the error.
>
> By using:
> a:=AllocMem(10);
> StrCopy(a, 'abcdefghi');
> b:='jklmnopqr';
> StrCopy(a, b);
>
> The variable 'b' now points to application memory while 'a' is 
> allocated a "write-enabled" memory address, so the access violation 
> doesn't occur.
>
> Regards,
>
> Bevan
>
>
> On 12/05/2013 5:30 p.m., Ross Levis wrote:
>
>     I did see a & b had a pointer address allocated.  It looked like a
>     normal pointer address range.  I'll try allocating 10 bytes to a
>     later and see what happens.
>
>     Ross.
>
>     *From:*delphi-bounces at listserver.123.net.nz
>     <mailto:delphi-bounces at listserver.123.net.nz>
>     [mailto:delphi-bounces at listserver.123.net.nz] *On Behalf Of *Bevan
>     Edwards
>     *Sent:* Sunday, 12 May 2013 4:48 PM
>     *To:* delphi at listserver.123.net.nz
>     <mailto:delphi at listserver.123.net.nz>
>     *Subject:* Re: [DUG] StrCopy problem
>
>     Actually, based on that article a:='abcdefghi' should assign 10
>     bytes and include the zero byte at the end. But I wonder if the
>     problem is due to where this data is stored and trying to copy b
>     to a results in an attempt to write to protected memory?
>
>     Have you tried allocating 10 bytes of memory to 'a' and then using
>     StrCopy?
>
>     Have you run the debugger and checked where the PChars 'a' and 'b'
>     are pointing to before StrCopy?
>
>
>
>
>     -------- Original message --------
>     From: Ross Levis <ross at stationplaylist.com
>     <mailto:ross at stationplaylist.com>>
>     Date:
>     To: 'NZ Borland Developers Group - Delphi List'
>     <delphi at listserver.123.net.nz <mailto:delphi at listserver.123.net.nz>>
>     Subject: Re: [DUG] StrCopy problem
>
>
>     Doesn't really help.
>
>     a := 'abcdefghi' does allocate 9 bytes of RAM.  I can access a and
>     b after it is assigned.  The problem is StrCopy crashes.  I would
>     expect "a" to have the same string as b once this is executed.
>
>     Ross.
>
>     *From:*delphi-bounces at listserver.123.net.nz
>     <mailto:delphi-bounces at listserver.123.net.nz>
>     [mailto:delphi-bounces at listserver.123.net.nz] *On Behalf Of *Keith
>     Allpress
>     *Sent:* Sunday, 12 May 2013 10:27 AM
>     *To:* 'NZ Borland Developers Group - Delphi List'
>     *Subject:* Re: [DUG] StrCopy problem
>
>     Perhaps:
>
>     http://rvelthuis.de/articles/articles-pchars.html
>
>     *From:*delphi-bounces at listserver.123.net.nz
>     <mailto:delphi-bounces at listserver.123.net.nz>
>     [mailto:delphi-bounces at listserver.123.net.nz] *On Behalf Of *Ross
>     Levis
>     *Sent:* Sunday, 12 May 2013 2:39 a.m.
>     *To:* 'NZ Borland Developers Group - Delphi List'
>     *Subject:* [DUG] StrCopy problem
>
>     var
>
>       a: pChar;
>
>       b: pChar;
>
>     begin
>
>       a := 'abcdefghi';
>
>       b := 'jklmnopqr';
>
>       StrCopy(a,b);
>
>     end;
>
>     Question:  Why does this code crash?
>
>
>
>
>     _______________________________________________
>
>     NZ Borland Developers Group - Delphi mailing list
>
>     Post:delphi at listserver.123.net.nz  <mailto:delphi at listserver.123.net.nz>
>
>     Admin:http://delphi.org.nz/mailman/listinfo/delphi
>
>     Unsubscribe: send an email todelphi-request at listserver.123.net.nz  <mailto:delphi-request at listserver.123.net.nz>  with Subject: unsubscribe
>
>
>
> _______________________________________________
> NZ Borland Developers Group - Delphi mailing list
> Post: delphi at listserver.123.net.nz
> Admin: http://delphi.org.nz/mailman/listinfo/delphi
> Unsubscribe: send an email to delphi-request at listserver.123.net.nz with Subject: unsubscribe

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://listserver.123.net.nz/pipermail/delphi/attachments/20130513/cba80ac0/attachment.html 


More information about the Delphi mailing list