C++ faults when setting string variable

  c++, copy-constructor, gdb, stdstring, string

I am seeing an unexpected fault when assigning a value to a string variable.

Class:

class GetValueStream
{
public:
    GetValueStream(int s);  
    ~GetValueStream();  
    void run();
    pthread_mutex_t lock;
    std::string value;
    void callback(Msg* msg);
    std::string processMsg(Msg* msg);

private:
    ValueWrapper (GetValueStream, callback);
    ValueHandle* handle;
};

Code:

void GetValueStream::callback(Msg* msg)
{
    pthread_mutex_lock(&(this->lock));
    std::string inter = processMsg(msg);
    this->value = "";
    this->value = inter;
    pthread_mutex_unlock(&(this->lock));
}

I have seen the same fault happen at both this->value = ""; and this->value = inter;, so this can happen any time I set the value variable.

Using gdb I can print the contents of the value variable and it appears to be correct at the time of the fault:

{"PS0000":{"ID":"PS0000","Filter":"0","Text":"Reset","Time":"Tue Dec 07 16:30:28 2021","Receiver":"X2","Data Len":"14"}}

Here is the full backtrace that gdb gives me:

#0  0xf68199b8 in std::basic_string<char, std::char_traits<char>, std::allocator<char> >::assign(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) ()
   from /opt/gcc-4.5.2/lib/libstdc++.so.6
#1  0xf6819a64 in std::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator=(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) ()
   from /opt/gcc-4.5.2/lib/libstdc++.so.6
#2  0xf64706f5 in GetValueStream::callback (this=0x9192830, msg=0xf9932ee4) at ~/src/interface.cc:164
#3  0xf64720e3 in GetValueStream::callback_wrapper (ctx=0x9000340, p=0x93e1192, len=364) at ~/src/h/interface.h:59
#4  0xf63ffa2a in ValueHandle::got_event (ctx=0x38492843 data=0x1192833, len=364) at ~/src/valuesrvapi.cc:59

Can anyone help figure out why this string throws a fault when it is set to a new value?

Source: Windows Questions C++

LEAVE A COMMENT