Skip to content

Commit

Permalink
Eliminate complex typed static variables
Browse files Browse the repository at this point in the history
Complex typed static variables (e.g. maps) in dll-s are to avoid.
On Windows they are not initialised in time (or at all) when the dll
is dynamically loaded. The current use of static QHash data members
results in crash on Windows when the PythonQt.dll is loaded.

See details here:
http://stackoverflow.com/questions/5114683/loading-dll-not-initializing-static-c-classes

This commit is broken. It is pushed only for demonstration.

DO NOT MERGE
  • Loading branch information
builder@murbella committed Feb 16, 2016
1 parent 503597b commit d8982e9
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 124 deletions.
2 changes: 0 additions & 2 deletions src/PythonQtClassInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@
#include <QMetaObject>
#include <QMetaEnum>

QHash<QByteArray, int> PythonQtMethodInfo::_parameterTypeDict;

PythonQtClassInfo::PythonQtClassInfo() {
_meta = NULL;
_constructors = NULL;
Expand Down
17 changes: 12 additions & 5 deletions src/PythonQtConversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,15 @@ PythonQtValueStorage<qint64, 128> PythonQtConv::global_valueStorage;
PythonQtValueStorage<void*, 128> PythonQtConv::global_ptrStorage;
PythonQtValueStorageWithCleanup<QVariant, 128> PythonQtConv::global_variantStorage;

QHash<int, PythonQtConvertMetaTypeToPythonCB*> PythonQtConv::_metaTypeToPythonConverters;
QHash<int, PythonQtConvertPythonToMetaTypeCB*> PythonQtConv::_pythonToMetaTypeConverters;
QHash<int, PythonQtConvertMetaTypeToPythonCB*>& PythonQtConv::GetMetaTypeToPythonConverters() {
static QHash<int, PythonQtConvertMetaTypeToPythonCB*> metaTypeToPythonConverters;
return metaTypeToPythonConverters;
}

QHash<int, PythonQtConvertPythonToMetaTypeCB*>& PythonQtConv::GetPythonToMetaTypeConverters() {
static QHash<int, PythonQtConvertPythonToMetaTypeCB*> pythonToMetaTypeConverters;
return pythonToMetaTypeConverters;
}

PyObject* PythonQtConv::GetPyBool(bool val)
{
Expand Down Expand Up @@ -103,7 +110,7 @@ PyObject* PythonQtConv::ConvertQtValueToPython(const PythonQtMethodInfo::Paramet

if (info.typeId >= QMetaType::User) {
// if a converter is registered, we use is:
PythonQtConvertMetaTypeToPythonCB* converter = _metaTypeToPythonConverters.value(info.typeId);
PythonQtConvertMetaTypeToPythonCB* converter = GetMetaTypeToPythonConverters().value(info.typeId);
if (converter) {
return (*converter)(info.pointerCount==0?data:*((void**)data), info.typeId);
}
Expand Down Expand Up @@ -668,7 +675,7 @@ void* PythonQtConv::ConvertPythonToQt(const PythonQtMethodInfo::ParameterInfo& i
// We only do this for registered type > QMetaType::User for performance reasons.
if (info.typeId >= QMetaType::User) {
// Maybe we have a special converter that is registered for that type:
PythonQtConvertPythonToMetaTypeCB* converter = _pythonToMetaTypeConverters.value(info.typeId);
PythonQtConvertPythonToMetaTypeCB* converter = GetPythonToMetaTypeConverters().value(info.typeId);
if (converter) {
if (!alreadyAllocatedCPPObject) {
// create a new empty variant of concrete type:
Expand Down Expand Up @@ -1174,7 +1181,7 @@ QVariant PythonQtConv::PyObjToQVariant(PyObject* val, int type)
} else if (type >= QVariant::UserType) {
// not an instance wrapper, but there might be other converters
// Maybe we have a special converter that is registered for that type:
PythonQtConvertPythonToMetaTypeCB* converter = _pythonToMetaTypeConverters.value(type);
PythonQtConvertPythonToMetaTypeCB* converter = GetPythonToMetaTypeConverters().value(type);
if (converter) {
// allocate a default object of the needed type:
v = QVariant(type, (const void*)NULL);
Expand Down
8 changes: 4 additions & 4 deletions src/PythonQtConversion.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,10 @@ class PYTHONQT_EXPORT PythonQtConv {
static QString CPPObjectToString(int type, const void* data);

//! register a converter callback from python to cpp for given metatype
static void registerPythonToMetaTypeConverter(int metaTypeId, PythonQtConvertPythonToMetaTypeCB* cb) { _pythonToMetaTypeConverters.insert(metaTypeId, cb); }
static void registerPythonToMetaTypeConverter(int metaTypeId, PythonQtConvertPythonToMetaTypeCB* cb) { GetPythonToMetaTypeConverters().insert(metaTypeId, cb); }

//! register a converter callback from cpp to python for given metatype
static void registerMetaTypeToPythonConverter(int metaTypeId, PythonQtConvertMetaTypeToPythonCB* cb) { _metaTypeToPythonConverters.insert(metaTypeId, cb); }
static void registerMetaTypeToPythonConverter(int metaTypeId, PythonQtConvertMetaTypeToPythonCB* cb) { GetMetaTypeToPythonConverters().insert(metaTypeId, cb); }

//! converts the Qt parameter given in \c data, interpreting it as a \c type registered qvariant/meta type, into a Python object,
static PyObject* convertQtValueToPythonInternal(int type, const void* data);
Expand All @@ -187,8 +187,8 @@ class PYTHONQT_EXPORT PythonQtConv {
static PythonQtValueStorageWithCleanup<QVariant, 128> global_variantStorage;

protected:
static QHash<int, PythonQtConvertMetaTypeToPythonCB*> _metaTypeToPythonConverters;
static QHash<int, PythonQtConvertPythonToMetaTypeCB*> _pythonToMetaTypeConverters;
static QHash<int, PythonQtConvertMetaTypeToPythonCB*>& GetMetaTypeToPythonConverters();
static QHash<int, PythonQtConvertPythonToMetaTypeCB*>& GetPythonToMetaTypeConverters();

//! handle automatic conversion of some special types (QColor, QBrush, ...)
static void* handlePythonToQtAutoConversion(int typeId, PyObject* obj, void* alreadyAllocatedCPPObject);
Expand Down
Loading

0 comments on commit d8982e9

Please sign in to comment.