Is this possible without a leak

Hi, I am getting a leak. Should this work out of the box?
Thanks in advance.

namespace CommonFunctionLambdas
        template<typename Type>
        inline std::function<ReferenceCountedObjectPtr<Type> (ValueTree)> newReferenceCountedObject
        =  []( ValueTree valueTree)
            ReferenceCountedObjectPtr<Type> returnObject    =  new Type (valueTree);
            valueTree.setProperty (
                                   , dynamic_cast<Type*> ( returnObject.get() )
                                   , nullptr
            return returnObject;

inline std::function<Type* (ValueTree)> is all that was necessary to populate a ReferenceCountedObjectPtr constructor with an inline Lambda.

This appeared to work in a stripped down version, but that was temporary. Still investigating?

If manually remove the reference counted object from the valuetree property before the destructor is called, there is no leak. I would like that extra shared_ptr like reference counted object without that hassle, but I will work around it.