I just realized something: The above method is a really dangerous one!
It can potentially overwrite other objects handled by the GC.
Here a fixed version.
Btw: You need to init the stringptr as well with that size when you create it. I created the init method for that.
*sorry that I had to remove the code. had a little nifty memoryleak ;-)*
It can potentially overwrite other objects handled by the GC.
Here a fixed version.
Btw: You need to init the stringptr as well with that size when you create it. I created the init method for that.
Strict Type CVar Field name:String Field namePtr:Byte Ptr Method init(length:Int) ' Length is the max length of the strings you want to use ' +1 is needed for the C Stringterminator char (chr(0)) namePtr = MemAlloc(length+1) End Method End Type Function SetCString(target:Byte Ptr, in:String) ' Lets say there is a constant max_len that defines the max string len Local ts:Byte Ptr If in.length > max_len in = Left(in , max_len) ts = in.toCString() Else Local tm:Byte Ptr = MemAlloc(max_len + 1) ts = in.toCString() MemCopy(tm , ts , in.length + 1) MemFree ts ts = tm EndIf MemCopy(target,ts,max_len+1) MemFree ts End Function Const max_len:Int = 20 Local test:cvar = New cvar test.name = "Test" test.init(max_len) Print "Memory: " + GCMemAlloced() For Local i = 0 To 100000000 setCString(test.namePtr , "Test: " + i) Next Print "Memory: " + GCMemAlloced() End
*sorry that I had to remove the code. had a little nifty memoryleak ;-)*