Oxygen Basic
Programming => Bugs & Feature Requests => Topic started by: Brian Alvarez on January 27, 2019, 05:33:16 AM
-
This code works once:
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:
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.
-
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
sys addr = (hBuffer + offset)
string bb = space(ln) 'also has invisible null terminators
copy strptr(bb), addr, ln
return bb
-
Unfortunately, that didn't work. :(
sys addr = (hBuffer + offset)
string bb = space(ln)
copy strptr(bb), addr, ln
return bb
This works:
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. :)
-
copy is one of the oldest and most frequently used o2 functions.
full example:
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.
-
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.
-
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.
-
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.