src/corelib/codecs/qtextcodec.cpp
changeset 22 79de32ba3296
parent 18 2f34d5167611
child 30 5dc02b23752f
--- a/src/corelib/codecs/qtextcodec.cpp	Mon May 03 13:17:34 2010 +0300
+++ b/src/corelib/codecs/qtextcodec.cpp	Fri May 14 16:40:13 2010 +0300
@@ -110,6 +110,7 @@
     (QTextCodecFactoryInterface_iid, QLatin1String("/codecs")))
 #endif
 
+
 static char qtolower(register char c)
 { if (c >= 'A' && c <= 'Z') return c + 0x20; return c; }
 static bool qisalnum(register char c)
@@ -224,6 +225,19 @@
 
 Q_GLOBAL_STATIC(QTextCodecCleanup, createQTextCodecCleanup)
 
+bool QTextCodec::validCodecs()
+{
+#ifdef Q_OS_SYMBIAN
+    // If we don't have a trap handler, we're outside of the main() function,
+    // ie. in global constructors or destructors. Don't use codecs in this
+    // case as it would lead to crashes because we don't have a cleanup stack on Symbian
+    return (User::TrapHandler() != NULL);
+#else
+    return true;
+#endif
+}
+
+
 #if defined(Q_OS_WIN32) || defined(Q_OS_WINCE)
 class QWindowsLocalCodec: public QTextCodec
 {
@@ -684,6 +698,14 @@
     if (all)
         return;
 
+#ifdef Q_OS_SYMBIAN
+    // If we don't have a trap handler, we're outside of the main() function,
+    // ie. in global constructors or destructors. Don't create codecs in this
+    // case as it would lead to crashes because of a missing cleanup stack on Symbian
+    if (User::TrapHandler() == NULL)
+        return;
+#endif
+
 #ifdef Q_DEBUG_TEXTCODEC
     if (destroying_is_ok)
         qWarning("QTextCodec: Creating new codec during codec cleanup");
@@ -986,6 +1008,9 @@
 #endif
     setup();
 
+    if (!validCodecs())
+        return 0;
+
     static QHash <QByteArray, QTextCodec *> cache;
     if (clearCaches & 0x1) {
         cache.clear();
@@ -1027,6 +1052,9 @@
 #endif
     setup();
 
+    if (!validCodecs())
+        return 0;
+
     static QHash <int, QTextCodec *> cache;
     if (clearCaches & 0x2) {
         cache.clear();
@@ -1074,6 +1102,10 @@
     setup();
 
     QList<QByteArray> codecs;
+
+    if (!validCodecs())
+        return codecs;
+
     for (int i = 0; i < all->size(); ++i) {
         codecs += all->at(i)->name();
         codecs += all->at(i)->aliases();
@@ -1112,6 +1144,10 @@
     setup();
 
     QList<int> codecs;
+
+    if (!validCodecs())
+        return codecs;
+
     for (int i = 0; i < all->size(); ++i)
         codecs += all->at(i)->mibEnum();
 
@@ -1165,6 +1201,9 @@
 
 QTextCodec* QTextCodec::codecForLocale()
 {
+    if (!validCodecs())
+        return 0;
+
     if (localeMapper)
         return localeMapper;