C++ delete导致程序崩溃的问题
问题的经过就是首先在主函数中申请了一个double数组。
然后定义了一个函数,并将这个函数以函数指针的形式传递给一个类,在那个类中调用了这个函数。
在这个函数中就用到了之前申请的double数组。函数如下:
void update(double* x,int numFeature,int numData,double* const data){
for(int i=0;i<numData;i++){
for(int j=0;j<numFeature;j++){
x[j]=x[j]+data[i*numFeature+j];
}
}
}
运行到最后就delete了,但是运行到delete的时候就崩溃了。
程序是MPI程序。运行就会报以下错误:
===================================================================================
= BAD TERMINATION OF ONE OF YOUR APPLICATION PROCESSES
= PID 10876 RUNNING AT node4
= EXIT CODE: 11
= CLEANING UP REMAINING PROCESSES
= YOU CAN IGNORE THE BELOW CLEANUP MESSAGES
===================================================================================
[proxy:0:0@node0] HYD_pmcd_pmip_control_cmd_cb (pm/pmiserv/pmip_cb.c:885): assert (!closed) failed
[proxy:0:0@node0] HYDT_dmxu_poll_wait_for_event (tools/demux/demux_poll.c:76): callback returned error status
[proxy:0:0@node0] main (pm/pmiserv/pmip.c:206): demux engine error waiting for event
[mpiexec@node0] HYDT_bscu_wait_for_completion (tools/bootstrap/utils/bscu_wait.c:76): one of the processes terminated badly; aborting
[mpiexec@node0] HYDT_bsci_wait_for_completion (tools/bootstrap/src/bsci_wait.c:23): launcher returned error waiting for completion
[mpiexec@node0] HYD_pmci_wait_for_completion (pm/pmiserv/pmiserv_pmci.c:218): launcher returned error waiting for completion
[mpiexec@node0] main (ui/mpich/mpiexec.c:344): process manager error waiting for completion
经过各种尝试,发现问题就出在update函数中。
神奇的地方在于,把函数改成这样就一切正常:
void update(double* x,int numFeature,int numData,double* const data){
for(int i=0;i<numData;i++){
for(int j=0;j<numFeature;j++){
double temp=data[i*numFeature+j];
x[j]+=temp;
}
}
}
把这个语句注释掉也不会报错:
void update(double* x,int numFeature,int numData,double* const data){
for(int i=0;i<numData;i++){
for(int j=0;j<numFeature;j++){
//x[j]+=data[i*numFeature+j];
}
}
}
改成这样也不会报错!
void update(double* x,int numFeature,int numData,double* const data){
for(int i=0;i<numData;i++){
for(int j=0;j<numFeature;j++){
x[j]=data[i*numFeature+j];
}
}
}
觉得好奇怪啊!匪夷所思!也没有越界,也没有修改过指针。求指点!