2012-07-28 31 views
0
void Simulator::writeData() 
{ 
    resultFile_<<"#"<<*gTime_<<"\n"; 
    Wire* wire=wires_->next; 
    char* c=0; 
    while(wire!=0) 
    { 
     if(wire->getType() ==TYPE_OUT) 
     { 
      c=wire->getValue(); 
       resultFile_<<"b"<<c<<" "<<wire->getName()<<"\n"; //output result 

      //// for vector of results:///// 
      Tlogic tempEntery(wire->getSize()); 
      tempEntery.setTime(*gTime_); 
      tempEntery.setLogic(c); 
      goldenResult_.push_back(tempEntery); 
      //////////////////////////////// 

     } 
    wire=wire->next; 
    } 
} //end of function writeData 

在这段代码中,我需要一个临时聊天*变量,我把它命名为C, 我分配内存给它的,然后删除, 问题: 我的程序调用这个函数正确它的工作原理, ,但第十次,我称之为,程序中断,当我暂停它出现此错误: 该进程似乎是死锁。的进程似乎陷入僵局

这是错误: 该进程似乎死锁(或没有运行任何用户模式代码)。所有线程都已停止。 +确定按钮!

.................. 我认为它是因为我的vector(goldenResult_)!!因为当我评论该行没有死锁 我该如何解决这个错误?

+0

你有内存泄漏 - 使用delete []来取消分配ARRAY类型的内存 – mathematician1975 2012-07-28 09:14:41

+0

请你指定它是如何崩溃的?我们是在永恒中循环吗?如果是这样,链表的线可能是圆形的,我们永远不会退出循环。 – 2012-07-28 09:15:15

+0

还有另外一个问题 - 你在初始化时调用c变量的delete。由于它是一个局部变量,它将包含一些随机数据。当你调用delete的时候,内存管理器可能会默默地吞噬它,并在稍后崩溃,或者立即执行。 – 2012-07-28 09:17:26

回答

0

您正在根据外部条件分配/释放内存,这些外部条件无法从所示代码中确定。我的建议是正确的分配成语:指针初始化为0,后删除设置为0:

char* c = 0; // be sure you don't free if not allocated 
    while(wire!=0) 
    { 
     if(wire->getType() ==TYPE_OUT) 
     { 
      c=new char[wire->getSize()+1] ; 
      wire->getValue(c); 
       resultFile_<<"b"<<c<<" "<<wire->getName()<<"\n"; //output result 

      //// for vector of results:///// 
      Tlogic tempEntery(wire->getSize()); 
      tempEntery.setTime(*gTime_); 
      tempEntery.setLogic(c); 
      goldenResult_.push_back(tempEntery); 
      //////////////////////////////// 
     // delete c; 
     } 
     delete c ; // this can remain unchanged: delete of NULL is harmless 
     c = 0; // this way you avoid to delete more times than allocated 
     wire=wire->next; 
    } 

另一种方法是使用智能指针,用于跟踪分配给你的。

+0

我改变了我的代码,如你所说,但我仍然有这个问题!它显示控制台窗口,并没有什么会发生,直到按下暂停按钮,它显示错误,当我按OK,它打开crtmbox.c和一个绿色的箭头,显示程序应该从这里继续,这是crtmbox.c中的代码:# ifdef _UNICODE GetProcAddress(hlib,“MessageBoxW”)))#else/* _UNICODE/GetProcAddress(hlib,“MessageBoxA”)))#endif/_UNICODE */return 0; – Mohammad 2012-07-28 09:33:20

+0

对不起,我无法猜测问题可能是什么。当然,上述建议纠正了**潜在**问题。尝试发布更正后的代码,添加到您的问题... – CapelliC 2012-07-28 09:41:35

0

我的猜测是:

tempEntery.setTime(*gTime_); <--- (hm... gTime ... race somewhere ??) 
    tempEntery.setLogic(c); 
    goldenResult_.push_back(tempEntery); 

你也可以说处理您的载体的一部分?

对不起,没有评论,没有足够的分数。