Question

I'm trying to track a very difficult to reproduce bug. I have a pool of items and use the following to automatically check the pool items back in when the client has finished with them:

typedef std::shared_ptr<T> Handle;

Handle MyPool::checkOut()
{
    static const auto CheckInDeleter = [this](T* item)
    {
        this->checkIn(item);
    };

    return Handle(item, CheckInDeleter);
}

My question is is this unsafe? Will this be assigned to the first pool that checks out an item?

Was it helpful?

Solution

Yes, static function-scope variables are assigned once,so there will be only one lambda object, that will refer to first MyPool that has checkOut member function called. It is probably not what you need, so just remove static.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top