Strange behaviour with QString class members
-
@Mesrine said in Strange behaviour with QString class members:
But I do not get the reason.
possibly the missing ,
AddressBox::AddressBox(
const QString hrp, QObject *parent):QObject(parent)
, m_bech32adddress("WHATISWRONG") -
@J-Hilk Now I do not get what you are trying to say :) .
I have declared a member function on the definition of the class that just returns the QString member.
The initialization of the const QString member is in the constructor.
Even if I do not initialize the QString member the function should return a default QString object. -
@Mesrine I agree that the code by itself shouldn't crash (though the nested qDebug() is suspicious, I still think it's behavior should be well defined).
Anyhow, the code snippets you're showing is also not valid C++ (it misses at least a ';'). Try to create a minimal example that actually compiles. Also, do you get any stack traces?
Kai
-
@kkoehne
Thanks, the code was just to get an idea. If I create a minimum reproducible example it works ok :).The runtime error was happening in a bigger project and it was fixed by passing the function definition from the headers to the sources.
I published it here although it can not be reproducible just in case I was missing something or someone experienced before.
-
If i do instead
class.cpp
#include"class.hpp" AddressBox::AddressBox( const QString hrp, QObject *parent):QObject(parent) m_bech32adddress("WHATISWRONG") { qDebug()<<"hello0:"<<m_bech32adddress.size(); qDebug()<<"hello1:"<<getAddressBech32().size(); qDebug()<<"hello2:"<<m_bech32adddress; qDebug()<<"hello3:"<<getAddressBech32(); };
the output is
hello0: 11 hello1: 94248499095840 hello2: "WHATISWRONG" Segmentation fault (core dumped)
-
@Mesrine said in Strange behaviour with QString class members:
AddressBox
since file and class name differ so much, do you have multiple classes in those files ?
-
@Mesrine said in Strange behaviour with QString class members:
Segmentation fault (core dumped)
Then run it in a debugger and inspect the stack trace (maybe post it here).
My feeling is, that you have some old object files that for some reason get linked but are incompatible to the rest of your code.
I'd clean all build artifacts and see if it reproduces after a clean build.
Regards.
-
Couldn't resist compiling the reproducer against the latest dev, as well as 6.6 and 6.5.
Works and doesn't crash. So I guess @aha_1980 is right about binary artifacts causing the issue.
Moving the member function from header to implementation, probably caused no more than a re-compile. -
Yeah, but if I move it back it gives the runtime error :) .
I will try some stack trace and posted here.
Also, I will try changing the name of the function as suggested by the remaining binary artifacts idea.