ci::Anim, timeline and memory safety

Hey Embers,

Initially, when I started using ci::Anim and the associated ci::Timeline object, I noticed that the appendTo() and similar functions accepted an address of the animation object to be modified. I somehow got it into my head that the underlying LibCinder code was pretty brittle and couldn’t handle a ci::Anim object going out of scope while it was being updated.

Having played around with ci::Anim more, it actually seems like the object is quite robust, and handles both being copied, moved, and destructed quite responsibly.

So I guess I’m writing this kind of as both a PSA to others, and also in the effort to hear if anyone else has run into any ‘don’t’s, when using ci::Anim?

Cheers,
Gazoo

Hey Gazoo -

I think your conclusion is right - the ci::Anim system is actually reasonably robust and handles memory safety in most cases automatically. I think you might have been remembering though the difference between the two APIs that Timeline offers.

The Safe Way - Using Anim:

When you use timeline->apply( &myAnim, targetValue, duration ), there’s a crucial safety mechanism happening where the Anim is registered with the Timeline.

This creates a shared_ptr connection between the Anim and the Timeline. So even if your Anim goes out of scope while being animated, the system handles it gracefully - the Anim’s destructor will properly clean up and remove itself from the Timeline before the memory is freed.

The “Advanced Use Case” Way - Using Raw Pointers:

The applyPtr() and appendToPtr() methods are marked as “advanced use case” for this reason. These take raw T* pointers directly. With applyPtr(), you’re on your own for lifetime management - the Timeline just stores the raw pointer and will happily try to update it even if the object it points to has been destroyed or has moved (e.g. is part of a std::vector).

So the summary I’d say is if you use Anim objects with the regular apply() / appendTo() methods, the system handles all the lifecycle stuff for you. Only use the Ptr variants if you know exactly what you’re doing and are managing the lifetime yourself.

-Andrew