为什么我的程序在返回语句中崩溃?

时间:2023-01-17 13:34:50

I get an exception while executing following piece of code

在执行下面的代码时,我得到一个异常。

bool FieldValueMessage::Get(const std::string &field, double & value)
{
    string text;
    if(Get(field,text))
    {
        std::stringstream sstr(text);
        sstr >> value;
        if(sstr.fail())
            return false;
        else
            return true;
    } 
    else 
    {
        return false;
    }        
} 

Get function is as below

Get函数如下所示

bool HashMapMessage::Get(const std::string &field, std::string & value)
{
    Field2Value::iterator i = f2v.find(field);
    if(i==f2v.end()){
        return false;
    } else {
        value = i->second;
        return true;
    }
}

caller of Get function. I don't see any issue here.

得到函数的调用者。我看不出有什么问题。

for(i=quote_fields.begin(),j=0;i!=quote_fields.end();i++,j++){
    if (msg->Get((*i).c_str(),tmp)){
        if(tmp>0 && x->staging_data[j+1]!=tmp){
            x->staging_data[j+1] = tmp;
            has_update = true;
        }
    }
}

Call Stack is

调用堆栈是

ntdll.dll!_RtlpCoalesceFreeBlocks@16()  + 0x35 bytes    
ntdll.dll!_RtlFreeHeap@12()  + 0x91f bytes  
msvcr90.dll!_free()  + 0xcd bytes   
msvcp90.dll!std::locale::`scalar deleting destructor'()  + 0x19 bytes   
msvcp90.dll!std::ios_base::_Ios_base_dtor()  + 0x39 bytes   
msvcp90.dll!std::basic_stringstream<char,std::char_traits<char>,std::allocator<char> >::`vbase destructor'()  + 0x19 bytes  
asapGeneric.dll!asap::FieldValueMessage::Get(const std::basic_string<char,std::char_traits<char>,std::allocator<char> > & field="ASK", double & value=0.055000000000000000)  Line 33 + 0x17 bytes   C++
_hdf.pyd!asap::TradeAndQuoteNormalizer::ParseQuote(asap::FieldValueMessage * msg=0x027194c0, asap::TAQ * taq=0x02716908)  Line 84 + 0x36 bytes  C++

value of field is "ASK".

字段的值为“ASK”。

This code has been running fine without any issues, but now i am getting exception on "return true" statement.

这段代码运行良好,没有任何问题,但是现在我在“return true”语句中得到了异常。

when i debug though the program... sstr.fail() returns false. The pointer comes on return true; statement. At this point when i do a single step, suddenly I get Unhandled exception: Access violation reading from location xxxxx. I have never seen an exception on a return statement. What is incorrect in this case? This c++ program is called from a python script.

当我调试程序时……sstr.fail()返回false。指针返回true;声明。在这一点上,当我执行一个步骤时,突然我得到了一个未处理的异常:访问违反从位置xxxxx读取。我从未见过回复声明中有例外。在这种情况下什么是不正确的?这个c++程序是从python脚本中调用的。

1 个解决方案

#1


1  

Answer to above post is really an collections of comments on the question. more than one replies helped me find the solution. The problem in above case was that I did not initialize the staging data properly. It was expected to be an array of size same as quote_fields but it was initialized to length 1. So first element was updated correctly but during the second element in quote_fields array is bring read, i was getting above exception. Apparantly exception is not raised as soon as i try to access incorrect element. Exception is comin from stringstream destructor. As per Retired Ninja, the exception is not raised immediately but only until heap tries to merge 2 free blocks at a later point as shown in the call stack.

对以上文章的回答实际上是对这个问题的一系列评论。不止一个人的回答帮助我找到了解决方案。上面的问题是我没有正确地初始化staging数据。它应该是一个大小与quote_fields相同的数组,但初始化为length 1。第一个元素得到了正确的更新但是在quote_fields数组中的第二个元素带来读取时,我得到了上面的异常。当我试图访问不正确的元素时,显然不会引发异常。异常来自stringstream析构函数。就像每个退休的忍者一样,这个异常不会立即被提升,除非堆尝试在稍后的点上合并两个空闲块,就像调用堆栈中显示的那样。

#1


1  

Answer to above post is really an collections of comments on the question. more than one replies helped me find the solution. The problem in above case was that I did not initialize the staging data properly. It was expected to be an array of size same as quote_fields but it was initialized to length 1. So first element was updated correctly but during the second element in quote_fields array is bring read, i was getting above exception. Apparantly exception is not raised as soon as i try to access incorrect element. Exception is comin from stringstream destructor. As per Retired Ninja, the exception is not raised immediately but only until heap tries to merge 2 free blocks at a later point as shown in the call stack.

对以上文章的回答实际上是对这个问题的一系列评论。不止一个人的回答帮助我找到了解决方案。上面的问题是我没有正确地初始化staging数据。它应该是一个大小与quote_fields相同的数组,但初始化为length 1。第一个元素得到了正确的更新但是在quote_fields数组中的第二个元素带来读取时,我得到了上面的异常。当我试图访问不正确的元素时,显然不会引发异常。异常来自stringstream析构函数。就像每个退休的忍者一样,这个异常不会立即被提升,除非堆尝试在稍后的点上合并两个空闲块,就像调用堆栈中显示的那样。