Need Help, Memory, GAH!

BlitzPlus Forums/BlitzPlus Programming/Need Help, Memory, GAH!

Gladclef - Ben B(Posted 2009) [#1]
I'm trying to write a program that lets you animate 2D animations frame-by-frame. The only problem (currently), is that half-way through execution, the program breaks down and starts using up memory like crazy. If you guys wouldn't mind helping me out... What the heck is eating up all of that memory?



What you're seeing: The top portion is the current frame in the editor (all images are randomly generated at the moment). The bottom portion is the preview bar with the current frame and some previous and post frames. You can click in the editor to zoom in/out and on the preview to switch frames.
Controls: mouse and left/right mouse buttons

Edit: Bug fixed, see line 515.


Mahan(Posted 2009) [#2]
This solves the immediate problem:




The big issue was that all your usage of scaleImageFast() was returning a newly created image each time drawimage() and drawblock() was called which ended up in hysterical memory usage increase when you used scaled (zoomed) images.

If you wanna know exactly what changes i made save my file under another name next to your original file and use WinMerge to see the differences.

After my changes there is still a (very small) increase of memory usage when the program was running so you still (probably) got some leak somewhere, but now it's at least usable :^)

edit: program updated. Now much better. The problems where more of the same as before.


Mahan(Posted 2009) [#3]
Sorry, issue is still not resolved :-/

wait a few more minutes :)

EDIT: Check my previous post. it's fixed now.


Gladclef - Ben B(Posted 2009) [#4]
Awesome. Would have taken me days, literally, to think of that.
Edit: took the bellow suggestion to heart. Have posted the edited program plus the fix in the original code.


Mahan(Posted 2009) [#5]
I'm glad if it helped.

I know this function scaleImageFast() was written by someone else but anyway here's my advise for the future: Any time you write (or use) a function or piece of code that creates or claims some resource that it doesn't release again write a **** HUGE WARNING <description> **** over it.

I.e. if a function allocates memory, creates/loads images/fonts/sounds etc. without removing them or opens files/network or serial ports without closing them etc. they need to be marked with a **** HUGE WARNING ****

This is especially important in Blitz+ as this language does not support Callbacks natively, which makes is generally harder to use code-patterns that minimize problems like this :)


sswift(Posted 2009) [#6]
There's nothing wrong with my code or the way it's commented.

"This function scales an image an arbitrary amount on the X and Y axis, and returns a pointer to the new image.
The original image is not modified."

It doesn't get any clearer than that. Are you saying any function which allocates memory in any way should have a huge warning to people that the function isn't going to magically free the memory on its own when you're done with it? That's silly.


Mahan(Posted 2009) [#7]
Ok, sswift I agree with you, and this was not meant as criticism against you personally. Now that I looked at the original submission (in code archives) I saw the comment made by you.

Clearly it's good to keep functions headers and comments for future use when one copies a function. :-)


Gladclef - Ben B(Posted 2009) [#8]
Sorry. I copied the function a long time ago and for some reason missed copying the comment. Original post has been fixed to include said comment. Thank you again, sswift, for your brilliant code.