Multi CreateBrush() with NIL no release brush

Report any Hollywood bugs here
Post Reply
sinisrus
Posts: 342
Joined: Tue Apr 21, 2015 5:43 pm

Multi CreateBrush() with NIL no release brush

Post by sinisrus »

Hello,

OpenResourceMonitor()

If i use CreateBrush(ID) With FOR => Good Work 1 brush maked

Code: Select all

FOR local i=1 TO 50 DO CreateBrush(1,200,200,#BLUE)
If i use CreateBrush(NIL) With FOR => No good Work 50 brush maked

Code: Select all

FOR local i=1 TO 50 DO Test$=CreateBrush(NIL,200,200,#BLUE)
Is this normal?
If that's yes why do that?

User avatar
jPV
Posts: 466
Joined: Sat Mar 26, 2016 10:44 am
Location: RNO
Contact:

Re: Multi CreateBrush() with NIL no release brush

Post by jPV »

sinisrus wrote:
Thu Oct 14, 2021 4:45 pm
If i use CreateBrush(ID) With FOR => Good Work 1 brush maked

Code: Select all

FOR local i=1 TO 50 DO CreateBrush(1,200,200,#BLUE)
This will always create a brush with ID 1, so it replaces (and frees) the previous brush which had the same ID (1). Only one brush stays in memory.

If i use CreateBrush(NIL) With FOR => No good Work 50 brush maked

Code: Select all

FOR local i=1 TO 50 DO Test$=CreateBrush(NIL,200,200,#BLUE)
This will create new brushes with a different ID always ("Nil" produces an ID that isn't used before and guarantees that you don't accidently replace any prevous brush). You store the ID of each brush into the same variable (Test$) and after the loop the variable only contains the last ID. All 50 brushes are in memory, but you only know how to refer to the last brush, because you didn't store IDs of the first 49 brushes.

So yes, it's normal.

If you want to create 50 separate brushes and have their IDs stored, you could do like this:

Code: Select all

brush_ids = {}
FOR local i=1 TO 50 DO brush_ids[i]=CreateBrush(NIL,200,200,#BLUE)
Now you can refer to the first brush by brush_ids[1] and the last by brush_ids[50], and so on... to display brush 24 would be DisplayBrush(brush_ids[24], 0, 0)

sinisrus
Posts: 342
Joined: Tue Apr 21, 2015 5:43 pm

Re: Multi CreateBrush() with NIL no release brush

Post by sinisrus »

@JPV

Thank you

In my Pixy project, I often need to change a brush that always the same ID so I have to do FreeBrush() each time it's not practical

User avatar
jPV
Posts: 466
Joined: Sat Mar 26, 2016 10:44 am
Location: RNO
Contact:

Re: Multi CreateBrush() with NIL no release brush

Post by jPV »

If you have the same ID, then you don't need to do FreeBrush(). It's just that you don't get the same ID ever if you're using NIL to create a brush.

Just use static IDs if you need to use the same ID, reserve certain IDs for certain uses and you're fine. If you do "CreateBrush(15,200,200,#BLUE)", it frees automatically the previous brush 15, and so on. I sometimes write a list in a comment section which IDs are reserved for which use and code according that if I don't want to use dynamic IDs for some reason.

sinisrus
Posts: 342
Joined: Tue Apr 21, 2015 5:43 pm

Re: Multi CreateBrush() with NIL no release brush

Post by sinisrus »

@JVP

Pixy uses a lot of brushes and the project also uses a lot of brushes so using static IDs would be unmanageable

SamuraiCrow
Posts: 432
Joined: Fri May 15, 2015 5:15 pm
Location: Waterville, Minnesota USA

Re: Multi CreateBrush() with NIL no release brush

Post by SamuraiCrow »

I think he means that you use the same brush ID as the one you want to flush. This causes the garbage collector to free the brush for you.
I'm on registered MorphOS using FlowStudio.

User avatar
jPV
Posts: 466
Joined: Sat Mar 26, 2016 10:44 am
Location: RNO
Contact:

Re: Multi CreateBrush() with NIL no release brush

Post by jPV »

sinisrus wrote:
Tue Oct 19, 2021 11:39 am
Pixy uses a lot of brushes and the project also uses a lot of brushes so using static IDs would be unmanageable
Yeah, then you have to catch all IDs into variables and free them manually when you replace them to avoid them being "leaked" into memory. In your original example you only "catched" one of fifty IDs... a FreeBrush(test$) within the same loop would do it right then.

Post Reply