diff options
author | Sona Kurazyan <sona.kurazyan@qt.io> | 2022-08-01 19:43:31 +0200 |
---|---|---|
committer | Sona Kurazyan <sona.kurazyan@qt.io> | 2022-08-05 15:54:06 +0200 |
commit | 1d3fb690cc9262db2c10900b3716512bf13b651f (patch) | |
tree | 1aa833c7fff1afdf1f75db64a68651799a0308c0 /src/corelib/global/qassert.cpp | |
parent | 8b2145463b38291dfa0ca2acf4a85552b7383a60 (diff) |
Move Q_CHECK_PTR and related helpers to qassert.h
Task-number: QTBUG-99313
Change-Id: Ia4152f0aad15975d8aebbbc506c8a76c8fbe144f
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Diffstat (limited to 'src/corelib/global/qassert.cpp')
-rw-r--r-- | src/corelib/global/qassert.cpp | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/src/corelib/global/qassert.cpp b/src/corelib/global/qassert.cpp index 9b7e5d9a38..5e0a366cf2 100644 --- a/src/corelib/global/qassert.cpp +++ b/src/corelib/global/qassert.cpp @@ -5,6 +5,12 @@ #include <QtCore/qlogging.h> +#include <cstdio> +#include <exception> +#ifndef QT_NO_EXCEPTIONS +#include <new> +#endif + QT_BEGIN_NAMESPACE /*! @@ -71,4 +77,64 @@ void qt_assert_x(const char *where, const char *what, const char *file, int line .fatal("ASSERT failure in %s: \"%s\", file %s, line %d", where, what, file, line); } +/*! + \macro void Q_CHECK_PTR(void *pointer) + \relates <QtAssert> + + If \a pointer is \nullptr, prints a message containing the source + code's file name and line number, saying that the program ran out + of memory and aborts program execution. It throws \c std::bad_alloc instead + if exceptions are enabled. + + Q_CHECK_PTR does nothing if \c QT_NO_DEBUG and \c QT_NO_EXCEPTIONS were + defined during compilation. Therefore you must not use Q_CHECK_PTR to check + for successful memory allocations because the check will be disabled in + some cases. + + Example: + + \snippet code/src_corelib_global_qglobal.cpp 21 + + \sa qWarning(), {Debugging Techniques} +*/ + +/*! + \fn template <typename T> T *q_check_ptr(T *p) + \relates <QtAssert> + + Uses Q_CHECK_PTR on \a p, then returns \a p. + + This can be used as an inline version of Q_CHECK_PTR. +*/ + +/*! + \internal + The Q_CHECK_PTR macro calls this function if an allocation check + fails. +*/ +void qt_check_pointer(const char *n, int l) noexcept +{ + // make separate printing calls so that the first one may flush; + // the second one could want to allocate memory (fputs prints a + // newline and stderr auto-flushes). + fputs("Out of memory", stderr); + fprintf(stderr, " in %s, line %d\n", n, l); + + std::terminate(); +} + +/* + \internal + Allows you to throw an exception without including <new> + Called internally from Q_CHECK_PTR on certain OS combinations +*/ +void qBadAlloc() +{ +#ifndef QT_NO_EXCEPTIONS + throw std::bad_alloc(); +#else + std::terminate(); +#endif +} + QT_END_NAMESPACE |