walrus
walrus

Reputation: 171

Segmentation Fault when updating cocos.text.Label.element.text (Python/Pyglet/Cocos)

I have a Python/Cocos application that (among other things) receives events through a bus and displays relevant information on screen.

I have a cocos.text.Label which displays the value coming from a remote sensor. When I try and update this label with the new value by setting the label's element.text I get a segmentation fault.

In the actual code other things use this value without any issues, so I would be confident that it's a problem with cocos.text.Label, but the segmentation fault only happens when the update is triggered by the bus rather than a key press event or similar.

Below is a minimal example that exhibits the same behaviour:

#!/usr/bin/python

import cocos
import functools
import pyglet
import remotes # Proprietary library for getting events through a remote event bus

class TestLayer(cocos.layer.Layer, pyglet.event.EventDispatcher):
    is_event_handler = True

def __init__(self):
    super(TestLayer, self).__init__()

    self.bus = remotes.RemoteEventBus()

    widget = remotes.RemoteWidget("force", self.bus, None)
    widget.set_on_value_listener(functools.partial(self.on_value_change))

    self.label_value = 0.0
    self.label = cocos.text.Label(text=str(self.label_value))
    self.add(self.label)

def on_value_change(self, event):
    # Normalised_value will be between 0.0 and ~1.1
    self.update_label(int(event.normalized_value * 100))

def on_key_press(self, key, modifiers):
    self.update_label(key)

def update_label(self, value):
    self.label_value = value
    ### Segfault here (sometimes
    self.label.element.text = str(self.label_value)


if __name__ == "__main__":
    cocos.director.director.init(width=320, height=240, caption="Segfault Test",  fullscreen=False)

    test_layer = TestLayer()
    main_scene = cocos.scene.Scene(test_layer)

    cocos.director.director.run(main_scene)

It does look as if value which is passed to the label affects whether or not it segfaults, but I can't work out why. What might be causing this behaviour?

Apologies for the use of proprietary libraries, I'm working on reproducing this problem without it, but so far without luck.

Upvotes: 1

Views: 176

Answers (0)

Related Questions