//begin public api Lav_PUBLIC_FUNCTION LavError Lav_createBufferNode(LavHandle simulationHandle, LavHandle* destination) { PUB_BEGIN auto simulation = incomingObject<Simulation>(simulationHandle); LOCK(*simulation); auto retval = createBufferNode(simulation); *destination = outgoingObject<Node>(retval); PUB_END }
void EnvironmentNode::playAsync(std::shared_ptr<Buffer> buffer, float x, float y, float z) { auto e = std::static_pointer_cast<EnvironmentNode>(shared_from_this()); auto s = createSourceNode(simulation, e); auto b = createBufferNode(simulation); b->getProperty(Lav_BUFFER_BUFFER).setBufferValue(buffer); b->connect(0, s, 0); s->getProperty(Lav_3D_POSITION).setFloat3Value(x, y, z); //The key here is that we capture the shared pointers, holding them until the event fires. //When the event fires, we null the pointers we captured, and then everything schedules for deletion. //We need the simulation shared pointer. auto simulation = this->simulation; b->getEvent(Lav_BUFFER_END_EVENT).setHandler([b, e, s, simulation] (Node* unused1, void* unused2) mutable { //Recall that events do not hold locks when fired. //If we lock anything we delete here, it will not unlock properly. //So lock the simulation. LOCK(*simulation); if(b) b->disconnect(0); b.reset(); s.reset(); e.reset(); }); }