Page 1 of 1

freeimage() needed before copyimage()

Posted: Wed May 29, 2024 6:02 am
by jassing
Let's say #image1 is an image, but I issues CopyImage(#image2, #image1) --- is the memory automatically freed, or should I be calling freeimage 1st?

Re: freeimage() needed before copyimage()

Posted: Wed May 29, 2024 7:31 am
by Bisonte
Clean programming means: If you take something, you give it back when it is no longer needed.

A programming language cannot see into the programmer's head to guess what he is going to do next.

But in this case : If #Image1 is a fixed number (and not created with #PB_Any) then it is released.

At least that's what everyone has said so far...

(translated by DeepL.com)

Re: freeimage() needed before copyimage()

Posted: Wed May 29, 2024 7:45 am
by boddhi
Hello,

Code: Select all

Enumeration
  #image1
  #Image2
EndEnumeration

; Example 1 : When #Image2 doesn't exist
CreateImage(#image1,50,50)
CopyImage(#image1,#Image2)
Debug IsImage(#Image2)

; Example 2 : When #Image2 already exists
ResizeImage(#Image2,1,1)
CopyImage(#image1,#Image2)
Debug IsImage(#Image2)

Re: freeimage() needed before copyimage()

Posted: Wed May 29, 2024 8:25 am
by mk-soft
PB-Help: https://www.purebasic.com/documentation ... jects.html
- An object that is associated with an index is automatically freed when reusing that index.

Re: freeimage() needed before copyimage()

Posted: Wed May 29, 2024 12:23 pm
by jassing
Bisonte wrote: Wed May 29, 2024 7:31 am Clean programming means: If you take something, you give it back when it is no longer needed.
Yes, I understand that. Yet, purebasic does some things "automatically" ... I realized I hadn't free'd the images, and wondered if I needed to go back into the code and change it. I don't regularly work with images... I was hoping copyimage would free the destination if it exists.
Bisonte wrote: Wed May 29, 2024 7:31 am A programming language cannot see into the programmer's head to guess what he is going to do next.
However, if you're copying #iamge1 to #image2 - the language could know #image2 is no longer needed.

Re: freeimage() needed before copyimage()

Posted: Wed May 29, 2024 12:24 pm
by jassing
mk-soft wrote: Wed May 29, 2024 8:25 am PB-Help: https://www.purebasic.com/documentation ... jects.html
- An object that is associated with an index is automatically freed when reusing that index.
Thank you.