Min/Max Functions Cause Double() Memory Leak When Used In Loops

This behavior is actually a LOT older than that. It’s always been recommended that you compute loop bounds before your For…Next statement for this reason (at least as far back as I can remember). Doing things like:

For i as Integer = 0 to arrayOfItems.Ubound

Independently loops through arrayOfItems on each iteration of the loop to get the last index for that trip through the loop, as Norman said above. The fact that it creates these objects and leaves them in memory is nasty, but a side effect of the same. I’m sure there are other areas where this leaks memory if you use certain methods to calculate your bounds within the For… line.

See Performance Considerations

Feel like this is often with Xojo Bugs “if there’s an easy woraround, we do not need to fix it. Users should simply learn how to use our Tool…”…

Personally I found for performance that NOT using min or max was faster, instead using a simple equation like below.

X = if( x < y, x, y )

In other languages this can be written

x = ( x < y ) ? x : y ;

In regards to using it in a loop; old habits die hard.

Dim n as integer = <countEquation> Dim l as integer For l = 0 to n ~ do stuff Next

[quote=449252:@Sam Rowlands]
In regards to using it in a loop; old habits die hard.

Dim n as integer = <countEquation> Dim l as integer For l = 0 to n ~ do stuff Next[/quote]
pluses - the end of loop condition is evaluated ONCE
if thats a function that does a lot of work then that could be a big plus

[quote=449241:@Anthony Cyphers]This behavior is actually a LOT older than that. It’s always been recommended that you compute loop bounds before your For…Next statement for this reason (at least as far back as I can remember). Doing things like:

For i as Integer = 0 to arrayOfItems.Ubound

Independently loops through arrayOfItems on each iteration of the loop to get the last index for that trip through the loop, as Norman said above. The fact that it creates these objects and leaves them in memory is nasty, but a side effect of the same. I’m sure there are other areas where this leaks memory if you use certain methods to calculate your bounds within the For… line.

See Performance Considerations[/quote]

I’m not on about the speed issue, I’m on about the leaking issue, the way someone codes shouldn’t determine if the program will eventually crash due to no fault of their own.

[quote=449252:@Sam Rowlands]Personally I found for performance that NOT using min or max was faster, instead using a simple equation like below.

X = if( x < y, x, y )[/quote]

Indeed, dipping into a function a few thousand times in a loop would always be worse than what you posted if the compiler didn’t flatten it out. Macro’s would be nice, but we can dream.

Totally agree, just pointing out why it was likely never fixed as the leak only seems to happen (according to my tests) when using Min/Max in this specific way.