facing core dump while assigning the empty string to string variable
<p>I am facing core dump when my application is trying to write the text into a file. It is using string operations as shown in the code. When multiple thread try to call this function and try to write to log file then my application is dumping core. I have gone through different forums and tried to search for any problem in string assignment. I have seen that some of us have commented on that string assignment statement as it is not good to use. Instead use string member function.</p>

<p><strong>Function:</strong></p>

<pre><code>void Debug::writeToFile()
{
if(_ptrMutex == NULL)
    _ptrMutex = new Mutex();
_ptrMutex-&gt;getLock();
write(_fd,_cacheStr.c_str(),_cacheStr.size());
_cacheStr = ""; //flush the write string
_ptrMutex-&gt;releaseLock();
}
</code></pre>

<p><strong>cacheWriteString</strong></p>

<pre><code>bool Debug::cacheWriteString(string strIn)
{
if(_ptrMutex == NULL)
    _ptrMutex = new CndMutex();
_ptrMutex-&gt;getLock();
char timeStamp[100];

struct timeval  tv;
struct tm   tm;

if (gettimeofday(&amp;tv, NULL)==0 &amp;&amp; localtime_r((time_t*)&amp;(tv.tv_sec), &amp;tm)) {
    int ret = strftime(timeStamp,sizeof(timeStamp),"%F-%T",&amp;tm);
    snprintf(timeStamp+ret, sizeof(timeStamp)-ret, ".%03ld", tv.tv_usec/1000);
} else
Time::getTimeStamp(timeStamp,100);
_cacheStr = _cacheStr + "[" + timeStamp + "] " + strIn;
if(_cacheStr.size() &gt;= _maxCacheSize)
{
    _ptrMutex-&gt;releaseLock();
    return true;
}
_ptrMutex-&gt;releaseLock();
return false;
}
</code></pre>

<p><strong>This functions is called as</strong></p>

<pre><code>void Debug::LOG_PRINT_ERROR(char* ptrFormat,...)
{
va_list argList;
va_start(argList,ptrFormat);

if(_logType == CONSOLE_LOG) //console log
{
    cout&lt;&lt;"#### [ERROR] ";
    vprintf(ptrFormat,argList);
    cout&lt;&lt;endl;
}
else if(_logType == SYS_LOG)// [syslog]
{
    syslog(LOG_ERR,"%s","[ERROR]####");
    vsyslog(LOG_ERR,ptrFormat,argList);
}
else if(_logType == FILE_LOG)
{
    char str[1024];
    //SBYTE4 ret = vsprintf(str,ptrFormat,argList);
    SBYTE4 ret = vsnprintf(str,1023,ptrFormat,argList);
    str[1023]='\0';
    if(ret &gt; -1)
    {
        string dataStr = "[ERROR]####";
        dataStr = dataStr +str+"\n";
        if(cacheWriteString(dataStr))
        {
            if(chkFile() == DIM_PASS)
            {
                writeToFile();
            }
            else
            {
                cout&lt;&lt;"#### LOG_ERROR() Unable to Write Data To File: "&lt;&lt;_fileName &lt;&lt;endl;
            }
        }
    }
    else
    {
        cout&lt;&lt;"#### LOG_ERROR() Output Error Is Encountered: "&lt;&lt;_fileName &lt;&lt;endl;
    }
}
va_end(argList);
</code></pre>

<p>}</p>

<p><strong>Core Dump details as follows:</strong></p>

<pre><code>Thread 1 (Thread 8426):
#0  0x00a2a402 in __kernel_vsyscall ()
#1  0x0072bdf0 in raise () from /lib/libc.so.6
#2  0x0072d701 in abort () from /lib/libc.so.6
#3  0x0545651a in ?? () from /usr/lib/libstdc++.so.6
#4  0x05456552 in std::terminate() () from /usr/lib/libstdc++.so.6
#5  0x0545668a in __cxa_throw () from /usr/lib/libstdc++.so.6
#6  0x053ed1ef in std::__throw_length_error(char const*) () from /usr/lib/libstdc++.so.6
#7  0x0543211d in std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;::_Rep::_S_create(unsigned int, unsigned int, std::allocator&lt;char&gt; const&amp;) () from /usr/lib/libstdc++.so.6
#8  0x05433e28 in std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;::_M_mutate(unsigned int, unsigned int, unsigned int) () from /usr/lib/libstdc++.so.6
#9  0x05433fca in std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;::_M_replace_safe(unsigned int, unsigned int, char const*, unsigned int) () from /usr/lib/libstdc++.so.6
#10 0x05434065 in std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;::assign(char const*, unsigned int) () from /usr/lib/libstdc++.so.6
#11 0x0815e9a8 in Debug::writeToFile() ()
#12 0x08161866 in Debug::LOG_PRINT_ERROR(char*, ...) ()
#13 0x0812bcc6 in DimInternalMsgHandler::handlePeerStatusIndication(DimPeerStatusInd*) ()
#14 0x0812c52a in DimInternalMsgHandler::handleInternalMessage(unsigned char*, int) ()
#15 0x0812aa05 in DimDanIfController::handleInMessage(NwPacket&amp;) ()
#16 0x081ab28f in WorkerThreadPool::onEvent(int, nxs_util::EventMessage64*) ()
#17 0x0021dd94 in nxs_util::EventHandler64::workerProc(int) () from /home/nextgen/NXS/lib/libnxsutil.so
#18 0x001c193e in nxs_util::WorkerThread::run() () from /home/nextgen/NXS/lib/libnxsutil.so
#19 0x001c5b36 in nxs_util::Thread::__Thread_run(void*) () from /home/nextgen/NXS/lib/libnxsutil.so
#20 0x0087f832 in start_thread () from /lib/libpthread.so.0
#21 0x007d4e0e in clone () from /lib/libc.so.6
</code></pre>

<p>We are suspecting that there is problem of &lt;_cacheStr = "";> statement we are using in our code. We are thinking to change to _cacheStr.clear(); But we are not able to reproduce this core now.</p>

<p>If anyone has any idea about it or faced it please let us know.</p>

<p>Thank you very much for your inputs.</p>
