Same Method being called twice

BlitzMax Forums/BlitzMax Beginners Area/Same Method being called twice

Gabriel(Posted 2005) [#1]
I have a type which extends another type, both of which have a delete method. It's not strictly necessary, but I'm using it like that to output debugging information until the program is finished.

I've noticed that I'm getting two notices that the object has been deleted in the debug file. So either one of the methods is being called twice or both methods are being called. Should not the inherited method be overloaded by the child's method? Is this right that both methods will be called?


skidracer(Posted 2005) [#2]
Both methods are called, new will call the baseclass's new first, delete will call the baseclass's delete last.


Gabriel(Posted 2005) [#3]
Ok, thanks. Just wanted to make sure I didn't have a loose bug in my code.


Grovesy(Posted 2005) [#4]
Interesting, isn't it true that in most OOP languages if you re-define an inherited function, the old function is not used (effectivly you overwrite the old onw)!! As having it called twice could be dangerious!


Sweenie(Posted 2005) [#5]
in c++ the constructors and deconstructors is called just like skidracer described above.

Simply let the baseclass constructor & deconstructor handle what "belongs" to the baseclass and likewise for the derived class.


PowerPC603(Posted 2005) [#6]
I guess this only happens with the New and Delete methods.
All other user-defined methods don't get called twice.

Type TestA
	Method New()
		Print "New TestA-type"
	End Method

	Method Delete()
		Print "TestA-Type deleted"
	End Method

	Method Something()
		Print "Printing something from TestA"
	End Method
End Type

Type TestB Extends TestA
	Method New()
		Print "New TestB-type"
	End Method

	Method Delete()
		Print "TestB-Type deleted"
	End Method

	Method Something()
		Print "Printing something from TestB"
	End Method
End Type

Local a:TestA = New TestA
a.SomeThing()
Print "-----"


Local b:TestB = New TestB
b.SomeThing()
Print "-----"

a = Null
FlushMem
Print "-----"

b = Null
FlushMem
Print "-----"


This code outputs:
New TestA-type
Printing something from TestA
-----
New TestA-type
New TestB-type
Printing something from TestB
-----
TestA-Type deleted
-----
TestB-Type deleted
TestA-Type deleted
-----


You see that when creating (and deleting) a TestB type-instance, both the TestA- and TestB- "New" (and "Delete") methods are called.

First, the TestA object is created (New-method of this object is executed upon creation) and then that object is "extended" by the TestB object, resulting in the second New-method being executed.
How could you extend the TestA object by a TestB object, if a TestA object didn't exist in the first place?

When you call the method "Something" from the TestB type, only the TestB-method is executed, because you've overridden the Something-method of the TestA object (the TestA-Something method can still be executed by using Super.Something).

After that, you delete the TestB object and therefore, the TestA object isn't required anymore, so is deleted too, resulting in the execution of both "Delete" methods in reverse order.


Dreamora(Posted 2005) [#7]
sure it only happens with new and delete as they are the constructor and destructor functionality of BM :-)


Jay Kyburz(Posted 2005) [#8]
i still this should be documented. I posted about this weeks ago and it seems others are finding out the hard way to.


Dreamora(Posted 2005) [#9]
I think it even is somewhere in the language part.

And it has been mentioned here on boards since first win betas are around ...


Bot Builder(Posted 2005) [#10]
Yeah, this needs doccing - I can imagine in many situations that you could copy the constructor code for derived classes and then added something. no error would be caused but you would create objects in your type twice and give th GC extra work.


Dreamora(Posted 2005) [#11]
This shouldn't happen.
Why? If you don't know that new and delete exist as base constructor you can't mess around with them. and if you already have this knowledge from somewhere you should know how they work or not touch them ...


Sweenie(Posted 2005) [#12]
But it will happen.
Why?
Because New and Delete are mentioned in the docs and some users will probably find them interesting and useful.
The manual does explain how New and Delete works but it doesn't explain that overriding them doesn't work the same way as overriding any other method.
Adding a small footnote about this would be much better than expecting the user to already know this, wouldn't it?


Dreamora(Posted 2005) [#13]
Thats true. a little footnote wouldn't do any harm :)


Bot Builder(Posted 2005) [#14]
Yep, I got the new/delete info from the docs and thought i knew how they worked - just like any other method, but it turns out that they do not work in this way.


Jay Kyburz(Posted 2005) [#15]
yeah.. come on Blitz.. in the Userdefined Types chapter in the New and Delete section!