From ae445b20fa2567f7e14d989f288edb69b0904433 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Sun, 19 Feb 2012 20:53:20 +0100 Subject: QEasingCurve: simplify and fix copy constructor The copy constructor used the default Private constructor, followed by the application of the compiler-generated copy assignment operator, and finally replaced the config member with a copy of itself. This is needlessly inefficient. Worse: it's incorrect: if config->copy() throws, then *d_ptr is leaked. Solution: implement the copy constructor for Private, and use it in the copy constructor of the public class. Effect: everything that can throw now prevents the new Private class from being created, and the compiler ends up cleaning up after us. Change-Id: I09ed18bb39ee7cd81aaa8ba01676fc202502a8e3 Reviewed-by: Lars Knoll --- src/corelib/tools/qeasingcurve.cpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'src/corelib') diff --git a/src/corelib/tools/qeasingcurve.cpp b/src/corelib/tools/qeasingcurve.cpp index 6f8572cfbf..7a47262798 100644 --- a/src/corelib/tools/qeasingcurve.cpp +++ b/src/corelib/tools/qeasingcurve.cpp @@ -419,6 +419,11 @@ public: config(0), func(&easeNone) { } + QEasingCurvePrivate(const QEasingCurvePrivate &other) + : type(other.type), + config(other.config ? other.config->copy() : 0), + func(other.func) + { } ~QEasingCurvePrivate() { delete config; } void setType_helper(QEasingCurve::Type); @@ -1080,12 +1085,9 @@ QEasingCurve::QEasingCurve(Type type) Construct a copy of \a other. */ QEasingCurve::QEasingCurve(const QEasingCurve &other) - : d_ptr(new QEasingCurvePrivate) + : d_ptr(new QEasingCurvePrivate(*other.d_ptr)) { // ### non-atomic, requires malloc on shallow copy - *d_ptr = *other.d_ptr; - if (other.d_ptr->config) - d_ptr->config = other.d_ptr->config->copy(); } /*! -- cgit v1.2.3