• Jason Rhinelander's avatar
    Don't construct unique_ptr around unowned pointers (#478) · c07ec31e
    Jason Rhinelander authored
    If we need to initialize a holder around an unowned instance, and the
    holder type is non-copyable (i.e. a unique_ptr), we currently construct
    the holder type around the value pointer, but then never actually
    destruct the holder: the holder destructor is called only for the
    instance that actually has `inst->owned = true` set.
    
    This seems no pointer, however, in creating such a holder around an
    unowned instance: we never actually intend to use anything that the
    unique_ptr gives us: and, in fact, do not want the unique_ptr (because
    if it ever actually got destroyed, it would cause destruction of the
    wrapped pointer, despite the fact that that wrapped pointer isn't
    owned).
    
    This commit changes the logic to only create a unique_ptr holder if we
    actually own the instance, and to destruct via the constructed holder
    whenever we have a constructed holder--which will now only be the case
    for owned-unique-holder or shared-holder types.
    
    Other changes include:
    
    * Added test for non-movable holder constructor/destructor counts
    
    The three alive assertions now pass, before #478 they fail with counts
    of 2/2/1 respectively, because of the unique_ptr that we don't want and
    don't destroy (because we don't *want* its destructor to run).
    
    * Return cstats reference; fix ConstructStats doc
    
    Small cleanup to the #478 test code, and fix to the ConstructStats
    documentation (the static method definition should use `reference` not
    `reference_internal`).
    
    * Rename inst->constructed to inst->holder_constructed
    
    This makes it clearer exactly what it's referring to.
    c07ec31e
test_issues.cpp 13.7 KB