Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does my program crash at the return statement?

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

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".

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

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.

like image 555
Alok Avatar asked Apr 04 '12 06:04

Alok


1 Answers

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.

like image 127
Alok Avatar answered Oct 12 '22 23:10

Alok