Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt Creator and other tools
  4. Error: terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc The program has unexpectedly finished.[SOLVED]

Error: terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc The program has unexpectedly finished.[SOLVED]

Scheduled Pinned Locked Moved Qt Creator and other tools
5 Posts 3 Posters 16.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • O Offline
    O Offline
    ogopa
    wrote on 12 Aug 2011, 18:35 last edited by
    #1

    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_OBJECT

    public:
    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);

    };@

    1 Reply Last reply
    0
    • R Offline
      R Offline
      rokemoon
      wrote on 12 Aug 2011, 19:04 last edited by
      #2

      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
      ////////////////////////////////////////

      }@

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mlong
        wrote on 12 Aug 2011, 19:04 last edited by
        #3

        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.

        Software Engineer
        My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

        1 Reply Last reply
        0
        • R Offline
          R Offline
          rokemoon
          wrote on 12 Aug 2011, 19:06 last edited by
          #4

          [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.

          1 Reply Last reply
          0
          • O Offline
            O Offline
            ogopa
            wrote on 16 Aug 2011, 19:23 last edited by
            #5

            Oh my god, after reading a bit more on it, i really was making silly mistakes. Thanks guys

            1 Reply Last reply
            0

            1/5

            12 Aug 2011, 18:35

            • Login

            • Login or register to search.
            1 out of 5
            • First post
              1/5
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved