1
0
mirror of https://github.com/cookiengineer/audacity synced 2025-07-19 22:27:43 +02:00

Revert r13868 and fix access violation on Windows

This puts the single instance checker back to pre-13868
behavior, so we're back to being able to open multiple
instance if the temp directory is different in portable
settings.

The access violation has apparently been happening for
quite a while, just hidden because it happened when
additional Audacity instances were executed and the DDE
command was sent to the first instance.  After sending
the command, the connection was disconnected, but the
object had already been deleted by the command execution
so a first-chance exception was triggered.
This commit is contained in:
lllucius 2015-01-21 07:52:15 +00:00
parent 09c213feed
commit 361d3add9b
2 changed files with 183 additions and 185 deletions

@ -565,6 +565,10 @@ GnomeShutdown GnomeShutdownInstance;
#endif #endif
// Where drag/drop or "Open With" filenames get stored until
// the timer routine gets around to picking them up.
static wxArrayString ofqueue;
// //
// DDE support for opening multiple files with one instance // DDE support for opening multiple files with one instance
// of Audacity. // of Audacity.
@ -588,23 +592,9 @@ public:
bool OnExec(const wxString & WXUNUSED(topic), bool OnExec(const wxString & WXUNUSED(topic),
const wxString & data) const wxString & data)
{ {
if (!gInited) { // Add the filename to the queue. It will be opened by
return false; // the OnTimer() event when it is safe to do so.
} ofqueue.Add(data);
AudacityProject *project = CreateNewAudacityProject();
// We queue a command event to the project responsible for
// opening the file since it can be a long process and we
// only have 5 seconds to return the Execute message to the
// client.
if (!data.IsEmpty()) {
wxCommandEvent e(EVT_OPEN_AUDIO_FILE);
e.SetString(data);
project->GetEventHandler()->AddPendingEvent(e);
}
delete this;
return true; return true;
} }
@ -618,11 +608,6 @@ public:
return OnExec(topic, data); return OnExec(topic, data);
} }
#endif #endif
virtual bool OnDisconnect()
{
return true;
}
}; };
class IPCServ : public wxServer class IPCServ : public wxServer
@ -680,10 +665,6 @@ int main(int argc, char *argv[])
} }
#endif #endif
// Where drag/drop or "Open With" filenames get stored until
// the timer routine gets around to picking them up.
static wxArrayString ofqueue;
#ifdef __WXMAC__ #ifdef __WXMAC__
// in response of an open-document apple event // in response of an open-document apple event
@ -845,8 +826,12 @@ void AudacityApp::OnTimer(wxTimerEvent& WXUNUSED(event))
// Get the user's attention if no file name was specified // Get the user's attention if no file name was specified
if (name.IsEmpty()) { if (name.IsEmpty()) {
// Get the users attention // Get the users attention
GetActiveProject()->Raise(); AudacityProject *project = GetActiveProject();
GetActiveProject()->RequestUserAttention(); if (project) {
project->Maximize();
project->Raise();
project->RequestUserAttention();
}
continue; continue;
} }
@ -1084,15 +1069,6 @@ bool AudacityApp::OnInit()
mLocale = NULL; mLocale = NULL;
InitLang(GetSystemLanguageCode()); InitLang(GetSystemLanguageCode());
// Check for another running instance. This must be done before
// any activities that may modify the same resources of the other
// instance, like initializing preferences.
if (!CreateSingleInstanceChecker()) {
return false;
}
// Now we know we're the only instance running, so we're safe to
// initialize preferences
InitPreferences(); InitPreferences();
#if defined(__WXMSW__) && !defined(__WXUNIVERSAL__) && !defined(__CYGWIN__) #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__) && !defined(__CYGWIN__)
@ -1449,44 +1425,52 @@ bool AudacityApp::InitTempDir()
{ {
// We need to find a temp directory location. // We need to find a temp directory location.
wxFileName temp; wxString tempFromPrefs = gPrefs->Read(wxT("/Directories/TempDir"), wxT(""));
wxArrayString paths; wxString tempDefaultLoc = wxGetApp().defaultTempDir;
paths.Add(gPrefs->Read(wxT("/Directories/TempDir"), wxEmptyString));
paths.Add(defaultTempDir);
for (size_t i = 0, cnt = paths.GetCount(); i < cnt; i++) wxString temp = wxT("");
{
temp.SetPath(paths[i]);
temp.AppendDir(wxGetUserId() + wxT("-temp-dir"));
if (temp.IsOk() && temp.IsAbsolute()) #ifdef __WXGTK__
{ if (tempFromPrefs.Length() > 0 && tempFromPrefs[0] != wxT('/'))
if (temp.DirExists() || temp.Mkdir(0755, wxPATH_MKDIR_FULL)) tempFromPrefs = wxT("");
{
#ifdef __UNIX__
// Check temp directory ownership on *nix systems only
wxStructStat stats;
if (wxLstat(temp.GetFullPath(), &stats) != 0 || stats.st_uid != geteuid())
{
temp.Clear();
continue;
}
// The permissions don't always seem to be set on
// some platforms. Hopefully this fixes it...
chmod(OSFILENAME(temp.GetFullPath()), 0755);
#endif #endif
gPrefs->Write(wxT("/Directories/TempDir"), paths[i]) && gPrefs->Flush(); // Stop wxWidgets from printing its own error messages
DirManager::SetTempDir(temp.GetFullPath());
break; wxLogNull logNo;
}
} // Try temp dir that was stored in prefs first
temp.Clear();
if (tempFromPrefs != wxT("")) {
if (wxDirExists(tempFromPrefs))
temp = tempFromPrefs;
else if (wxMkdir(tempFromPrefs, 0755))
temp = tempFromPrefs;
} }
if (!temp.IsOk()) // If that didn't work, try the default location
{
if (temp==wxT("") && tempDefaultLoc != wxT("")) {
if (wxDirExists(tempDefaultLoc))
temp = tempDefaultLoc;
else if (wxMkdir(tempDefaultLoc, 0755))
temp = tempDefaultLoc;
}
// Check temp directory ownership on *nix systems only
#ifdef __UNIX__
struct stat tempStatBuf;
if ( lstat(temp.mb_str(), &tempStatBuf) != 0 ) {
temp.clear();
}
else {
if ( geteuid() != tempStatBuf.st_uid ) {
temp.clear();
}
}
#endif
if (temp == wxT("")) {
// Failed // Failed
wxMessageBox(_("Audacity could not find a place to store temporary files.\nPlease enter an appropriate directory in the preferences dialog.")); wxMessageBox(_("Audacity could not find a place to store temporary files.\nPlease enter an appropriate directory in the preferences dialog."));
@ -1498,70 +1482,56 @@ bool AudacityApp::InitTempDir()
return false; return false;
} }
return true; // The permissions don't always seem to be set on
// some platforms. Hopefully this fixes it...
#ifdef __UNIX__
chmod(OSFILENAME(temp), 0755);
#endif
bool bSuccess = gPrefs->Write(wxT("/Directories/TempDir"), temp) && gPrefs->Flush();
DirManager::SetTempDir(temp);
// Make sure the temp dir isn't locked by another process.
if (!CreateSingleInstanceChecker(temp))
return false;
return bSuccess;
} }
// Return true if there are no other instances of Audacity running, // Return true if there are no other instances of Audacity running,
// false otherwise. // false otherwise.
bool AudacityApp::CreateSingleInstanceChecker() //
// Use "dir" for creating lockfiles (on OS X and Unix).
bool AudacityApp::CreateSingleInstanceChecker(wxString dir)
{ {
wxString name = wxString(wxT(".")) + IPC_APPL; wxString name = wxString::Format(wxT("audacity-lock-%s"), wxGetUserId().c_str());
mChecker = new wxSingleInstanceChecker();
#if defined(__UNIX__)
wxString sockFile(wxGetHomeDir() + wxT("/") + name + wxT(".sock"));
#endif
wxString runningTwoCopiesStr = _("Running two copies of Audacity simultaneously may cause\ndata loss or cause your system to crash.\n\n"); wxString runningTwoCopiesStr = _("Running two copies of Audacity simultaneously may cause\ndata loss or cause your system to crash.\n\n");
bool success;
mChecker = new wxSingleInstanceChecker(); if (!mChecker->Create(name, dir)) {
success = mChecker->Create(name + wxT(".lock"), wxGetHomeDir());
if (!success)
{
// Error initializing the wxSingleInstanceChecker. We don't know // Error initializing the wxSingleInstanceChecker. We don't know
// whether there is another instance running or not. // whether there is another instance running or not.
wxString prompt = wxString prompt =
_("Audacity was not able to obtain lock the temporary files directory.\nThis folder may be in use by another copy of Audacity.\n") + _("Audacity was not able to lock the temporary files directory.\nThis folder may be in use by another copy of Audacity.\n") +
runningTwoCopiesStr + runningTwoCopiesStr +
_("Do you still want to start Audacity?"); _("Do you still want to start Audacity?");
int action = wxMessageBox(prompt, int action = wxMessageBox(prompt,
_("Error Locking Temporary Folder"), _("Error Locking Temporary Folder"),
wxYES_NO | wxICON_EXCLAMATION, wxYES_NO | wxICON_EXCLAMATION,
NULL); NULL);
if (action == wxNO) if (action == wxNO) {
{ delete mChecker;
return false; return false;
} }
} }
else if ( mChecker->IsAnotherRunning() ) {
#if defined(__UNIX__)
wxString sockFile(wxGetHomeDir() + wxT("/") + name + wxT(".sock"));
#endif
// Is this process the first one?
if (!mChecker->IsAnotherRunning())
{
#if defined(__WXMSW__)
// Create the DDE IPC server
mIPCServ = new IPCServ(IPC_APPL);
#else
int mask = umask(077);
remove(OSFILENAME(sockFile));
wxUNIXaddress addr;
addr.Filename(sockFile);
mIPCServ = new wxSocketServer(addr, wxSOCKET_NOWAIT);
umask(mask);
if (!mIPCServ || !mIPCServ->IsOk())
{
// TODO: Complain here
return false;
}
mIPCServ->SetEventHandler(*this, ID_IPC_SERVER);
mIPCServ->SetNotify(wxSOCKET_CONNECTION_FLAG);
mIPCServ->Notify(true);
#endif
return true;
}
// Parse the command line to ensure correct syntax, but // Parse the command line to ensure correct syntax, but
// ignore options and only use the filenames, if any. // ignore options and only use the filenames, if any.
wxCmdLineParser *parser = ParseCommandLine(); wxCmdLineParser *parser = ParseCommandLine();
@ -1572,37 +1542,44 @@ bool AudacityApp::CreateSingleInstanceChecker()
} }
#if defined(__WXMSW__) #if defined(__WXMSW__)
// On Windows, we attempt to make a DDE connection // On Windows, we attempt to make a connection
// to an already active Audacity. If successful, we send // to an already active Audacity. If successful, we send
// the first command line argument (the audio file name) // the first command line argument (the audio file name)
// to that Audacity for processing. // to that Audacity for processing.
wxClient client; wxClient client;
wxConnectionBase *conn;
// We try up to 50 times since there's a small window // We try up to 50 times since there's a small window
// where the server may not have been fully initialized. // where the server may not have been fully initialized.
for (int i = 0; i < 50; i++) for (int i = 0; i < 50; i++)
{ {
conn = client.MakeConnection(wxEmptyString, IPC_APPL, IPC_TOPIC); wxConnectionBase *conn = client.MakeConnection(wxEmptyString, IPC_APPL, IPC_TOPIC);
if (conn) if (conn)
{ {
bool ok = true; bool ok;
for (size_t i = 0, cnt = parser->GetParamCount(); i < cnt && ok; i++) if (parser->GetParamCount() > 0)
{
// Send each parameter to existing Audacity
for (size_t i = 0, cnt = parser->GetParamCount(); i < cnt; i++)
{ {
ok = conn->Execute(parser->GetParam(i)); ok = conn->Execute(parser->GetParam(i));
} }
}
else
{
// Send an empty string to force existing Audacity to front
ok = conn->Execute(wxEmptyString);
}
conn->Disconnect();
delete conn; delete conn;
if (ok) if (ok)
{ {
// Command was successfully queued so exit quietly
delete parser; delete parser;
return false; return false;
} }
} }
wxMilliSleep(100);
wxMilliSleep(10);
} }
#else #else
// On Unix-like machines, we use a local (file based) socket to // On Unix-like machines, we use a local (file based) socket to
@ -1640,12 +1617,7 @@ bool AudacityApp::CreateSingleInstanceChecker()
sock->Destroy(); sock->Destroy();
#endif #endif
// There is another copy of Audacity running. Force quit.
delete parser;
// There is another copy of Audacity running and we weren't able to
// communicate to it...force quit. We should never really get to this point
// but let the user know just in case.
wxString prompt = wxString prompt =
_("The system has detected that another copy of Audacity is running.\n") + _("The system has detected that another copy of Audacity is running.\n") +
@ -1653,9 +1625,35 @@ bool AudacityApp::CreateSingleInstanceChecker()
_("Use the New or Open commands in the currently running Audacity\nprocess to open multiple projects simultaneously.\n"); _("Use the New or Open commands in the currently running Audacity\nprocess to open multiple projects simultaneously.\n");
wxMessageBox(prompt, _("Audacity is already running"), wxMessageBox(prompt, _("Audacity is already running"),
wxOK | wxICON_ERROR); wxOK | wxICON_ERROR);
delete parser;
delete mChecker;
return false; return false;
} }
#if defined(__WXMSW__)
// Create the DDE IPC server
mIPCServ = new IPCServ(IPC_APPL);
#else
int mask = umask(077);
remove(OSFILENAME(sockFile));
wxUNIXaddress addr;
addr.Filename(sockFile);
mIPCServ = new wxSocketServer(addr, wxSOCKET_NOWAIT);
umask(mask);
if (!mIPCServ || !mIPCServ->IsOk())
{
// TODO: Complain here
return false;
}
mIPCServ->SetEventHandler(*this, ID_IPC_SERVER);
mIPCServ->SetNotify(wxSOCKET_CONNECTION_FLAG);
mIPCServ->Notify(true);
#endif
return true;
}
#if defined(__UNIX__) #if defined(__UNIX__)
void AudacityApp::OnServerEvent(wxSocketEvent & evt) void AudacityApp::OnServerEvent(wxSocketEvent & evt)
{ {

@ -226,7 +226,7 @@ class AudacityApp:public wxApp {
void DeInitCommandHandler(); void DeInitCommandHandler();
bool InitTempDir(); bool InitTempDir();
bool CreateSingleInstanceChecker(); bool CreateSingleInstanceChecker(wxString dir);
wxCmdLineParser *ParseCommandLine(); wxCmdLineParser *ParseCommandLine();