Solved code: 0xc0000005: read access violation at: 0x0, flags=0x0 (first chance)
-
I have a static const instance of QString defined in a class header, the class is called PSUApplication:
static const QString m_userDataRoot; static const QString m_xmlFolder;
In the CPP file:
const QString PSUApplication::m_userDataRoot("./UserData"); const QString PSUApplication::m_xmlFolder(PSUApplication::m_userDataRoot + "xml/");
I have a references in other cpp files that use these statics to initialise other objects, such as QString's to make up references and QDir's.
These all fail at run-time. Is there a way to resolve this?
-
The problem doesn't exist when changing the types to char*.
-
@SPlatten Can you show one of these failing initialisations?
Note: it is unusual to provide direct access to m_* fields as m_ usually is a prefix for private fields.
-
Hi @SPlatten
like @kshegunov told me in my topic a long time ago:
https://forum.qt.io/topic/97838/static-const-qstring-implicit-sharing-issue/8C++ does not guarantee you order of initialization of statics
so m_xmlFolder relies on m_userDataRoot which probably does not exist at that moment int time
-
I'm thinking that now if I was to change these to use basic data types like char[], it wouldn't be a problem. I'll try and report back.
-
The problem doesn't exist when changing the types to char*.
-
@SPlatten said in code: 0xc0000005: read access violation at: 0x0, flags=0x0 (first chance):
The problem doesn't exist when changing the types to char*.
if the 2nd static variable is still depending on the first one, you will eventually run into trouble. Because the initialization order may now fit, but it could brake any moment.
-
I've changed these now, access functions are used to return QString's of the concatenated strings.