Need some help with c++/qt5 code
Shane Ambler
FreeBSD at ShaneWare.Biz
Fri Apr 15 16:37:10 UTC 2016
On 15/04/2016 21:45, Raphael Kubo da Costa wrote:
> Dimitry Andric <dim at FreeBSD.org> writes:
>
>> On 14 Apr 2016, at 13:58, Shane Ambler <FreeBSD at ShaneWare.Biz> wrote:
>>>
>>> Hi there, while I am comfortable with c and python, I only know a little
>>> c++ and could use some help.
>> ...
>>> class TPanelFactory
>>> {
>>> QString m_panelType;
>>> static QMap<QString, TPanelFactory *> m_table;
>>>
>>> public:
>>> TPanelFactory(QString panelType);
>>> ~TPanelFactory();
>>>
>>> QString getPanelType() const { return m_panelType; }
>>>
>>> virtual void initialize(TPanel *panel) = 0;
>>> virtual TPanel *createPanel(QWidget *parent);
>>> static TPanel *createPanel(QWidget *parent, QString panelType);
>>> };
>>>
>>> m_table is then in the source file as
>>>
>>> QMap<QString, TPanelFactory *> TPanelFactory::m_table;
>>>
>>> The segfault happens in the constructor -
>>>
>>> TPanelFactory::TPanelFactory(QString panelType)
>>> : m_panelType(panelType)
>>> {
>>> assert(m_table.count(panelType) == 0);
>>> m_table[m_panelType] = this;
>>> }
>>>
>>> the last line causes the segfault, if I comment it out then main() is
>>> entered, but I expect removing that line will bite back soon enough.
>>>
>>> How can I get this working?
>>>
>>> Why would this fail on FreeBSD but not OSX or windows?
>>
>> Most likely the program depends on the initialization order of global
>> constructors. This is bad practice, and should be avoided.
>
> I agree. Maybe using Q_GLOBAL_STATIC helps?
>
> - Remove m_table from TPanelFactory.
> - In pane.cpp, you do something like this:
>
> typedef QMap<QString, TPanelFactory *> PanelMapType;
> Q_GLOBAL_STATIC(PanelMapType, s_panelMap);
>
> you then need to replace uses of m_table with s_panelMap and use
> s_panelMap->operation() instead of m_table.operation().
Thanks that does the trick.
--
FreeBSD - the place to B...Software Developing
Shane Ambler
More information about the freebsd-ports
mailing list