Possible bug

General discussion for topics related to the FreeBASIC project or its community.
Post Reply
angros47
Posts: 2324
Joined: Jun 21, 2005 19:04

Possible bug

Post by angros47 »

In the code:

Code: Select all

extern "c++"
type foo extends object
	a as integer
	declare virtual destructor

end type
end extern

destructor foo
end destructor
the linker returns "undefined reference to `foo::~foo()'"

By removing the extern "c++", all works correctly
If the lines "Destructor...end destructor" are removed, if "extern c++" is used, the linker would report two missing references to the destructor.

I suspect the cause of this behavior is that in a C++ class two destructors are supposed to exist (I found a bug about that some years ago), and the in the example above only one is created, instead of two

Strangely, if the keyword "virtual" is removed all works as it should. So I assume it's a bug of the compiler.
coderJeff
Site Admin
Posts: 4326
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Re: Possible bug, extern c++ and virtual destructor

Post by coderJeff »

Looks like interoperability with extern c++ is maybe only a symptom (appearing in fbc 1.09.0) of a deeper issue (existing in earlier fbc versions):

Code: Select all

type D extends object
	declare virtual destructor
end type

destructor D
	print "~D()"
end destructor

var x = new D
dim as object ptr y = x

'' delete derived object through base pointer
delete y
I think we should expect that the destructor on the derived class is called ... but it's not.
And I as far as I can tell, there is no destructor of any kind implemented for the fb's built-in object class.
coderJeff
Site Admin
Posts: 4326
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Re: Possible bug, extern c++ and virtual destructor

Post by coderJeff »

A possible work-around for fb only code (non extern "c++") is to derive your own object and use that as the base pointer:

Code: Select all

type VirtualObject extends object
	declare virtual destructor
end type

destructor VirtualObject
	print "~VirtualObject()"
end destructor

type D extends VirtualObject
	declare virtual destructor
end type

destructor D
	print "~D()"
end destructor

var x = new D
dim as VirtualObject ptr y = x

'' delete derived object through base pointer
delete y

/' OUTPUT:
~D()
~VirtualObject()
'/
Bug report submitted: Unable to delete derived class through object base pointer
fxm
Moderator
Posts: 12110
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Possible bug

Post by fxm »

The 'destructor()' of 'OBJECT' is not virtual (or equivalent):

Code: Select all

Dim As Object Ptr po = New Object

Print "object size:", sizeof(*po)
Print "vptr:", cptr(Any Ptr Ptr Ptr, po)[0]
Print "vptr[0]:", cptr(Any Ptr Ptr Ptr, po)[0][0]

Delete po
coderJeff
Site Admin
Posts: 4326
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Re: Possible bug

Post by coderJeff »

fxm wrote: Mar 26, 2022 13:24 The 'destructor()' of 'OBJECT' is not virtual (or equivalent):
Should it be virtual? Or is there some reason it is not? Because I don't know. virtual methods can't be used in any class/type unless it is derived/extended from 'object'. On the surface seems like there is something missing here.
fxm
Moderator
Posts: 12110
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Possible bug

Post by fxm »

It seems that 'OBJECT' downright doesn't have a 'destructor()' (only 'constructors').
Post Reply