problem with release command

Archives Forums/BlitzMax Bug Reports/problem with release command

yossi(Posted 2014) [#1]
when I run the release example:

Rem
Release removes the internal reference caused by creating an integer handle to a type.
End Rem

Type MyType
Field bigmap[1024*1024]
End Type

GCCollect
Print GCMemAlloced()

a=New MyType
GCCollect
Print GCMemAlloced()

Release a
GCCollect
Print GCMemAlloced()

I get the next output:

Building .release
Compiling:.release.bmx
flat assembler version 1.69.14 (1048575 kilobytes memory)
3 passes, 4366 bytes.
Linking:.release.debug.exe
Executing:.release.debug.exe
15204
4209544
4209544

Process complete

from this output it seems that 'release a' doesn't remove the reference of a to the allocated memory for MyType and therefore
the last GCCollect doesn't release the memory that was allocated for MyType memory and which was supposed to be free by release a command.

my OS is windows 8.1.


GfK(Posted 2014) [#2]
There's nothing wrong with it, it's just a really badly-written example.

The problem is that GCCollect doesn't free up the allocated memory in time for the next Print command. Try the code below - you'll see that after a couple of seconds, memory usage returns to normal.
Rem
Release removes the internal reference caused by creating an integer handle to a type.
End Rem

Type MyType
  Field bigmap[1024*1024]
End Type

Graphics 640,480 'need this so we can hit Esc to quit the while/wend loop - makes no difference to the example

GCCollect
Print GCMemAlloced()

a=New MyType
GCCollect
Print GCMemAlloced()

Release a
GCCollect
While Not KeyHit(key_escape)
  Print GCMemAlloced()
Delay 1
Wend



yossi(Posted 2014) [#3]
I am very confused because in the next example the garbage collector free the memory immediately and doesn't need some seconds or even less to free the memory (it works like it should):

Type MyType
Field bigmap[1024*1024]
End Type

GCCollect
Print GCMemAlloced()

a:mytype=New MyType
GCCollect
Print GCMemAlloced()

a=Null
GCCollect
Print GCMemAlloced()

and now the output is correct:

Building .release
Compiling:.release.bmx
flat assembler version 1.69.14 (1048575 kilobytes memory)
3 passes, 4309 bytes.
Linking:.release.debug.exe
Executing:.release.debug.exe
15204
4209544
15204

Process complete

it seems that when we put the address of the allocated memory into a variable of the same type (not integer) and free the reference to it by putting a null into it later it works fine but work less well when we use integer to hold the address and later using release on the integer variable.

sorry but I steal think there is a problem here because I would expect the 2 methods to work the same.


GfK(Posted 2014) [#4]
They do both work the same on my PC - I tested both. Memory isn't released for at least a couple of seconds.


yossi(Posted 2014) [#5]
in my computer on the first method it took the gc some seconds to release the memory but on the second method the gc release the memory
immediately .

why the difference ?


GfK(Posted 2014) [#6]
Your second bit of code is not the same.

In your first code:
a=New MyType

In your second code:
a:MyType=New MyType


The first does not work immediately with GC, the second one does.


yossi(Posted 2014) [#7]
so can I assume that the second method of a:MyType=New MyType and a=null is better then the method of a=New Type and release a ?


Floyd(Posted 2014) [#8]
Using integer variables for objects is allowed in order to ease the transition for programmers who already know an earlier Blitz language. But it is a bad practice and you should not do it.

You should always make variables the appropriate type and forget that you ever saw the Release command.


yossi(Posted 2014) [#9]
thank you Floyd.
now it is clear.


GfK(Posted 2014) [#10]
Just to add, it's good practice to use "Strict" (or "SuperStrict") at the top of your code. These two commands force you, to varying levels, to declare variables before you use them. Pretty sure none of this code would even compile in Strict mode.


Brucey(Posted 2014) [#11]
Use SuperStrict at the top of your code.
It *forces* you to define the types of everything - which is not a bad thing.


marksibly(Posted 2014) [#12]
Please don't use 'Release'!

As Floyd says, it was to make life easier for people coming from Blitz3D etc, but it is slow and makes for ugly code!