Allow copying scoped_nsobject of different type.
commit941226ba0e49b1e2d359ea084ecb00c672dd6054
authordroger <droger@chromium.org>
Wed, 14 Jan 2015 16:17:45 +0000 (14 08:17 -0800)
committerCommit bot <commit-bot@chromium.org>
Wed, 14 Jan 2015 16:19:03 +0000 (14 16:19 +0000)
tree576347a73a2bb73286aabd9a1efdbe45065e2738
parentf6ade006723de6a100b6dd6b545cb5916b197925
Allow copying scoped_nsobject of different type.

This code was leading to a crash:
scoped_nsobject<MyClass> a([[MyClass alloc] init]);
scoped_nsobject<NSObject> b(a);

Because |a| is converted to raw pointer, and |b| is constructed with the
raw pointer, without calling retain. This creates a subtle crash later on
when the scoped_nsobject are destroyed.

This code however, was not crashing:
scoped_nsobject<MyClass> b(a);
Because in that case the copy constructor is used.

This was somewhat inconsistent and bug prone, especially if the type of
|a| is changed. In that case the type of |b| must be changed at the same
time, or it causes a crash.

This CL introduces a new constructor for the scoped_nsobject variants
that allows passing a scoped_nsobject of a different type.
It is consistent with scoped_refptr which also has this constructor.

Review URL: https://codereview.chromium.org/848033006

Cr-Commit-Position: refs/heads/master@{#311487}
base/mac/scoped_nsobject.h