when launching boost::thread the .exe chrashes -


this 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) {      (i = 0; < chnum; i++) {         cmd_getthresh_getedge(cmd_buffer, i, oldchannelvoltage, oldedge);      }      sleep(500);      newmask = 0;     (i = 0; < 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 %.2fv.", newchannelvoltage[i], + 1, 1.5);             warn(newchannelvoltage[i] < -1.5, newchannelvoltage[i] = -1.5f, "threshold of %.2fv exceeds channel %i's max. rounding %.2fv.", newchannelvoltage[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 launch thread main() 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 ?? thought problem caused arrays i'm using in function can't figure out how solve problem. thank much!

you need wait thread finish in main.

if thread destructor called , thread still running terminate() called.

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

ps. none of good:

double* oldchannelvoltage = new double[chnum]; double* newchannelvoltage = new double[chnum]; bool* oldedge = new bool[chnum]; bool* newedge = new bool[chnum]; 

use vector (or array).

get code review: http://codereview.stackexchange.com


Comments

Popular posts from this blog

php - Zend Framework / Skeleton-Application / Composer install issue -

c# - Better 64-bit byte array hash -

python - PyCharm Type error Message -