Fix resource leakage in capacity (similar to
f2152321f6e21ea7caac903ecde5717c16c3e77d)