Byref in return function

Post your FreeBASIC tips and tricks here. Please don’t post your code without including an explanation.
aloberoger
Posts: 495
Joined: Jan 13, 2009 19:23

Re: Byref in return function

Postby aloberoger » Aug 13, 2020 18:43

José said ...
if you have an example on hand, this will be welcome please
aloberoger
Posts: 495
Joined: Jan 13, 2009 19:23

Re: Byref in return function

Postby aloberoger » Aug 13, 2020 18:48

with these two functions things are normal. curious that you still have bugs

Code: Select all


Constructor CPicture(ByVal opic As IPicture Ptr)
     If m_pPicture Then
               m_pPicture->lpvtbl->Release(m_pPicture)
    EndIf
        m_pPicture=opic
        If m_pPicture Then
               m_pPicture->lpvtbl->AddRef(m_pPicture)
       EndIf   
End Constructor

Operator CPicture.let(ByVal opic As IPicture Ptr)
        If m_pPicture Then
            m_pPicture->lpvtbl->Release(m_pPicture)
     EndIf
        m_pPicture=opic
        If m_pPicture Then
               m_pPicture->lpvtbl->AddRef(m_pPicture)
       EndIf      
  End Operator

fxm
Posts: 9983
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Byref in return function

Postby fxm » Aug 13, 2020 19:23

Just because the operating system doesn't detect a segmentation violation (and the program doesn't crash) doesn't mean there isn't a bug.
It all depends on the execution context.

The context is changing when using static allocation + copy (with Copy-Constructor or Let-Operator) versus dynamic allocation with New + Copy-Constructor.
fxm
Posts: 9983
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Byref in return function

Postby fxm » Aug 13, 2020 19:49

An example of a Copy-Constructor and Let-Operator a little like yours, but with the bug fixed:

Code: Select all

Type UDT
    Dim As String Ptr ps
    Declare Sub InitString (Byref s As String)
    Declare Constructor ()
    Declare Constructor (Byref u As UDT)
    Declare Operator Let (Byref u As UDT)
    Declare Destructor ()
    Declare Sub DelStr ()
End Type

Sub UDT.InitString (Byref s As String)
    This.DelStr()
    This.ps = New String(s)
End Sub

Constructor UDT ()
    This.ps = 0                     ' useless
End Constructor

Constructor UDT (Byref u As UDT)
    This.DelStr()                   ' useless
    ' This.ps = u.ps                ' pointer copy => bug, rather:
    This.ps = New String(*u.ps)
End Constructor

Operator UDT.Let (Byref u As UDT)
    If @u <> @This Then             ' against self-assignment
        This.DelStr()
        ' This.ps = u.ps            ' pointer copy => bug, rather:
        This.ps = New String(*u.ps)
    End If
End Operator

Destructor UDT ()
    This.DelStr()
End Destructor

Sub UDT.DelStr ()
    If This.ps <> 0 Then
        Delete This.ps
        This.ps = 0
    End If
End Sub

Scope
    Dim As UDT u1
    u1.InitString("FreeBASIC")
    Dim Byref As UDT u2 = *New UDT(u1)
    Print *u2.ps
    Delete @u2
End Scope

Sleep

Return to “Tips and Tricks”

Who is online

Users browsing this forum: No registered users and 5 guests