mirror of
https://github.com/tomahawk-player/tomahawk.git
synced 2025-07-31 11:20:22 +02:00
don't assume that exit() will clean up the stack
This commit is contained in:
@@ -11,8 +11,8 @@
|
||||
|
||||
|
||||
KDSingleApplicationGuard::Instance::Instance( const QStringList& args, qint64 p )
|
||||
: arguments( args ),
|
||||
pid( p )
|
||||
: arguments( args ),
|
||||
pid( p )
|
||||
{
|
||||
}
|
||||
|
||||
@@ -61,31 +61,31 @@ void KDSingleApplicationGuard::timerEvent( QTimerEvent* )
|
||||
using namespace kdtools;
|
||||
|
||||
/*!
|
||||
\class KDSingleApplicationGuard KDSingleApplicationGuard
|
||||
\brief A guard to protect an application from having several instances.
|
||||
|
||||
KDSingleApplicationGuard can be used to make sure only one instance of an
|
||||
application is running at the same time.
|
||||
|
||||
\note As KDSingleApplicationGuard uses QSharedMemory Qt 4.4 or later is required
|
||||
* \class KDSingleApplicationGuard KDSingleApplicationGuard
|
||||
* \brief A guard to protect an application from having several instances.
|
||||
*
|
||||
* KDSingleApplicationGuard can be used to make sure only one instance of an
|
||||
* application is running at the same time.
|
||||
*
|
||||
* \note As KDSingleApplicationGuard uses QSharedMemory Qt 4.4 or later is required
|
||||
*/
|
||||
|
||||
/*!
|
||||
\fn void KDSingleApplicationGuard::instanceStarted()
|
||||
This signal is emitted by the primary instance when ever one other
|
||||
instance was started.
|
||||
* \fn void KDSingleApplicationGuard::instanceStarted()
|
||||
* This signal is emitted by the primary instance when ever one other
|
||||
* instance was started.
|
||||
*/
|
||||
|
||||
/*!
|
||||
\fn void KDSingleApplicationGuard::instanceExited()
|
||||
This signal is emitted by the primary instance when ever one other
|
||||
instance was exited.
|
||||
* \fn void KDSingleApplicationGuard::instanceExited()
|
||||
* This signal is emitted by the primary instance when ever one other
|
||||
* instance was exited.
|
||||
*/
|
||||
|
||||
/*!
|
||||
\fn void KDSingleApplicationGuard::becamePrimaryInstance()
|
||||
This signal is emitted, when the current running application gets the new
|
||||
primary application. The old primary application has quit.
|
||||
* \fn void KDSingleApplicationGuard::becamePrimaryInstance()
|
||||
* This signal is emitted, when the current running application gets the new
|
||||
* primary application. The old primary application has quit.
|
||||
*/
|
||||
|
||||
enum Command
|
||||
@@ -158,8 +158,8 @@ bool operator!=( const ProcessInfo& lhs, const ProcessInfo& rhs )
|
||||
}
|
||||
|
||||
/*!
|
||||
This struct contains information about the managed process system.
|
||||
\internal
|
||||
* This struct contains information about the managed process system.
|
||||
* \internal
|
||||
*/
|
||||
struct InstanceRegister
|
||||
{
|
||||
@@ -171,7 +171,7 @@ struct InstanceRegister
|
||||
}
|
||||
|
||||
/*!
|
||||
Returns wheter this register was properly initialized by the first instance.
|
||||
* Returns wheter this register was properly initialized by the first instance.
|
||||
*/
|
||||
bool isValid() const
|
||||
{
|
||||
@@ -196,7 +196,7 @@ bool operator==( const InstanceRegister& lhs, const InstanceRegister& rhs )
|
||||
}
|
||||
|
||||
/*!
|
||||
\internal
|
||||
* \internal
|
||||
*/
|
||||
class KDSingleApplicationGuard::Private
|
||||
{
|
||||
@@ -258,14 +258,14 @@ void SIGINT_handler( int sig )
|
||||
#endif
|
||||
|
||||
/*!
|
||||
Creates a new KDSingleApplicationGuard guarding \a parent from mulitply instances.
|
||||
If \a policy is AutoKillOtherInstances (the default), all instances, which try to start,
|
||||
are killed automatically and instanceStarted() is emitted.
|
||||
If \a policy is NoPolicy, the other instance will run and instanceStarted() is emitted.
|
||||
* Creates a new KDSingleApplicationGuard guarding \a parent from mulitply instances.
|
||||
* If \a policy is AutoKillOtherInstances (the default), all instances, which try to start,
|
||||
* are killed automatically and instanceStarted() is emitted.
|
||||
* If \a policy is NoPolicy, the other instance will run and instanceStarted() is emitted.
|
||||
*/
|
||||
KDSingleApplicationGuard::KDSingleApplicationGuard( QCoreApplication* parent, Policy policy )
|
||||
: QObject( parent ),
|
||||
d( new Private( this ) )
|
||||
: QObject( parent ),
|
||||
d( new Private( this ) )
|
||||
{
|
||||
const QString name = parent->applicationName();
|
||||
Q_ASSERT_X( !name.isEmpty(), "KDSingleApplicationGuard::KDSingleApplicationGuard", "applicationName must not be emty" );
|
||||
@@ -273,10 +273,10 @@ KDSingleApplicationGuard::KDSingleApplicationGuard( QCoreApplication* parent, Po
|
||||
|
||||
// if another instance crashed, the shared memory segment is still there on Unix
|
||||
// the following lines trigger deletion in that case
|
||||
#ifndef Q_WS_WIN
|
||||
#ifndef Q_WS_WIN
|
||||
d->mem.attach();
|
||||
d->mem.detach();
|
||||
#endif
|
||||
#endif
|
||||
|
||||
d->policy = policy;
|
||||
|
||||
@@ -298,7 +298,8 @@ KDSingleApplicationGuard::KDSingleApplicationGuard( QCoreApplication* parent, Po
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool killMyself = false;
|
||||
{
|
||||
KDLockedSharedMemoryPointer< InstanceRegister > instances( &d->mem );
|
||||
|
||||
if( !created )
|
||||
@@ -318,7 +319,7 @@ KDSingleApplicationGuard::KDSingleApplicationGuard( QCoreApplication* parent, Po
|
||||
if( killOurSelf )
|
||||
{
|
||||
info.command |= ExitedInstance;
|
||||
exit( 1 );
|
||||
killMyself = true;
|
||||
}
|
||||
}
|
||||
else
|
||||
@@ -330,20 +331,24 @@ KDSingleApplicationGuard::KDSingleApplicationGuard( QCoreApplication* parent, Po
|
||||
reg.info[ 0 ] = ProcessInfo( NoCommand, parent->arguments(), QCoreApplication::applicationPid() );
|
||||
*instances = reg; // push this is the process list into shared memory
|
||||
}
|
||||
}
|
||||
// call exit after we let the locker release our memory, as exit() is not guaranteed to clean up objects on the stack
|
||||
if ( killMyself )
|
||||
exit( 1 );
|
||||
|
||||
#ifndef Q_WS_WIN
|
||||
#ifndef Q_WS_WIN
|
||||
::signal( SIGINT, SIGINT_handler );
|
||||
#endif
|
||||
#endif
|
||||
|
||||
// now listen for commands
|
||||
startTimer( 250 );
|
||||
}
|
||||
|
||||
/*!
|
||||
Destroys this SingleApplicationGuard.
|
||||
If this instance has been the primary instance and no other instance is existing anymore,
|
||||
the application is shut down completely. Otherwise the destructor selects another instance to
|
||||
be the primary instances.
|
||||
* Destroys this SingleApplicationGuard.
|
||||
* If this instance has been the primary instance and no other instance is existing anymore,
|
||||
* the application is shut down completely. Otherwise the destructor selects another instance to
|
||||
* be the primary instances.
|
||||
*/
|
||||
KDSingleApplicationGuard::~KDSingleApplicationGuard()
|
||||
{
|
||||
@@ -354,14 +359,14 @@ KDSingleApplicationGuard::~KDSingleApplicationGuard()
|
||||
}
|
||||
|
||||
/*!
|
||||
\property KDSingleApplicationGuard::primaryInstance
|
||||
Determines wheter this instance is the primary instance.
|
||||
The primary instance is the first instance which was started or an instance which
|
||||
got selected by KDSingleApplicationGuard's destructor, when the primary instance was
|
||||
shut down.
|
||||
|
||||
Get this property's value using %isPrimaryInstance(), and monitor changes to it
|
||||
using becamePrimaryInstance().
|
||||
* \property KDSingleApplicationGuard::primaryInstance
|
||||
* Determines wheter this instance is the primary instance.
|
||||
* The primary instance is the first instance which was started or an instance which
|
||||
* got selected by KDSingleApplicationGuard's destructor, when the primary instance was
|
||||
* shut down.
|
||||
*
|
||||
* Get this property's value using %isPrimaryInstance(), and monitor changes to it
|
||||
* using becamePrimaryInstance().
|
||||
*/
|
||||
bool KDSingleApplicationGuard::isPrimaryInstance() const
|
||||
{
|
||||
@@ -369,12 +374,12 @@ bool KDSingleApplicationGuard::isPrimaryInstance() const
|
||||
}
|
||||
|
||||
/*!
|
||||
\property KDSingleApplicationGuard::Policy
|
||||
Specifies the policy KDSingleApplicationGuard is using when new instances are started.
|
||||
This can only be set in the primary instance.
|
||||
|
||||
Get this property's value using %policy(), set it using %setPolicy(), and monitor changes
|
||||
to it using policyChanged().
|
||||
* \property KDSingleApplicationGuard::Policy
|
||||
* Specifies the policy KDSingleApplicationGuard is using when new instances are started.
|
||||
* This can only be set in the primary instance.
|
||||
*
|
||||
* Get this property's value using %policy(), set it using %setPolicy(), and monitor changes
|
||||
* to it using policyChanged().
|
||||
*/
|
||||
KDSingleApplicationGuard::Policy KDSingleApplicationGuard::policy() const
|
||||
{
|
||||
@@ -394,7 +399,7 @@ void KDSingleApplicationGuard::setPolicy( Policy policy )
|
||||
}
|
||||
|
||||
/*!
|
||||
Returns a list of all currently running instances.
|
||||
* Returns a list of all currently running instances.
|
||||
*/
|
||||
QList< KDSingleApplicationGuard::Instance > KDSingleApplicationGuard::instances() const
|
||||
{
|
||||
@@ -410,9 +415,9 @@ QList< KDSingleApplicationGuard::Instance > KDSingleApplicationGuard::instances(
|
||||
}
|
||||
|
||||
/*!
|
||||
Shuts down all other instances. This can only be called from the
|
||||
the primary instance.
|
||||
Shut down is done gracefully via QCoreApplication::quit().
|
||||
* Shuts down all other instances. This can only be called from the
|
||||
* the primary instance.
|
||||
* Shut down is done gracefully via QCoreApplication::quit().
|
||||
*/
|
||||
void KDSingleApplicationGuard::shutdownOtherInstances()
|
||||
{
|
||||
@@ -426,9 +431,9 @@ void KDSingleApplicationGuard::shutdownOtherInstances()
|
||||
}
|
||||
|
||||
/*!
|
||||
Kills all other instances. This can only be called from the
|
||||
the primary instance.
|
||||
Killing is done via exit(1)
|
||||
* Kills all other instances. This can only be called from the
|
||||
* the primary instance.
|
||||
* Killing is done via exit(1)
|
||||
*/
|
||||
void KDSingleApplicationGuard::killOtherInstances()
|
||||
{
|
||||
@@ -442,7 +447,7 @@ void KDSingleApplicationGuard::killOtherInstances()
|
||||
}
|
||||
|
||||
/*!
|
||||
\reimp
|
||||
* \reimp
|
||||
*/
|
||||
void KDSingleApplicationGuard::timerEvent( QTimerEvent* event )
|
||||
{
|
||||
@@ -615,7 +620,7 @@ KDAB_UNITTEST_SIMPLE( KDSingleApplicationGuard, "kdcoretools" ) {
|
||||
|
||||
delete spy3;
|
||||
delete guard3;
|
||||
}
|
||||
}
|
||||
|
||||
#endif // KDTOOLSCORE_UNITTESTS
|
||||
|
||||
|
Reference in New Issue
Block a user