Moved initialization of shared memory segment to the main funcion in order to exit gracefully in case of errors.

git-svn-id: svn+ssh://svn.code.sf.net/p/wsjt/wsjt/branches/wsjtx@3163 ab8295b8-cf94-4d9e-aec4-7959e3be5d79
This commit is contained in:
Edson W. R. Pereira 2013-04-13 12:28:03 +00:00
parent 2edf0aebd4
commit ee55040005
3 changed files with 26 additions and 7 deletions

View File

@ -2,6 +2,8 @@
#include <QtGui/QApplication> #include <QtGui/QApplication>
#include "mainwindow.h" #include "mainwindow.h"
QSharedMemory mem_jt9("mem_jt9");
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
QApplication a(argc, argv); QApplication a(argc, argv);
@ -18,7 +20,20 @@ int main(int argc, char *argv[])
a.setFont(font); a.setFont(font);
} }
MainWindow w; // Create and initialize shared memory segment
if(!mem_jt9.attach()) {
if (!mem_jt9.create(sizeof(jt9com_))) {
QMessageBox::critical( 0, "Error", "Unable to create shared memory segment.");
exit(1);
}
}
char *to = (char*)mem_jt9.data();
int size=sizeof(jt9com_);
if(jt9com_.newdat==0) {
}
memset(to,0,size); //Zero all decoding params in shared memory
MainWindow w(&mem_jt9);
w.show(); w.show();
return a.exec(); return a.exec();
} }

View File

@ -21,14 +21,13 @@ double dFreq[]={0.136,0.4742,1.838,3.578,5.357,7.078,10.130,14.078,
18.104,21.078,24.918,28.078,50.293,70.091,144.489,432.178}; 18.104,21.078,24.918,28.078,50.293,70.091,144.489,432.178};
WideGraph* g_pWideGraph = NULL; WideGraph* g_pWideGraph = NULL;
QSharedMemory mem_jt9("mem_jt9");
QString rev="$Rev$"; QString rev="$Rev$";
QString Program_Title_Version=" WSJT-X v0.9, r" + rev.mid(6,4) + QString Program_Title_Version=" WSJT-X v0.9, r" + rev.mid(6,4) +
" by K1JT"; " by K1JT";
//-------------------------------------------------- MainWindow constructor //-------------------------------------------------- MainWindow constructor
MainWindow::MainWindow(QWidget *parent) : MainWindow::MainWindow(QSharedMemory *shdmem, QWidget *parent) :
QMainWindow(parent), QMainWindow(parent),
ui(new Ui::MainWindow) ui(new Ui::MainWindow)
{ {
@ -193,6 +192,8 @@ MainWindow::MainWindow(QWidget *parent) :
QString::number(iret)); QString::number(iret));
} }
#endif #endif
mem_jt9 = shdmem;
/*
if(!mem_jt9.attach()) { if(!mem_jt9.attach()) {
if (!mem_jt9.create(sizeof(jt9com_))) { if (!mem_jt9.create(sizeof(jt9com_))) {
msgBox("Unable to create shared memory segment."); msgBox("Unable to create shared memory segment.");
@ -203,6 +204,7 @@ MainWindow::MainWindow(QWidget *parent) :
if(jt9com_.newdat==0) { if(jt9com_.newdat==0) {
} }
memset(to,0,size); //Zero all decoding params in shared memory memset(to,0,size); //Zero all decoding params in shared memory
*/
PaError paerr=Pa_Initialize(); //Initialize Portaudio PaError paerr=Pa_Initialize(); //Initialize Portaudio
if(paerr!=paNoError) { if(paerr!=paNoError) {
@ -895,7 +897,7 @@ void MainWindow::OnExit()
{ {
g_pWideGraph->saveSettings(); g_pWideGraph->saveSettings();
m_killAll=true; m_killAll=true;
mem_jt9.detach(); mem_jt9->detach();
QFile quitFile(m_appDir + "/.quit"); QFile quitFile(m_appDir + "/.quit");
quitFile.open(QIODevice::ReadWrite); quitFile.open(QIODevice::ReadWrite);
QFile lockFile(m_appDir + "/.lock"); QFile lockFile(m_appDir + "/.lock");
@ -1154,7 +1156,7 @@ void MainWindow::decode() //decode()
//newdat=1 ==> this is new data, must do the big FFT //newdat=1 ==> this is new data, must do the big FFT
//nagain=1 ==> decode only at fQSO +/- Tol //nagain=1 ==> decode only at fQSO +/- Tol
char *to = (char*)mem_jt9.data(); char *to = (char*)mem_jt9->data();
char *from = (char*) jt9com_.ss; char *from = (char*) jt9com_.ss;
int size=sizeof(jt9com_); int size=sizeof(jt9com_);
if(jt9com_.newdat==0) { if(jt9com_.newdat==0) {
@ -1163,7 +1165,7 @@ void MainWindow::decode() //decode()
from += noffset; from += noffset;
size -= noffset; size -= noffset;
} }
memcpy(to, from, qMin(mem_jt9.size(), size)); memcpy(to, from, qMin(mem_jt9->size(), size));
QFile lockFile(m_appDir + "/.lock"); // Allow jt9 to start QFile lockFile(m_appDir + "/.lock"); // Allow jt9 to start
lockFile.remove(); lockFile.remove();

View File

@ -21,7 +21,7 @@ class MainWindow : public QMainWindow
Q_OBJECT Q_OBJECT
public: public:
explicit MainWindow(QWidget *parent = 0); explicit MainWindow(QSharedMemory *shdmem, QWidget *parent = 0);
~MainWindow(); ~MainWindow();
public slots: public slots:
@ -309,6 +309,8 @@ private:
SoundInThread soundInThread; //Instantiate the audio threads SoundInThread soundInThread; //Instantiate the audio threads
SoundOutThread soundOutThread; SoundOutThread soundOutThread;
QSharedMemory *mem_jt9;
//---------------------------------------------------- private functions //---------------------------------------------------- private functions
void readSettings(); void readSettings();
void writeSettings(); void writeSettings();