About Monkey 2 › Forums › Monkey 2 Programming Help › DataBuffer resizing
This topic contains 5 replies, has 3 voices, and was last updated by
Hezkore
1 year, 7 months ago.
-
AuthorPosts
-
September 19, 2017 at 3:12 am #10617
I’m trying to resize a DataBuffer, but it doesn’t seem to update properly?
In this example I’m resizing it to 8, but Length still returns 512Monkey1234Local buff:DataBuffer=New DataBuffer( 512 )buff.Resize( 8 )Print buff.LengthSeptember 19, 2017 at 10:58 am #10621Here is the code in the source.
Monkey123456Method Resize( length:Int )Local data:=Cast<UByte Ptr>( GCMalloc( length ) )libc.memcpy( data,_data,Min( length,_length ) )GCFree( _data )_data=dataEndAs you can see, the buffer is being resized properly. The problem is that the _length field is not being updated, so it is reporting the wrong size (and could cause an error if you read or write outside the true length).
Should be a _length = length in there somewhere.Edit: just submitted an issue on Github.
September 20, 2017 at 12:22 am #10633Thanks for the issue submit to Github.
I have loads of issues with DataBuffer.
For example, when using ‘server.ReceiveFrom’ with network sockets (seen in ‘banans/echoserver_udp’)Monkey12345If server.CanReceive>0 Thenbuffer=New DataBuffer( server.CanReceive+1 )server.ReceiveFrom( Varptr buffer, server.CanReceive, addr )Print buffer.PeekUByte( 0 )EndifI get this error:
MS DOS12345678910Memory access violation{{!DEBUG!}}>PeekUByte:Ubyte(offset:Int);G:/tools/Monkey2/modules/std/memory/databuffer.monkey2;118;9267912Self:std.memory.DataBuffer=@0159B65Coffset:Int=0>Main:Void();G:/code/monkey2/crawl/src/server/server.monkey2;28;135server:std.socket.Socket=@0008F58Cclients:std.collections.Map<std.socket.SocketAddress,Uint>=@0008F554addr:std.socket.SocketAddress=@0008F7E4buffer:std.memory.DataBuffer=@0159B65CThough I guess that could be a network socket issue and not a DataBuffer issue.
But it seems like whenever I use the DataBuffer, I run into some weird problem heh.September 20, 2017 at 2:51 am #10636Maybe possible that more data is received between lines 2 and 3 causing server.ReceiveFrom to read in more data than the buffer was originally allocated?
Maybe try something like thisMonkey123456Local CanReceive:=server.CanReceiveIf CanReceive>0 Thenbuffer=New DataBuffer( CanReceive+1 )server.ReceiveFrom( Varptr buffer, CanReceive, addr )Print buffer.PeekUByte( 0 )EndifSeptember 20, 2017 at 4:18 am #10640Monkey123buffer=New DataBuffer( server.CanReceive+1 )server.ReceiveFrom( Varptr buffer, server.CanReceive, addr )Danger! Use ‘buffer.Data’ to get a pointer to the data within a buffer, NOT ‘Varptr buffer’. Varptr buffer returns the address of the buffer object. The above code will completely destroy the contents of the ‘buffer’ reference.‘Varptr object’ should really be illegal, and is likely to become so one day.September 21, 2017 at 5:03 am #10661Okay good to know Mark!
Resize issue is still a problem though :/ -
AuthorPosts
You must be logged in to reply to this topic.