Possible Memory Leak in Canvas
Closed this issue · 1 comments
Hello devs!
I'm pretty new at Dome and Wren so excuse me if this is caused by me and not an error in the engine/language itself. Given the below code the Dome window never appears however it does start in my task manager and immediately hogs 10 GBs of RAM and attempts to get even more as my system struggles to feed this monster and eventually crashes.
There are no error messages. Nothing written to the log. Nothing to help me understand or remedy the issue.
I've attached the project so you can run it and see for yourself.
import "graphics" for Canvas, Color, ImageData
import "math" for M, Math, Vector
class Resources {
birdIdle{ImageData.loadFromFile("graphics/bird_idle.png")}
birdFlap{ImageData.loadFromFile("graphics/bird_flap.png")}
obstacleTile{ImageData.loadFromFile("graphics/obstacle_tile.jpg")}
construct new() {}
}
class Bird {
bird_idle=(value){ bird_idle = value }
bird_flap=(value){ bird_flap = value }
center=(value){ center = value }
gravity=(value){ gravity = value }
force=(value){ force = value }
velocity=(value){ velocity = value }
velocityLimit=(value){ velocityLimit = value }
isGameRunning=(value){ isGameRunning = value }
construct new(idle, flap, horizontalPosition, verticalPosition, drag, jumpForce) {
bird_idle = idle
bird_flap = flap
center = Vector.new(horizontalPosition + (idle.width * 0.5), verticalPosition + (idle.height*0.5))
gravity = drag
force = jumpForce
velocityLimit = 1000
isGameRunning = true
}
jump() {
velocity = -force
}
isTouchingCeiling() {
var birdTop = center.y - (bird_idle.height * 0.5)
if(birdTop < 0) {
return true
}
return false
}
isTouchingGround() {
var birdBottom = center.y + (bird_idle.height * 0.5)
if(birdBottom > height) {
return true
}
return false
}
}
class Main {
resources=(value){resources = value}
bird=(value){bird = value}
construct new() {
resources = Resources.new()
bird = Bird.new(resources.birdIdle, resources.birdFlap, resources.obstacleTile.width * 2, resources.obstacleTile.height * 4, 10, 4)
}
init() {}
update() {}
draw(alpha) {
Canvas.cls()
Canvas.draw(bird.bird_idle, bird.center.x, bird.center.y)
}
}
var Game = Main.new()
Hi there, thank you for getting in touch. Fortunately, this doesn't appear to be a bug in DOME, just a misunderstanding in how Wren works.
The culprit is the lines you have which look like this:
bird_idle=(value){ bird_idle = value }
These define methods which look like the "setters" you would see other languages, meaning you can do bird.bird_idle = some-value
. In the example above, you are actually calling the setter again, recursively, leading to the behaviour you are seeing.
[https://wren.io/classes.html#getters and https://wren.io/classes.html#setters]
Classes and objects in Wren have fields, which are private and can only be accessed inside the object, which is why we have the setters and getters.
A common pattern you see for defining a field, with public setters and getters, looks like this:
bird_idle=(value) { _bird_idle = value } // setter, note the _, to indicate an instance field
bird_idle { _bird_idle } // getter, returning the value of _bird_idle
[More info: https://wren.io/classes.html#fields]
I hope this helps you with your project.