Redim NEW constructed pointers calls the DELETE?

General FreeBASIC programming questions.
Post Reply
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Redim NEW constructed pointers calls the DELETE?

Post by Tourist Trap »

Hello,

if I have an array of udt ptr constructed with NEW. Will the ERASE or REDIM call the DELETE for us?

Code: Select all

redim as udt ptr array(1)
array(1) = new udt
erase array  ' content deleted or memory leak? -> leak confirmed

Thanks
Last edited by Tourist Trap on Jan 20, 2019 10:11, edited 2 times in total.
fxm
Moderator
Posts: 12082
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Redim NEW constructed pointers calls the DELETE?

Post by fxm »

No

The general rule is:
If you call New, you must call Delete as many times.
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: Redim NEW constructed pointers calls the DELETE?

Post by Tourist Trap »

fxm wrote:No

The general rule is:
If you call New, you must call Delete as many times.
Ok.

So I should rather use this:

Code: Select all

 redim as udt ptr array(1)
array(1) =@(udt())
erase array  ' content deleted or memory leak?
Unless NEW leads to something faster for an reason I ignore? Maybe NEW doesn't require an explicit constructor, constructors tends to slow the things down I believe.
badidea
Posts: 2586
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: Redim NEW constructed pointers calls the DELETE?

Post by badidea »

I think that both are not good:

Code: Select all

type udt
	dim as integer a
	declare constructor 
	declare destructor 
end type

constructor udt
	print "constructing"
end constructor

destructor udt
	print "destructing"
end destructor

? 1
redim as udt ptr array(1)
? 2
array(1) =@(udt())
? 3
erase array  ' content deleted or memory leak?

? 4
redim as udt ptr array(1)
? 5
array(1) = new udt
? 6
erase array  ' content deleted or memory leak?
? 7
The 'array(1) = new udt' case: no destructor called
The 'array(1) =@(udt())' case: destructor called to early (?)
fxm
Moderator
Posts: 12082
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Redim NEW constructed pointers calls the DELETE?

Post by fxm »

Yes, both are bad.
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: Redim NEW constructed pointers calls the DELETE?

Post by Tourist Trap »

badidea wrote:I think that both are not good:
fxm wrote:Yes, both are bad.
Yes?
So you would construct the objects by value outside the array and then add their address to the array?

Code: Select all

dim as UDT   u1
array(1) = @ u1
erase array  '.. u1 is still leaving its life
This is a little like using New without memory leaks due to the forgotten delete.
badidea
Posts: 2586
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: Redim NEW constructed pointers calls the DELETE?

Post by badidea »

Something like this?

Code: Select all

type udt
	dim as integer a
	declare constructor 
	declare destructor 
end type

constructor udt
	print "constructor udt"
end constructor

destructor udt
	print "destructor udt"
end destructor

'----------------------------------------------------

type array_udt
	dim as udt ptr pArray(any)
	declare constructor
	declare destructor
	declare sub createSomeItems(nItems as integer)
	declare sub eraseAndDelete()
end type

constructor array_udt
	print "constructor array_udt"
end constructor

destructor array_udt
	print "destructor array_udt"
	eraseAndDelete()
end destructor

sub array_udt.createSomeItems(nItems as integer)
	redim as udt ptr pArray(nItems-1)
	for i as integer = 0 to ubound(pArray)
		pArray(i) = new udt
	next
end sub

sub array_udt.eraseAndDelete()
	for i as integer = 0 to ubound(pArray)
		pArray(i)->destructor
		pArray(i) = 0
	next
	erase pArray
end sub

'----------------------------------------------------

dim as array_udt stuff

stuff.createSomeItems(3)
print "do things"
This kind of code always gets longer then planned.
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: Redim NEW constructed pointers calls the DELETE?

Post by Tourist Trap »

badidea wrote: This kind of code always gets longer then planned.
Thanks, that's indeed very longer than expected.
You used NEW however. So maybe here:

Code: Select all

 sub array_udt.eraseAndDelete()
	for i as integer = 0 to ubound(pArray)
		pArray(i)->destructor
		pArray(i) = 0
	next
	erase pArray
end sub
Pointer = 0, should be if we follow the advice of fxm:
delete Pointer.
fxm
Moderator
Posts: 12082
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Redim NEW constructed pointers calls the DELETE?

Post by fxm »

Code: Select all

sub array_udt.eraseAndDelete()
   for i as integer = 0 to ubound(pArray)
      'pArray(i)->destructor  '' udt instance is destructed but corresponding allocated memory is not freed
      delete pArray(i)        '' udt instance is destructed then corresponding allocated memory is freed
      'pArray(i) = 0          '' useless because the array is then erased
   next
   erase pArray
end sub
badidea
Posts: 2586
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: Redim NEW constructed pointers calls the DELETE?

Post by badidea »

Sorry, I indeed forgot delete
Post Reply