Crash when setting textures to object at each step

Report crashes, strange behaviour, or apparent bugs
Post Reply
Firas
Posts: 3
Joined: 05 Jan 2021, 14:21

Crash when setting textures to object at each step

Post by Firas »

Hey there!

I am using RLBench/PyRep to apply various textures to objects in my scene.

However, the memory usage constantly increases until at some point the simulation runs very slowly before it crashes at the end.

As you can see here (https://github.com/stepjam/RLBench/blob ... n_scene.py) in the this function

Code: Select all

def _randomize(self):
tree = self._active_task.get_base().get_objects_in_tree(
    ObjectType.SHAPE)
tree = [Shape(obj.get_handle()) for obj in tree + self._scene_objects]
if self._visual_rand_config is not None:
    files = self._visual_rand_config.sample(len(tree))
    for file, obj in zip(files, tree):
        if self._visual_rand_config.should_randomize(obj.get_name()):
            text_ob, texture = self._pyrep.create_texture(file)
            try:
                obj.set_texture(texture, **TEX_KWARGS)
            except RuntimeError:
                ungrouped = obj.ungroup()
                for o in ungrouped:
                    o.set_texture(texture, **TEX_KWARGS)
                self._pyrep.group_objects(ungrouped)
            text_ob.remove()
at first a new texture is created, before it is set to the object. At first, I thought that the intense memory usage comes from the fact that a new texture is generated at every iteration. But this does not seem to be the problem. Instead, set_texture() methods seems to be the one using that much memory as I have commented it out and everything seemed to run fine (while still creating a new texture at every iteration). The latter is triggering your
simSetShapeTexture() method.

I think this the same issue mentioned here
(/https://github.com/stepjam/PyRep/issues/219).

I would really appreciate any help. Thanks in advance!

Best,
Firas

coppelia
Site Admin
Posts: 10336
Joined: 14 Dec 2012, 00:25

Re: Crash when setting textures to object at each step

Post by coppelia »

Hello Firas,

thanks for reporting this. This is actually what happens when a new texture is created:
a texture on its own cannot exist in CoppeliaSim. So when a new texture is loaded/created with sim.createTexture, a new, auxiliary shape is also created and the texture is applied to it. The C side of the documentation is not complete: the return value of the call to sim.createTexture is the handle of the auxiliary shape that holds the texture. So eventually that shape will have to be removed (e.g. with sim.removeObject), otherwise the scene will be full of unused shapes. Remove the auxiliary shape after you have applied the texture to the shape it was intended to.

Does that solve the problem, or does it still persist in that case?

Edit: the documentation is actually correct, and is the shape handle of the created auxiliary shape.
Edit2: I just noticed in your code that the auxiliary shape is being removed... then it must be something different. Sorry for the noise, will dig into this again...

Cheers

coppelia
Site Admin
Posts: 10336
Joined: 14 Dec 2012, 00:25

Re: Crash when setting textures to object at each step

Post by coppelia »

There is definitely a problem. Need to check more. But here could be a first workaround:

Instead of removing the auxiliary shape immediately, simply hide it, or attach it on top of a dummy. Then, every n iterations (e.g. 100 or so), remove all auxiliary shapes at once. Then the memory leak doesn't happen it appears (or is reduced by a factor n or similar).

Cheers

Firas
Posts: 3
Joined: 05 Jan 2021, 14:21

Re: Crash when setting textures to object at each step

Post by Firas »

Thanks for the quick reply!

I have tried various things to stop memory leak but unfortunately I couldn't get it running. The only (really bad) workaround I found is to simply shutdown and reload the scene every once in while.

Regarding your workaround suggestion:
Why should deleting the object every n iterations prevent memory leak if not even deleting it every iteration helps?

Best,
Firas

coppelia
Site Admin
Posts: 10336
Joined: 14 Dec 2012, 00:25

Re: Crash when setting textures to object at each step

Post by coppelia »

The bug appears to be tricky, and not directly linked to the texture or shape itself: those are successfully destroyed..

Cheers

Firas
Posts: 3
Joined: 05 Jan 2021, 14:21

Re: Crash when setting textures to object at each step

Post by Firas »

Is there any update on this?
best,
Firas

coppelia
Site Admin
Posts: 10336
Joined: 14 Dec 2012, 00:25

Re: Crash when setting textures to object at each step

Post by coppelia »

I think at the moment there is no better workaround than the one I suggested previously unfortunately. Doesn't that workaround work for you? If not, can you prepare a minimalistic, self-contained scene that illustrates the problem (and why the workaround doesn't work in that situation)?

Cheers

Post Reply