Error: terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc The program has unexpectedly finished.[SOLVED]
-
I have a program that generates a sine wave with click of pushButton and but has a never ending loop. Im using threads to eliminate this loop with click of a second pushButton2.
No compiling errors.
Im getting the following error when running:
@terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
The program has unexpectedly finished.@Code is below. It is different from my last thread:
wave.cpp:@void wave::on_write_loop(snd_pcm_t *handles, signed short *samples , snd_pcm_channel_area_t *areas)
{
double freq = ui->frequency->text().toDouble();
double ampl = ui->amplitude->text().toDouble();
double phase = 0;
signed short ptr;
int err, cptr;
while (1) {
generate_sine(0, period_size, &phase, freq, ampl);
ptr = samples;
cptr = period_size;
while (cptr > 0) {
err = snd_pcm_writei(hspdif, ptr, cptr);
if (err == -EAGAIN)
continue;
if (err < 0) {
if (xrun_recovery(hspdif, err) < 0) {
printf("Write error: %s ", snd_strerror(err));
exit(EXIT_FAILURE);
}
break; / skip one period */
}
ptr += err * channels;
cptr -= err;
}
}}
void wave::setup() {
const char *device = "plughw:0,0";snd_pcm_hw_params_alloca(&hwparams); snd_pcm_sw_params_alloca(&swparams); int err = snd_output_stdio_attach(&output, stdout, 0); printf( "snd_output_stdio_attach err=%d\n", err); err = snd_pcm_open(&hspdif, device, SND_PCM_STREAM_PLAYBACK, 0); printf( "snd_pcm_open err=%d\n", err); err = set_hwparams(hspdif, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED); printf( "set_hwparams err=%d\n", err); err = set_swparams(hspdif, swparams); printf( "set_swparams err=%d\n", err); samples = new signed short [period_size * channels * snd_pcm_format_physical_width(format)]; printf( "samples array_size=%d\n", int( period_size * channels * snd_pcm_format_physical_width(format)) ); areas = new snd_pcm_channel_area_t [channels]; printf( "areas channels=%d\n", channels); for (unsigned int chn = 0; chn < channels; chn++) { areas[chn].addr = samples; areas[chn].first = chn * snd_pcm_format_physical_width(format); areas[chn].step = channels * snd_pcm_format_physical_width(format); }
}
wave::wave(QWidget parent) :
QMainWindow(parent),
ui(new Ui::wave)
{
ui->setupUi(this);
setup();
thread = new QThread();
thread->start();
wave Wave = new wave();
Wave->moveToThread(thread);
connect(this, SIGNAL(startWave(snd_pcm_t, signed short, snd_pcm_channel_area_t*)), Wave, SLOT(on_write_loop(snd_pcm_t*, signed short*, snd_pcm_channel_area_t*)));}
wave::~wave()
{
delete ui;}
void wave::on_pushButton_clicked()
{
//Generate
}void wave::on_pushButton_2_clicked()
{
//Terminate
}@wave.h:
@#ifndef WAVE_H
#define WAVE_H
#include "ui_wave.h"
#include <alsa/asoundlib.h>
#include <QMainWindow>
#include <QObject>
#include "thread.h"namespace Ui {
class wave;
}class wave : public QMainWindow
{
Q_OBJECTpublic:
explicit wave(QWidget *parent = 0);
~wave();private slots:
void on_pushButton_clicked();
void on_pushButton_2_clicked();
void setup();signals:
void startWave(snd_pcm_t*, signed short*, snd_pcm_channel_area_t*);private:
QThread *thread;
Ui::wave *ui;public slots:
void on_write_loop(snd_pcm_t *handles, signed short *samples , snd_pcm_channel_area_t *areas);};@
-
Here is the error:
@wave::wave(QWidget parent) :
QMainWindow(parent),
ui(new Ui::wave)
{
ui->setupUi(this);
setup();
thread = new QThread();
thread->start();
////////////////////////////////////////
///move this section to main.cpp
wave Wave = new wave(); //<<<<----- you can't create wave in his constructor
Wave->moveToThread(thread);
connect(this, SIGNAL(startWave(snd_pcm_t, signed short, snd_pcm_channel_area_t*)), Wave, SLOT(on_write_loop(snd_pcm_t*, signed short*, snd_pcm_channel_area_t*)));
//and in connect use your first instance of wave class
////////////////////////////////////////}@
-
Are you trying to create a "wave" object inside of a "wave" and, and then move that second object into a thread?
That'll never work. If for no other reason than wave contains GUI elements, which are forbidden in threads other than the main thread. And, of course, you can't construct a class from within its own constructor.
You need a class for your interface, and a separate class to handle the work you're trying to do.
-
[quote author="mlong" date="1313175877"]Are you trying to create a "wave" object inside of a "wave" and, and then move that second object into a thread?
That'll never work. If for no other reason than wave contains GUI elements, which are forbidden in threads other than the main thread. And, of course, you can't construct a class from within its own constructor.
You need a class for your interface, and a separate class to handle the work you're trying to do.
[/quote]
Oh, mlong you are right as usual :-). I'm quite forgot about threads.