Changeset 561 for trunk/src/corelib/tools/qsharedpointer.cpp
- Timestamp:
- Feb 11, 2010, 11:19:06 PM (15 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk
-
Property svn:mergeinfo
set to (toggle deleted branches)
/branches/vendor/nokia/qt/4.6.1 merged eligible /branches/vendor/nokia/qt/current merged eligible /branches/vendor/trolltech/qt/current 3-149
-
Property svn:mergeinfo
set to (toggle deleted branches)
-
trunk/src/corelib/tools/qsharedpointer.cpp
r2 r561 2 2 ** 3 3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). 4 ** Contact: Qt Software Information ([email protected]) 4 ** All rights reserved. 5 ** Contact: Nokia Corporation ([email protected]) 5 6 ** 6 7 ** This file is part of the QtCore module of the Qt Toolkit. … … 21 22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. 22 23 ** 23 ** In addition, as a special exception, Nokia gives you certain 24 ** additional rights. These rights are described in the Nokia Qt LGPL 25 ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this 26 ** package. 24 ** In addition, as a special exception, Nokia gives you certain additional 25 ** rights. These rights are described in the Nokia Qt LGPL Exception 26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. 27 27 ** 28 28 ** GNU General Public License Usage … … 34 34 ** met: http://www.gnu.org/copyleft/gpl.html. 35 35 ** 36 ** If you are unsure which license is appropriate for your use, please37 ** contact the sales department at qt-sales@nokia.com.36 ** If you 37 ** @nokia.com. 38 38 ** $QT_END_LICENSE$ 39 39 ** … … 51 51 52 52 \reentrant 53 \ingroup misc54 53 55 54 The QSharedPointer is an automatic, shared pointer in C++. It … … 99 98 it creates a copy atomically for the operation to complete. 100 99 101 QExplicitlySharedDataPointer behaves like QSharedDataPointer, 102 except that it only detaches if 103 QExplicitlySharedDataPointer::detach() is explicitly called. 104 105 Finally, QPointer holds a pointer to a QObject-derived object, but 106 it does so weakly. QPointer is similar, in that behaviour, to 107 QWeakPointer: it does not allow you to prevent the object from 108 being destroyed. All you can do is query whether it has been 109 destroyed or not. 110 111 \sa QSharedDataPointer, QWeakPointer 100 QExplicitlySharedDataPointer is a variant of QSharedDataPointer, except 101 that it only detaches if QExplicitlySharedDataPointer::detach() is 102 explicitly called (hence the name). 103 104 QScopedPointer simply holds a pointer to a heap allocated object and 105 deletes it in its destructor. This class is useful when an object needs to 106 be heap allocated and deleted, but no more. QScopedPointer is lightweight, 107 it makes no use of additional structure or reference counting. 108 109 Finally, QPointer holds a pointer to a QObject-derived object, but it 110 does so weakly. QPointer can be replaced by QWeakPointer in almost all 111 cases, since they have the same functionality. See 112 \l{QWeakPointer#tracking-qobject} for more information. 113 114 \section1 Optional pointer tracking 115 116 A feature of QSharedPointer that can be enabled at compile-time for 117 debugging purposes is a pointer tracking mechanism. When enabled, 118 QSharedPointer registers in a global set all the pointers that it tracks. 119 This allows one to catch mistakes like assigning the same pointer to two 120 QSharedPointer objects. 121 122 This function is enabled by defining the \tt{QT_SHAREDPOINTER_TRACK_POINTERS} 123 macro before including the QSharedPointer header. 124 125 It is safe to use this feature even with code compiled without the 126 feature. QSharedPointer will ensure that the pointer is removed from the 127 tracker even from code compiled without pointer tracking. 128 129 Note, however, that the pointer tracking feature has limitations on 130 multiple- or virtual-inheritance (that is, in cases where two different 131 pointer addresses can refer to the same object). In that case, if a 132 pointer is cast to a different type and its value changes, 133 QSharedPointer's pointer tracking mechanism mail fail to detect that the 134 object being tracked is the same. 135 136 \omit 137 \secton1 QSharedPointer internals 138 139 QSharedPointer is in reality implemented by two ancestor classes: 140 QtSharedPointer::Basic and QtSharedPointer::ExternalRefCount. The reason 141 for having that split is now mostly legacy: in the beginning, 142 QSharedPointer was meant to support both internal reference counting and 143 external reference counting. 144 145 QtSharedPointer::Basic implements the basic functionality that is shared 146 between internal- and external-reference counting. That is, it's mostly 147 the accessor functions into QSharedPointer. Those are all inherited by 148 QSharedPointer, which adds another level of shared functionality (the 149 constructors and assignment operators). The Basic class has one member 150 variable, which is the actual pointer being tracked. 151 152 QtSharedPointer::ExternalRefCount implements the actual reference 153 counting and introduces the d-pointer for QSharedPointer. That d-pointer 154 itself is shared with with other QSharedPointer objects as well as 155 QWeakPointer. 156 157 The reason for keeping the pointer value itself outside the d-pointer is 158 because of multiple inheritance needs. If you have two QSharedPointer 159 objects of different pointer types, but pointing to the same object in 160 memory, it could happen that the pointer values are different. The \tt 161 differentPointers autotest exemplifies this problem. The same thing could 162 happen in the case of virtual inheritance: a pointer of class matching 163 the virtual base has different address compared to the pointer of the 164 complete object. See the \tt virtualBaseDifferentPointers autotest for 165 this problem. 166 167 The d pointer is of type QtSharedPointer::ExternalRefCountData for simple 168 QSharedPointer objects, but could be of a derived type in some cases. It 169 is basically a reference-counted reference-counter. 170 171 \section2 d-pointer 172 \section3 QtSharedPointer::ExternalRefCountData 173 174 This class is basically a reference-counted reference-counter. It has two 175 members: \tt strongref and \tt weakref. The strong reference counter is 176 controlling the lifetime of the object tracked by QSharedPointer. a 177 positive value indicates that the object is alive. It's also the number 178 of QSharedObject instances that are attached to this Data. 179 180 When the strong reference count decreases to zero, the object is deleted 181 (see below for information on custom deleters). The strong reference 182 count can also exceptionally be -1, indicating that there are no 183 QSharedPointers attached to an object, which is tracked too. The only 184 case where this is possible is that of 185 \l{QWeakPointer#tracking-qobject}{QWeakPointers tracking a QObject}. 186 187 The weak reference count controls the lifetime of the d-pointer itself. 188 It can be thought of as an internal/intrusive reference count for 189 ExternalRefCountData itself. This count is equal to the number of 190 QSharedPointers and QWeakPointers that are tracking this object. (In case 191 the object tracked derives from QObject, this number is increased by 1, 192 since QObjectPrivate tracks it too). 193 194 ExternalRefCountData is a virtual class: it has a virtual destructor and 195 a virtual destroy() function. The destroy() function is supposed to 196 delete the object being tracked and return true if it does so. Otherwise, 197 it returns false to indicate that the caller must simply call delete. 198 This allows the normal use-case of QSharedPointer without custom deleters 199 to use only one 12- or 16-byte (depending on whether it's a 32- or 64-bit 200 architecture) external descriptor structure, without paying the price for 201 the custom deleter that it isn't using. 202 203 \section3 QtSharedPointer::ExternalRefCountDataWithDestroyFn 204 205 This class is not used directly, per se. It only exists to enable the two 206 classes that derive from it. It adds one member variable, which is a 207 pointer to a function (which returns void and takes an 208 ExternalRefCountData* as a parameter). It also overrides the destroy() 209 function: it calls that function pointer with \tt this as parameter, and 210 returns true. 211 212 That means when ExternalRefCountDataWithDestroyFn is used, the \tt 213 destroyer field must be set to a valid function that \b will delete the 214 object tracked. 215 216 This class also adds an operator delete function to ensure that simply 217 calls the global operator delete. That should be the behaviour in all 218 compilers already, but to be on the safe side, this class ensures that no 219 funny business happens. 220 221 On a 32-bit architecture, this class is 16 bytes in size, whereas it's 24 222 bytes on 64-bit. (On Itanium where function pointers contain the global 223 pointer, it can be 32 bytes). 224 225 \section3 QtSharedPointer::ExternalRefCountWithCustomDeleter 226 227 This class derives from ExternalRefCountDataWithDestroyFn and is a 228 template class. As template parameters, it has the type of the pointer 229 being tracked (\tt T) and a \tt Deleter, which is anything. It adds two 230 fields to its parent class, matching those template parameters: a member 231 of type \tt Deleter and a member of type \tt T*. 232 233 The purpose of this class is to store the pointer to be deleted and the 234 deleter code along with the d-pointer. This allows the last strong 235 reference to call any arbitrary function that disposes of the object. For 236 example, this allows calling QObject::deleteLater() on a given object. 237 The pointer to the object is kept here to avoid the extra cost of keeping 238 the deleter in the generic case. 239 240 This class is never instantiated directly: the constructors and 241 destructor are private. Only the create() function may be called to 242 return an object of this type. See below for construction details. 243 244 The size of this class depends on the size of \tt Deleter. If it's an 245 empty functor (i.e., no members), ABIs generally assign it the size of 1. 246 But given that it's followed by a pointer, up to 3 or 7 padding bytes may 247 be inserted: in that case, the size of this class is 16+4+4 = 24 bytes on 248 32-bit architectures, or 24+8+8 = 40 bytes on 64-bit architectures (48 249 bytes on Itanium with global pointers stored). If \tt Deleter is a 250 function pointer, the size should be the same as the empty structure 251 case, except for Itanium where it may be 56 bytes due to another global 252 pointer. If \tt Deleter is a pointer to a member function (PMF), the size 253 will be even bigger and will depend on the ABI. For architectures using 254 the Itanium C++ ABI, a PMF is twice the size of a normal pointer, or 24 255 bytes on Itanium itself. In that case, the size of this structure will be 256 16+8+4 = 28 bytes on 32-bit architectures, 24+16+8 = 48 bytes on 64-bit, 257 and 32+24+8 = 64 bytes on Itanium. 258 259 (Values for Itanium consider an LP64 architecture; for ILP32, pointers 260 are 32-bit in length, function pointers are 64-bit and PMF are 96-bit, so 261 the sizes are slightly less) 262 263 \section3 QtSharedPointer::ExternalRefCountWithContiguousData 264 265 This class also derives from ExternalRefCountDataWithDestroyFn and it is 266 also a template class. The template parameter is the type \tt T of the 267 class which QSharedPointer tracks. It adds only one member to its parent, 268 which is of type \tt T (the actual type, not a pointer to it). 269 270 The purpose of this class is to lay the \tt T object out next to the 271 reference counts, saving one memory allocation per shared pointer. This 272 is particularly interesting for small \tt T or for the cases when there 273 are few if any QWeakPointer tracking the object. This class exists to 274 implement the QSharedPointer::create() call. 275 276 Like ExternalRefCountWithCustomDeleter, this class is never instantiated 277 directly. This class also provides a create() member that returns the 278 pointer, and hides its constructors and destructor. (With C++0x, we'd 279 delete them). 280 281 The size of this class depends on the size of \tt T. 282 283 \section3 Instantiating ExternalRefCountWithCustomDeleter and ExternalRefCountWithContiguousData 284 285 Like explained above, these classes have private constructors. Moreover, 286 they are not defined anywhere, so trying to call \tt{new ClassType} would 287 result in a compilation or linker error. Instead, these classes must be 288 constructed via their create() methods. 289 290 Instead of instantiating the class by the normal way, the create() method 291 calls \tt{operator new} directly with the size of the class, then calls 292 the parent class's constructor only (ExternalRefCountDataWithDestroyFn). 293 This ensures that the inherited members are initialised properly, as well 294 as the virtual table pointer, which must point to 295 ExternalRefCountDataWithDestroyFn's virtual table. That way, we also 296 ensure that the virtual destructor being called is 297 ExternalRefCountDataWithDestroyFn's. 298 299 After initialising the base class, the 300 ExternalRefCountWithCustomDeleter::create() function initialises the new 301 members directly, by using the placement \tt{operator new}. In the case 302 of the ExternalRefCountWithContiguousData::create() function, the address 303 to the still-uninitialised \tt T member is saved for the callee to use. 304 The member is only initialised in QSharedPointer::create(), so that we 305 avoid having many variants of the internal functions according to the 306 arguments in use for calling the constructor. 307 308 When initialising the parent class, the create() functions pass the 309 address of the static deleter() member function. That is, when the 310 virtual destroy() is called by QSharedPointer, the deleter() functions 311 are called instead. These functiosn static_cast the ExternalRefCountData* 312 parameter to their own type and execute their deletion: for the 313 ExternalRefCountWithCustomDeleter::deleter() case, it runs the user's 314 custom deleter, then destroys the deleter; for 315 ExternalRefCountWithContiguousData::deleter, it simply calls the \tt T 316 destructor directly. 317 318 By not calling the constructor of the derived classes, we avoid 319 instantiating their virtual tables. Since these classes are 320 template-based, there would be one virtual table per \tt T and \tt 321 Deleter type. (This is what Qt 4.5 did) 322 323 Instead, only one non-inline function is required per template, which is 324 the deleter() static member. All the other functions can be inlined. 325 What's more, the address of deleter() is calculated only in code, which 326 can be resolved at link-time if the linker can determine that the 327 function lies in the current application or library module (since these 328 classes are not exported, that is the case for Windows or for builds with 329 \tt{-fvisibility=hidden}). 330 331 In contrast, a virtual table would require at least 3 relocations to be 332 resolved at module load-time, per module where these classes are used. 333 (In the Itanium C++ ABI, there would be more relocations, due to the 334 RTTI) 335 336 \section3 Modifications due to pointer-tracking 337 338 To ensure that pointers created with pointer-tracking enabled get 339 un-tracked when destroyed, even if destroyed by code compiled without the 340 feature, QSharedPointer modifies slightly the instructions of the 341 previous sections. 342 343 When ExternalRefCountWithCustomDeleter or 344 ExternalRefCountWithContiguousData are used, their create() functions 345 will set the ExternalRefCountDataWithDestroyFn::destroyer function 346 pointer to safetyCheckDeleter() instead. These static member functions 347 simply call internalSafetyCheckRemove2() before passing control to the 348 normal deleter() function. 349 350 If neither custom deleter nor QSharedPointer::create() are used, then 351 QSharedPointer uses a custom deleter of its own: the normalDeleter() 352 function, which simply calls \tt delete. By using a custom deleter, the 353 safetyCheckDeleter() procedure described above kicks in. 354 355 \endomit 356 357 \sa QSharedDataPointer, QWeakPointer, QScopedPointer 112 358 */ 113 359 … … 117 363 \since 4.5 118 364 \reentrant 119 \ingroup misc120 365 121 366 The QWeakPointer is an automatic weak reference to a … … 124 369 deleted or not in another context. 125 370 126 QWeakPointer objects can only be created by assignment 127 from a QSharedPointer. 128 129 To access the pointer that QWeakPointer is tracking, you 130 must first create a QSharedPointer object and verify if the pointer 131 is null or not. 132 133 \sa QSharedPointer 371 QWeakPointer objects can only be created by assignment from a 372 QSharedPointer. The exception is pointers derived from QObject: in that 373 case, QWeakPointer serves as a replacement to QPointer. 374 375 It's important to note that QWeakPointer provides no automatic casting 376 operators to prevent mistakes from happening. Even though QWeakPointer 377 tracks a pointer, it should not be considered a pointer itself, since it 378 doesn't guarantee that the pointed object remains valid. 379 380 Therefore, to access the pointer that QWeakPointer is tracking, you must 381 first promote it to QSharedPointer and verify if the resulting object is 382 null or not. QSharedPointer guarantees that the object isn't deleted, so 383 if you obtain a non-null object, you may use the pointer. See 384 QWeakPointer::toStrongRef() for more an example. 385 386 QWeakPointer also provides the QWeakPointer::data() method that returns 387 the tracked pointer without ensuring that it remains valid. This function 388 is provided if you can guarantee by external means that the object will 389 not get deleted (or if you only need the pointer value) and the cost of 390 creating a QSharedPointer using toStrongRef() is too high. 391 392 That function can also be used to obtain the tracked pointer for 393 QWeakPointers that cannot be promoted to QSharedPointer, such as those 394 created directly from a QObject pointer (not via QSharedPointer). 395 396 \section1 Tracking QObject 397 398 QWeakPointer can be used to track deletion classes derives from QObject, 399 even if they are not managed by QSharedPointer. When used in that role, 400 QWeakPointer replaces the older QPointer in all use-cases. QWeakPointer 401 is also more efficient than QPointer, so it should be preferred in all 402 new code. 403 404 To do that, QWeakPointer provides a special constructor that is only 405 available if the template parameter \tt T is either QObject or a class 406 deriving from it. Trying to use that constructor if \tt T does not derive 407 from QObject will result in compilation errors. 408 409 To obtain the QObject being tracked by QWeakPointer, you must use the 410 QWeakPointer::data() function, but only if you can guarantee that the 411 object cannot get deleted by another context. It should be noted that 412 QPointer had the same constraint, so use of QWeakPointer forces you to 413 consider whether the pointer is still valid. 414 415 QObject-derived classes can only be deleted in the thread they have 416 affinity to (which is the thread they were created in or moved to, using 417 QObject::moveToThread()). In special, QWidget-derived classes cannot be 418 created in non-GUI threads nor moved there. Therefore, guaranteeing that 419 the tracked QObject has affinity to the current thread is enough to also 420 guarantee that it won't be deleted asynchronously. 421 422 Note that QWeakPointer's size and data layout do not match QPointer, so 423 it cannot replace that class in a binary-compatible manner. 424 425 Care must also be taken with QWeakPointers created directly from QObject 426 pointers when dealing with code that was compiled with Qt versions prior 427 to 4.6. Those versions may not track the reference counters correctly, so 428 QWeakPointers created from QObject should never be passed to code that 429 hasn't been recompiled. 430 431 \omit 432 \secton1 QWeakPointer internals 433 434 QWeakPointer shares most of its internal functionality with 435 \l{QSharedPointer#qsharedpointer-internals}{QSharedPointer}, so see that 436 class's internal documentation for more information. 437 438 QWeakPointer requires an external reference counter in order to operate. 439 Therefore, it is incompatible by design with \l QSharedData-derived 440 classes. 441 442 It has a special QObject constructor, which works by calling 443 QtSharedPointer::ExternalRefCountData::getAndRef, which retrieves the 444 d-pointer from QObjectPrivate. If one isn't set yet, that function 445 creates the d-pointer and atomically sets it. 446 447 If getAndRef needs to create a d-pointer, it sets the strongref to -1, 448 indicating that the QObject is not shared: QWeakPointer is used only to 449 determine whether the QObject has been deleted. In that case, it cannot 450 be upgraded to QSharedPointer (see the previous section). 451 452 \endomit 453 454 \sa QSharedPointer, QScopedPointer 134 455 */ 135 456 … … 211 532 class, QSharedPointer will perform an automatic cast. Otherwise, 212 533 you will get a compiler error. 534 535 213 536 */ 214 537 … … 341 664 342 665 /*! 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 343 684 \fn QWeakPointer<T> QSharedPointer::toWeakRef() const 344 685 345 686 Returns a weak reference object that shares the pointer referenced 346 687 by this object. 688 689 347 690 */ 348 691 … … 388 731 class, QWeakPointer will perform an automatic cast. Otherwise, 389 732 you will get a compiler error. 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 390 762 */ 391 763 … … 461 833 462 834 /*! 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 463 877 \fn QSharedPointer<T> QWeakPointer::toStrongRef() const 464 878 465 879 Promotes this weak reference to a strong one and returns a 466 QSharedPointer object holding that reference. 880 QSharedPointer object holding that reference. When promoting to 881 QSharedPointer, this function verifies if the object has been deleted 882 already or not. If it hasn't, this function increases the reference 883 count to the shared object, thus ensuring that it will not get 884 deleted. 885 886 Since this function can fail to obtain a valid strong reference to the 887 shared object, you should always verify if the conversion succeeded, 888 by calling QSharedPointer::isNull() on the returned object. 889 890 For example, the following code promotes a QWeakPointer that was held 891 to a strong reference and, if it succeeded, it prints the value of the 892 integer that was held: 893 894 \code 895 QWeakPointer<int> weakref; 896 897 // ... 898 899 QSharedPointer<int> strong = weakref.toStrongRef(); 900 if (strong) 901 qDebug() << "The value is:" << *strong; 902 else 903 qDebug() << "The value has already been deleted"; 904 \endcode 905 906 \sa QSharedPointer::QSharedPointer() 467 907 */ 468 908 … … 719 1159 720 1160 /*! 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 721 1206 \fn QWeakPointer<X> qWeakPointerCast(const QWeakPointer<T> &other) 722 1207 \relates QWeakPointer … … 733 1218 #include <qset.h> 734 1219 #include <qmutex.h> 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 735 1277 736 1278 #if !defined(QT_NO_MEMBER_TEMPLATES) … … 745 1287 # endif 746 1288 747 # if !defined(BACKTRACE_SUPPORTED) 748 // Dummy implementation of the functions. 749 // Using QHashDummyValue also means that the QHash below is actually a QSet 750 typedef QT_PREPEND_NAMESPACE(QHashDummyValue) Backtrace; 751 752 static inline Backtrace saveBacktrace() { return Backtrace(); } 753 static inline void printBacktrace(Backtrace) { } 754 755 # else 1289 # if defined(BACKTRACE_SUPPORTED) 756 1290 # include <sys/types.h> 757 1291 # include <execinfo.h> … … 760 1294 # include <sys/wait.h> 761 1295 762 typedef QT_PREPEND_NAMESPACE(QByteArray) Backtrace; 763 764 static inline BacktracesaveBacktrace() __attribute__((always_inline));765 static inline BacktracesaveBacktrace()1296 QT_BEGIN_NAMESPACE 1297 1298 static inline saveBacktrace() __attribute__((always_inline)); 1299 static inline saveBacktrace() 766 1300 { 767 1301 static const int maxFrames = 32; 768 1302 769 Backtracestacktrace;1303 stacktrace; 770 1304 stacktrace.resize(sizeof(void*) * maxFrames); 771 1305 int stack_size = backtrace((void**)stacktrace.data(), maxFrames); … … 775 1309 } 776 1310 777 static void printBacktrace( Backtracestacktrace)1311 static void printBacktrace( stacktrace) 778 1312 { 779 1313 void *const *stack = (void *const *)stacktrace.constData(); … … 822 1356 } 823 1357 } 1358 1359 1360 824 1361 # endif // BACKTRACE_SUPPORTED 825 1362 826 1363 namespace { 827 1364 QT_USE_NAMESPACE 1365 1366 1367 1368 1369 1370 1371 828 1372 class KnownPointers 829 1373 { 830 1374 public: 831 1375 QMutex mutex; 832 QHash<void *, Backtrace> values; 1376 QHash<const void *, Data> dPointers; 1377 QHash<const volatile void *, const void *> dataPointers; 833 1378 }; 834 1379 } … … 838 1383 QT_BEGIN_NAMESPACE 839 1384 1385 1386 1387 1388 1389 1390 840 1391 /*! 841 1392 \internal 842 1393 */ 843 void QtSharedPointer::internalSafetyCheckAdd(const volatile void *ptr) 1394 void QtSharedPointer::internalSafetyCheckAdd(const volatile void *) 1395 { 1396 // Qt 4.5 compatibility 1397 // this function is broken by design, so it was replaced with internalSafetyCheckAdd2 1398 // 1399 // it's broken because we tracked the pointers added and 1400 // removed from QSharedPointer, converted to void*. 1401 // That is, this is supposed to track the "top-of-object" pointer in 1402 // case of multiple inheritance. 1403 // 1404 // However, it doesn't work well in some compilers: 1405 // if you create an object with a class of type A and the last reference 1406 // is dropped of type B, then the value passed to internalSafetyCheckRemove could 1407 // be different than was added. That would leave dangling addresses. 1408 // 1409 // So instead, we track the pointer by the d-pointer instead. 1410 } 1411 1412 /*! 1413 \internal 1414 */ 1415 void QtSharedPointer::internalSafetyCheckRemove(const volatile void *) 1416 { 1417 // Qt 4.5 compatibility 1418 // see comments above 1419 } 1420 1421 /*! 1422 \internal 1423 */ 1424 void QtSharedPointer::internalSafetyCheckAdd2(const void *d_ptr, const volatile void *ptr) 1425 { 1426 // see comments above for the rationale for this function 1427 KnownPointers *const kp = knownPointers(); 1428 if (!kp) 1429 return; // end-game: the application is being destroyed already 1430 1431 QMutexLocker lock(&kp->mutex); 1432 Q_ASSERT(!kp->dPointers.contains(d_ptr)); 1433 1434 //qDebug("Adding d=%p value=%p", d_ptr, ptr); 1435 1436 const void *other_d_ptr = kp->dataPointers.value(ptr, 0); 1437 if (other_d_ptr) { 1438 # ifdef BACKTRACE_SUPPORTED 1439 printBacktrace(knownPointers()->dPointers.value(other_d_ptr).backtrace); 1440 # endif 1441 qFatal("QSharedPointer: internal self-check failed: pointer %p was already tracked " 1442 "by another QSharedPointer object %p", ptr, other_d_ptr); 1443 } 1444 1445 Data data; 1446 data.pointer = ptr; 1447 # ifdef BACKTRACE_SUPPORTED 1448 data.backtrace = saveBacktrace(); 1449 # endif 1450 1451 kp->dPointers.insert(d_ptr, data); 1452 kp->dataPointers.insert(ptr, d_ptr); 1453 Q_ASSERT(kp->dPointers.size() == kp->dataPointers.size()); 1454 } 1455 1456 /*! 1457 \internal 1458 */ 1459 void QtSharedPointer::internalSafetyCheckRemove2(const void *d_ptr) 844 1460 { 845 1461 KnownPointers *const kp = knownPointers(); … … 848 1464 849 1465 QMutexLocker lock(&kp->mutex); 850 void *actual = const_cast<void*>(ptr); 851 if (kp->values.contains(actual)) { 852 printBacktrace(knownPointers()->values.value(actual)); 853 qFatal("QSharedPointerData: internal self-check failed: pointer %p was already tracked " 854 "by another QSharedPointerData object", actual); 1466 1467 QHash<const void *, Data>::iterator it = kp->dPointers.find(d_ptr); 1468 if (it == kp->dPointers.end()) { 1469 qFatal("QSharedPointer: internal self-check inconsistency: pointer %p was not tracked. " 1470 "To use QT_SHAREDPOINTER_TRACK_POINTERS, you have to enable it throughout " 1471 "in your code.", d_ptr); 855 1472 } 856 1473 857 kp->values.insert(actual, saveBacktrace()); 1474 QHash<const volatile void *, const void *>::iterator it2 = kp->dataPointers.find(it->pointer); 1475 Q_ASSERT(it2 != kp->dataPointers.end()); 1476 1477 //qDebug("Removing d=%p value=%p", d_ptr, it->pointer); 1478 1479 // remove entries 1480 kp->dataPointers.erase(it2); 1481 kp->dPointers.erase(it); 1482 Q_ASSERT(kp->dPointers.size() == kp->dataPointers.size()); 858 1483 } 859 1484 860 1485 /*! 861 1486 \internal 862 */ 863 void QtSharedPointer::internalSafetyCheckRemove(const volatile void *ptr) 1487 Called by the QSharedPointer autotest 1488 */ 1489 void QtSharedPointer::internalSafetyCheckCleanCheck() 864 1490 { 1491 865 1492 KnownPointers *const kp = knownPointers(); 866 if (!kp) 867 return; // end-game: the application is being destroyed already 868 869 QMutexLocker lock(&kp->mutex); 870 void *actual = const_cast<void*>(ptr); 871 kp->values.remove(actual); 1493 Q_ASSERT_X(kp, "internalSafetyCheckSelfCheck()", "Called after global statics deletion!"); 1494 1495 if (kp->dPointers.size() != kp->dataPointers.size()) 1496 qFatal("Internal consistency error: the number of pointers is not equal!"); 1497 1498 if (!kp->dPointers.isEmpty()) 1499 qFatal("Pointer cleaning failed: %d entries remaining", kp->dPointers.size()); 1500 # endif 872 1501 } 873 1502
Note:
See TracChangeset
for help on using the changeset viewer.