While developing a streaming app for Pythonista I noticed that it usually crashes after some time. Turns out it's somehow related to loading/unloading images with scene. Here is a minimal example that crashes Pythonista reliably:
import scene
import random
from cStringIO import StringIO
import Image
def raw_jpeg(img):
buf = StringIO()
img.save(buf, 'JPEG', quality=50)
data = buf.getvalue()[::]
buf.close()
return data
imglist = [raw_jpeg(img) for img in [
Image.new('RGB', (512, 384), 'red'),
Image.new('RGB', (512, 384), 'yellow'),
Image.new('RGB', (512, 384), 'green'),
Image.new('RGB', (512, 384), 'blue'),
Image.new('RGB', (512, 384), 'gray'),
]]
imgnames = []
while True:
name = scene.load_image_data(random.choice(imglist))
if len(imgnames) == 16:
scene.unload_image(imgnames.pop(0))
imgnames.append(name)
print name
It takes a variable amount of time, somewhere between a second and a minute I'd say. And using scene.load_pil_image instead does not help, but only makes the loading process slower.
Am I missing something? scene.unload_image should free memory, and since this example never exceeds 16 images loaded at once it should have plenty of that.
EDIT: I am using iPad 4 and what seems to be the latest version of Pythonista.