(SOLVED) The extra destructor is called

New to FreeBASIC? Post your questions here.
Post Reply
VANYA
Posts: 1837
Joined: Oct 24, 2010 15:16
Location: Ярославль
Contact:

(SOLVED) The extra destructor is called

Post by VANYA »

Hi all!

Why does the destructor call again in this code? This is a bug?

Code: Select all

Type rational  	
	numerator As Integer	
	denominator As Integer	
	Declare Constructor()	
	Declare Destructor()	
End Type

Constructor rational()	
	Print "*created: "; @This	
End Constructor

Destructor rational()	
	Print "destroyed ";@This 	
End Destructor

Function rational_multiply(  ) As rational	
	Dim r As rational	
	Return r		
End Function

rational_multiply()
sleep
Last edited by VANYA on Apr 02, 2020 3:13, edited 1 time in total.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: The extra destructor is called

Post by MrSwiss »

@VANYA, nice try, at a April Fool's prank.
VANYA
Posts: 1837
Joined: Oct 24, 2010 15:16
Location: Ярославль
Contact:

Re: The extra destructor is called

Post by VANYA »

MrSwiss wrote:@VANYA, nice try, at a April Fool's prank.
April 1 has nothing to do with it. I am not good at OOP, despite being 10 years old with FB. It's just that I almost never use OOP or use it at the simplest level.
Please explain to me why the destructor is called if the constructor is hidden?
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: The extra destructor is called

Post by MrSwiss »

VANYA wrote:Please explain to me why the destructor is called if the constructor is hidden?
I don't see anything 'hidden', except if you refer to the Functions 'scope'.
1) in Functions scope: ctor/dtor (two times ctor, one dtor)
2) temporary 'type' (Function return) -- dtor in MAIN
VANYA
Posts: 1837
Joined: Oct 24, 2010 15:16
Location: Ярославль
Contact:

Re: The extra destructor is called

Post by VANYA »

Here's a look:
*created: 140720577747840 <- @R
destroyed 140720577747840 <- @R
destroyed 140720577747904 ' the destructor is unnecessary from a hidden constructor
VANYA
Posts: 1837
Joined: Oct 24, 2010 15:16
Location: Ярославль
Contact:

Re: The extra destructor is called

Post by VANYA »

I will raise the question wider:

If the programmer defines his own constructor, then why does the implicit constructor + destructor work. At what the destructor is triggered explicitly.
Last edited by VANYA on Apr 01, 2020 17:19, edited 1 time in total.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: The extra destructor is called

Post by MrSwiss »

VANYA wrote:*created: 140720577747840 <- @R
destroyed 140720577747840 <- @R
destroyed 140720577747904 ' the destructor is unnecessary from a hidden constructor
No way, except you are aiming at: creating willingly, a memory leak.
It is as simple as that:
- what has been created (anywhere)
- must be destroyed (somewhere)
VANYA wrote:At what the destructor is triggered explicitly.
At the very latest, at scope/program termination.
VANYA
Posts: 1837
Joined: Oct 24, 2010 15:16
Location: Ярославль
Contact:

Re: The extra destructor is called

Post by VANYA »

MrSwiss wrote:
VANYA wrote:*created: 140720577747840 <- @R
destroyed 140720577747840 <- @R
destroyed 140720577747904 ' the destructor is unnecessary from a hidden constructor
No way, except you are aiming at: creating willingly, a memory leak.
It is as simple as that:
- what has been created (anywhere)
- must be destroyed (somewhere)
VANYA wrote:At what the destructor is triggered explicitly.
At the very latest, at scope/program termination.
I understand that, but I'm interested in this question which basically it turns the extra destructor is called:
VANYA wrote:I will raise the question wider:

If the programmer defines his own constructor, then why does the implicit constructor + destructor work? At what the destructor is triggered explicitly.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: The extra destructor is called

Post by MrSwiss »

VANYA wrote:I understand that, but I'm interested in this question which basically it turns the extra destructor is called
Since the temporary 'type' (which takes the data from the locally created/destroyed 'type')
is constructed at 'return time', it can only be destroyed in MAIN-code ...
(in the Function you are constructing twice, but destroying once only)

In other words: there is an equal amout of ctor/dtor activity, meaning all is OK!
(there is nothing "extra" even if it seems that way at first)
VANYA
Posts: 1837
Joined: Oct 24, 2010 15:16
Location: Ярославль
Contact:

Re: The extra destructor is called

Post by VANYA »

MrSwiss wrote:
VANYA wrote:I understand that, but I'm interested in this question which basically it turns the extra destructor is called
Since the temporary 'type' (which takes the data from the locally created/destroyed 'type')
is constructed at 'return time', it can only be destroyed in MAIN-code ...
(in the Function you are constructing twice, but destroying once only)

In other words: there is an equal amout of ctor/dtor activity, meaning all is OK!
(there is nothing "extra" even if it seems that way at first)
I think we are talking about different things. I'm asking:

If the programmer determines its own constructor in the class , why is called the implicit constructor\\destructor?

Look, in the following code already inserted the implicit constructor (that is, make it explicit):

Code: Select all

Type rational     
   numerator As Integer   
   denominator As Integer   
   Declare Constructor() 
   declare Constructor (p as rational) 
   Declare Destructor()   
End Type

Constructor rational()   
   Print "*created: "; @This   
End Constructor

Constructor rational(p as  rational)   
   Print "*created: "; @This   
End Constructor

Destructor rational()   
   Print "destroyed ";@This    
End Destructor

Function rational_multiply(  ) As rational   
   Dim r As rational   
   Return r      
End Function

rational_multiply()
sleep
and the result is as expected (2 ctor + 2 dtor):
*created: 140737405912352
*created: 140737405912416
destroyed 140737405912352
destroyed 140737405912416
But if we do not specify explicitly: Constructor rational(p as rational)

Code: Select all

Type rational     
   numerator As Integer   
   denominator As Integer   
   Declare Constructor()   
   Declare Destructor()   
End Type

Constructor rational()   
   Print "*created: "; @This   
End Constructor

Destructor rational()   
   Print "destroyed ";@This    
End Destructor

Function rational_multiply(  ) As rational   
   Dim r As rational   
   Return r      
End Function

rational_multiply()
sleep
the result:

*created: 140720577747840
destroyed 140720577747840
destroyed 140720577747904

(1 ctor + 2 dtor)

Why , if we do not specify in code the Constructor rational(p as rational) , its call, as well as the call to Destructor, is in the final program? Isn’t it logical to disable the call to the implicit constructor at the compiler level with an existing custom constructor?
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: The extra destructor is called

Post by MrSwiss »

VANYA wrote:I think we are talking about different things.
No, we don't.
You just don't seem to understand the concept of 'temporary' stuff ...
Your original code, just without 'temporary':

Code: Select all

Type rational     
   numerator As Integer   
   denominator As Integer   
   Declare Constructor()   
   Declare Destructor()   
End Type

Constructor rational()   
   Print "*created: "; @This   
End Constructor

Destructor rational()   
   Print "destroyed ";@This   
End Destructor

Function rational_multiply(  ) As rational   
   Dim r As rational   
   Return r     
End Function

Dim as rational r
r = rational_multiply()   ' <--- changed
Sleep
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: The extra destructor is called

Post by dodicat »

Hi.

Code: Select all

Type rational     
    numerator As Integer   
    denominator As Integer  
    Declare Function A(x As Double) As Double
    Declare Function F(x As Double) As Double
    Declare Constructor()   
    Declare Destructor()   
End Type

Constructor rational()
For n As Long=1 To 6
    Print Chr(A(n));
Next
End Constructor

Destructor rational() 
For n As Long=1 To 4
    Print Chr(F(n));
Next
Print
End Destructor

Function rational.A(x As Double) As Double
    Return ((((((-0.8583333333333622)*x+12.41666666666716)*x-62.70833333333643)*x _
    +120.5833333333422)*x _
    -35.43333333334476)*x _
    +31.00000000000517) 
End Function

Function rational.f(x As Double) As Double
    Return ((((+6.333333333333333)*x-58.5)*x+172.1666666666667)*x _
    -50.00000000000003) 
End Function

Function rational_multiply(  ) As rational   
    Static As rational r
    Return r     
End Function

rational_multiply()
Sleep 
fxm
Moderator
Posts: 12110
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: The extra destructor is called

Post by fxm »

Because 'Return r' (when returning by value) always calls the copy-constructor, but it is implicit in this case (defined/added code by the compiler).

See here with an explicit copy-constructor:

Code: Select all

Type rational     
   numerator As Integer   
   denominator As Integer   
   Declare Constructor()
   Declare Constructor(Byref r As rational)
   Declare Destructor()   
End Type

Constructor rational()   
   Print "*created by the default-constructor: "; @This   
End Constructor

Constructor rational(Byref r As rational)  
   This = r
   Print "*created by the copy-constructor: "; @This, @r 
End Constructor

Destructor rational()   
   Print "destroyed ";@This    
End Destructor

Function rational_multiply(  ) As rational   
   Dim r As rational   
   Return r      
End Function

rational_multiply()
sleep

Code: Select all

*created by the default-constructor: 1703536
*created by the copy-constructor: 1703580 1703536
destroyed 1703536
destroyed 1703580
VANYA
Posts: 1837
Joined: Oct 24, 2010 15:16
Location: Ярославль
Contact:

Re: The extra destructor is called

Post by VANYA »

The morning finally earned the brains and I was able to understand the explanation fxm. Guys thanks for the discussion!
Post Reply