Thanks very much for helping me this far with FreeBasic.
I am trying to understand the examples in the "EXTENDS ZSTRING". The following function code causes a double free segmentation
fault.
Code: Select all
function replaceB (byref s as estring) as estring
print "replaceB entered"
dim as string ret = s
dim es as estring
ret = ret & ret
es = ret
return es
end function
'''main
dim ess as estring = "A-"
dim ess2 as estring
ess2 = replaceA(ess)
print "returned from replace"
print "ess2 = "; ess2
Code: Select all
function replaceA (byref s as estring) as estring
print "replaceA entered"
dim as string ret = s
ret = ret & ret
return estring(ret)
end function
Code: Select all
cons with zstring called -- pz = A-
cons with zstring param -- allocated = 20443648
cons with null called
replaceB entered
cons with null called
let with string param: allocated 20443776
destructor: deallocating 20443776
let with estring param: allocated 20443776
destructor: deallocating 20443776
returned from replace
ess2 =
destructor: deallocating 20443776
free(): double free detected in tcache 2
Aborted (core dumped)
Code: Select all
#ifndef null
#define null 0
#endif
type estring extends zstring
public:
declare constructor()
declare constructor(byval pz as const zstring ptr = 0)
declare operator cast() as string
declare operator let(byval pz as const zstring ptr)
declare operator let(byref es as estring)
declare operator let(s as string)
declare function length() as ulongint
declare destructor()
private:
dim as zstring ptr p = null
end type
constructor estring()
print "cons with null called"
this.p = null
end constructor
constructor estring(byval pz as const zstring ptr)
print "cons with zstring called -- pz = "; *pz
if this.p <> null then
print "cons with zstring param: deallocating "; this.p
deallocate(this.p)
this.p = null
end if
this.p = allocate(len(*pz) + 1)
print "cons with zstring param -- allocated = "; this.p
*this.p = *pz
end constructor
operator estring.cast() as string
return *this.p
end operator
operator estring.let(byval pz as const zstring ptr)
if this.p <> null then
print "let with zstring param: deallocating "; this.p
deallocate(this.p)
this.p = null
end if
this.p = allocate(len(*pz) + 1)
print "let with zstring param: allocated "; this.p
*this.p = *pz
end operator
operator estring.let(byref es as estring)
if this.p <> null then
print "let with estring param: deallocating "; this.p
deallocate(this.p)
this.p = null
end if
this.p = allocate(es.length() + 1)
print "let with estring param: allocated "; this.p
*this.p = *es.p
end operator
operator estring.let(s as string)
if this.p <> null then
print "let with string param: deallocating "; this.p
deallocate(this.p)
this.p = null
end if
this.p = allocate(len(s) + 1)
print "let with string param: allocated "; this.p
*this.p = s
end operator
destructor estring()
if this.p <> null then
print "destructor: deallocating "; this.p
deallocate(this.p)
this.p = null
end if
end destructor
function estring.length() as ulongint
return len(*this.p)
end function