-2

This is my function:

void cmdChangeSett(cmdbuf* cmd_buffer, CTimeTag tagger, uint8_t chNum, int mask) {

double* oldChannelvoltage = new double[chNum];
double* newChannelvoltage = new double[chNum];
bool* oldEdge = new bool[chNum];
bool* newEdge = new bool[chNum];
int newmask;
double chDiff;
int edgeDiff;
int i;

while (runAcquisition) {

    for (i = 0; i < chNum; i++) {
        cmd_getThresh_getEdge(cmd_buffer, i, oldChannelvoltage, oldEdge);

    }

    Sleep(500);

    newmask = 0;
    for (i = 0; i < chNum; i++) {
        cmd_getThresh_getEdge(cmd_buffer, i, newChannelvoltage, newEdge);
        chDiff = oldChannelvoltage[i] - newChannelvoltage[i];
        edgeDiff = oldEdge[i] - newEdge[i];
        //printf("\nOld: %.2f, New: %.2f -> DIFF = %.2f", oldChannelvoltage[i], newChannelvoltage[i], diff);

        if (chDiff != 0) {

            WARN(newChannelvoltage[i] > 1.5, newChannelvoltage[i] = 1.5f, "Threshold of %.2fV exceeds channel %i's max. Rounding to %.2fV.", newChannelvoltage[i], i + 1, 1.5);
            WARN(newChannelvoltage[i] < -1.5, newChannelvoltage[i] = -1.5f, "Threshold of %.2fV exceeds channel %i's max. Rounding to %.2fV.", newChannelvoltage[i], i + 1, -1.5);
            tagger.SetInputThreshold(i + 1, newChannelvoltage[i]);

        }

        if (edgeDiff) {
            if (!newEdge[i]) newmask += 1 << i;
        }
    }

    if (newmask != mask) {
        tagger.SetInversionMask(newmask);
        mask = newmask;
    }

}

delete[] oldChannelvoltage;
delete[] newChannelvoltage;
delete[] oldEdge;
delete[] newEdge;
}

When I launch the thread from the main() it crashes:

    int main(int argc, char** argv) {
      int mask = 0;             
      cmdbuf* cmd_buffer;
      CTimeTag tagger;

      //some code ....
      //......

      boost::function<void()> cmdChangeSettThread = boost::bind(&cmdChangeSett,cmd_buffer, tagger, 16, mask);
      boost::thread th(cmdChangeSettThread);
      //some other code ...
      return 0;
      }

Any idea ?? I thought the problem was caused by the arrays I'm using in the function but I can't figure out how to solve the problem. Thank you very much!

cala
  • 1
  • 2
  • Does the function `cmdChangeSett` get called? Nothing looks wrong with the code. How much _other code_ is between the launch of the thread and the `return`? Consider putting a `th.join()` before `return 0;` Your thread may be continuing to run after main exits (and cleans up all the arrays it passes so when the thread tries to access them it causes a problem). I can't see `runAcquisition` being declared or changed to a `false` condition. Is this happening before the `return`? – Tas Jul 17 '15 at 00:47

2 Answers2

2

You need to wait for the thread to finish in main.

If the thread destructor is called and the thread is still running terminate() is called.

  th.join();  // should stop the application crashing.
  return 0;
  }

PS. None of this is good:

double* oldChannelvoltage = new double[chNum];
double* newChannelvoltage = new double[chNum];
bool* oldEdge = new bool[chNum];
bool* newEdge = new bool[chNum];

Use a vector (or an array).

Get a code review: http://codereview.stackexchange.com

Martin York
  • 257,169
  • 86
  • 333
  • 562
  • @Loki, yes I absolutely agree with your suggestion "Use a vector", I was indeed planning to change it after having found the problem! Thank you:) – cala Jul 17 '15 at 16:58
0

Thank you everybody! I found the problem!! I was stupidly passing the object CTimeTag tagger by value, I'm sorry if I wasn't super clear in presenting the problem! So now the function definition is:

void cmdChangeSett(cmdbuf* cmd_buffer, CTimeTag *tagger, tt_buf* buffer, uint8_t chNum, int mask) 

and when I'm calling it with boost::bind I have:

boost::function<void()> cmdChangeSettThread = boost::bind(&cmdChangeSett,cmd_buffer, &tagger, buffer, 16, mask);

Thank you again!

cala
  • 1
  • 2