goldendict-ng/articleinspector.cc
Igor Kushnir cf84f57632 Don't append duplicates to openedInspectors
There is no benefit in storing the same pointer multiple times in
openedInspectors. This occurred when an article inspector window was
closed then shown again. When the tab corresponding to the duplicated
article inspector pointer was closed, ArticleInspector::beforeClosed()
erased only one pointer from openedInspectors. This left dangling
duplicate pointer(s) in the list and eventually caused a crash when
another inspector's showEvent() accessed a dangling pointer at
openedInspectors.front().
2022-11-02 22:22:16 +03:00

57 lines
1.4 KiB
C++

#include "articleinspector.hh"
#if QT_VERSION >= 0x040600
#include <algorithm>
using std::list;
list< ArticleInspector * > ArticleInspector::openedInspectors;
ArticleInspector::ArticleInspector( Config::Class * cfg, QWidget* parent ) :
QWebInspector( parent ),
cfg( cfg )
{
if ( cfg == NULL )
throw exInit();
}
ArticleInspector::~ArticleInspector()
{
}
void ArticleInspector::beforeClosed()
{
list< ArticleInspector * >::iterator itemIter = std::find( openedInspectors.begin(),
openedInspectors.end(), this );
if ( itemIter != openedInspectors.end() )
{
openedInspectors.erase( itemIter );
// Save geometry of the recent closed inspector window
QByteArray geometry = saveGeometry();
cfg->inspectorGeometry = geometry;
}
}
void ArticleInspector::showEvent( QShowEvent * event )
{
if ( openedInspectors.empty() )
{
// Restore geometry from config, if no inspector opened
restoreGeometry( cfg->inspectorGeometry );
}
else
{
// Load geometry from first inspector opened
ArticleInspector * p = openedInspectors.front();
setGeometry( p->geometry() );
}
if( std::find( openedInspectors.begin(), openedInspectors.end(), this ) == openedInspectors.end() )
openedInspectors.push_back( this );
QWebInspector::showEvent( event );
}
#endif // QT_VERSION