Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 16 Apr 2016 02:07:04 +0930
From:      Shane Ambler <FreeBSD@ShaneWare.Biz>
To:        Raphael Kubo da Costa <rakuco@FreeBSD.org>, freebsd-ports@freebsd.org
Subject:   Re: Need some help with c++/qt5 code
Message-ID:  <571118B0.3020806@ShaneWare.Biz>
In-Reply-To: <86inzjdq4w.fsf@FreeBSD.org>
References:  <570F85E3.6060000@ShaneWare.Biz> <CC7B253B-DE3B-4049-96D8-A06DB52A0346@FreeBSD.org> <86inzjdq4w.fsf@FreeBSD.org>

next in thread | previous in thread | raw e-mail | index | archive | help
On 15/04/2016 21:45, Raphael Kubo da Costa wrote:
> Dimitry Andric <dim@FreeBSD.org> writes:
>
>> On 14 Apr 2016, at 13:58, Shane Ambler <FreeBSD@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




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?571118B0.3020806>