Copying objects

BlitzMax Forums/BlitzMax Beginners Area/Copying objects

if the copy method does not copy a field as the space for the field is reserved no mather if it is filled or not.


Sorry Dreamora. I am sure it's right I just don't understand what you're saying.
If the object isn't held in a single contiguous area of memory there'll be a problem which was one of my earlier questions.
I think I see what you mean about referencing other objects. If, for example, my:TTest has a field otherobject:TTest2 then it's possible that object might be deleted as it does not know my:ttest2 is referencing it.
OK. I think that's worth a quick test.
<edit> The result...
Type ttest
  Global mylist:Tlist=CreateList()
  Field x
  Field y
  Field name:String
  Field test2:TTest2
  Function create:ttest()
		my:ttest = New ttest
		my.x = 5
		my.y = 51
		my.test2 = New ttest2
		my.test2.defined = "TTEST2"
		my.name="Original Object"
		ListAddLast(mylist,my)
		Return my
   End Function
   Method remove()
		ListRemove mylist,Self
		test2=Null
   End Method
End Type
Type ttest2 
  Field defined:String
End Type
my:ttest=ttest.create()
mynew:ttest=New ttest
MemCopy(Byte Ptr(mynew),Byte Ptr(my),SizeOf my)
GCCollect()
Print "Memcheck1 : " + GCMemAlloced()
my.remove()
my=Null
'GCCollect()
Print "Memcheck2 : " + GCMemAlloced()
ListAddLast(ttest.mylist,mynew)
Print mynew.x + " " + mynew.y + " " + mynew.name + " " +  mynew.test2.defined
'Print my.x + " " + my.y + " " + my.name + " " +  my.test2.defined
'GCCollect()
Print "Memcheck3 : " + GCMemAlloced()
Print mynew.x + " " + mynew.y + " " + mynew.name + " " + mynew.test2.defined

<edit> Added extra gccollect and it does fail for exactly the reason Dreamora says. No reference for ttest2 so it was deleted although mynew needed it.