Author Topic: Copy sometimes does not copy...  (Read 1319 times)

0 Members and 1 Guest are viewing this topic.

Brian Alvarez

  • Guest
Copy sometimes does not copy...
« on: January 27, 2019, 05:33:16 AM »
This code works once:

Code: [Select]
        sys addr = (hBuffer + offset)
        string bb = space(ln+1)
        copy0 @bb, addr, ln
        return bb

Second time returns an empty string. Apparently when it deallocates space for bb, it overwrites the data at addr. Using this works fine:

Code: [Select]
        sys addr = (hBuffer + offset)
        string bb = space(ln+1)
        copy0 @bb, addr, ln
        string cc = bb
        return cc

Also... how would i do it with copy instead of copy0? replacing copy0 with copy crashes.

Charles Pegge

  • Guest
Re: Copy sometimes does not copy...
« Reply #1 on: January 27, 2019, 06:38:46 AM »
Hi Brian,

Copy0(dest,src)  is for null-terminated ascii

Copy00(dest,src)  is for null-terminated unicode

Copy(dest,src,n) is for any

Code: [Select]
sys addr = (hBuffer + offset)
        string bb = space(ln) 'also has invisible null terminators
        copy strptr(bb), addr, ln
        return bb

Brian Alvarez

  • Guest
Re: Copy sometimes does not copy...
« Reply #2 on: January 27, 2019, 07:08:11 AM »
Unfortunately, that didn't work. :(

Code: [Select]
        sys addr = (hBuffer + offset)
        string bb = space(ln)
        copy strptr(bb), addr, ln
        return bb

This works:

Code: [Select]
        sys addr = (hBuffer + offset)
        string bb = space(ln+1)
        copy0 @bb, addr, ln
        string cc = bb
        return cc

But is not ideal. Without cc, the code overwrites addr. Can you check how class functions deallocate space for returned strings? It seems like directly returning bb it is overwritting at addr.

Added:
I will update today, to see if an updated Oxygen has fixed this. :)
« Last Edit: January 27, 2019, 07:23:41 AM by Brian Alvarez »

Charles Pegge

  • Guest
Re: Copy sometimes does not copy...
« Reply #3 on: January 27, 2019, 08:02:23 AM »
copy is one of the oldest and most frequently used o2 functions.

full example:

Code: [Select]
function f(sys p, int le) as string
  string s=space(le)
  copy strptr(s),p,le
  return s
end function

print f(strptr("12345"),4)
'1234

The caller automatically frees the returned o2 string when it goes out of scope.



Charles Pegge

  • Guest
Re: Copy sometimes does not copy...
« Reply #4 on: January 27, 2019, 11:26:25 AM »
You may be encountering a string/local object problem:

If an object is created locally within a function, and has initialised string members, it cannot be returned from that function without losing its string members to the local garbage collector.

For this reason, an object should use bstring members instead of string members,  and free them explicitly in a destructor.

Brian Alvarez

  • Guest
Re: Copy sometimes does not copy...
« Reply #5 on: January 27, 2019, 12:33:36 PM »
This code works great by itself. I dont know what is happening in my program that is overwriting the original memory block.
i will see if i can post an example that duplicates the problem, there is a possibility that this is being caused by something else.
« Last Edit: January 27, 2019, 12:42:32 PM by Brian Alvarez »

Brian Alvarez

  • Guest
Re: Copy sometimes does not copy...
« Reply #6 on: January 27, 2019, 06:33:28 PM »
Charles, you were right, that was exactly it. It works fine now. :)

 I was too sleepy to notice i was storing the pointer to a bstr instead of the bytes in it.