• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2668
  • Last Modified:

Segmentation fault in stringstream initialization

Can someone pls tell me whats going wrong here


 bt
#0  0x00000000019dfabe in malloc_consolidate () from /lib64/libc.so.6
#1  0x00000000019e1c72 in _int_malloc () from /lib64/libc.so.6
#2  0x00000000019e2efd in malloc () from /lib64/libc.so.6
#3  0x0000000005c5a1fd in operator new () from /usr/lib64/libstdc++.so.6
#4  0x0000000005c388b1 in std::string::_Rep::_S_create () from /usr/lib64/libstdc++.so.6
#5  0x0000000005c3928b in std::string::_Rep::_M_clone () from /usr/lib64/libstdc++.so.6
#6  0x0000000005c39b95 in std::string::reserve () from /usr/lib64/libstdc++.so.6
#7  0x0000000005c33115 in std::basic_stringbuf<char, std::char_traits<char>, std::allocator<char> >::overflow () from /usr/lib64/libstdc++.so.6
#8  0x0000000005c37d6d in std::basic_streambuf<char, std::char_traits<char> >::xsputn () from /usr/lib64/libstdc++.so.6
#9  0x0000000005c2b62d in std::operator<< <char, std::char_traits<char>, std::allocator<char> > () from /usr/lib64/libstdc++.so.6
stringstream ss;
std::string outputdir;
std::string filename;
const char fileseparator = '/';
...
ss<<outputdir<<fileseparator<<filename;  //<<---- this is the line where it segfaults

Open in new window

0
sunnycoder
Asked:
sunnycoder
  • 3
  • 2
  • 2
2 Solutions
 
jkrCommented:
Honestly, the above snippet cannto be the reason for that. The following does not show this behaviour with neither VC++ nor g++:
#include <sstream>
#include <string>
using namespace std;
 
int main () {
 
stringstream ss;
std::string outputdir;
std::string filename;
const char fileseparator = '/';
 
ss<<outputdir<<fileseparator<<filename;  //<<---- this is the line where it segfaults
 
  return 0;
}

Open in new window

0
 
Infinity08Commented:
A segmentation fault in a malloc points to heap corruption as the most likely cause.

Check the rest of your code to see if there are any buffer overflows anywhere. If you can't spot it immediately, you can use a memory debugger to help you look for it :)
0
 
sunnycoderAuthor Commented:
thanks jkr ... unfortunately, that is what the core says ... am running it in valgrind now in case there is a memory corruption.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
jkrCommented:
Any chance that the string's buffers got screwed up?
0
 
sunnycoderAuthor Commented:
:) thanks I08 ... am giving that a shot.

jkr,

This is the first line of code in a function.  outputdir, fileseparator, filename are all members and all have been initialized - can see right values in gdb.

Is there a possibility that string's buffer are an issue?
0
 
jkrCommented:
Are you changing the strings by accessing their buffers directly or casting away the 'const' from 'string::c_str()'?
0
 
Infinity08Commented:
If it's a heap corruption, then it could have occurred anywhere, and at any time. There is no reason that the problem code would be close to the location where the segmentation fault occurred.

Start by running a memory check tool to look for buffer overflows.
If that doesn't help, you can debug the segmentation fault directly. Examine the memory location, and check the block just before it. Find out what part of the code was using that block last, and you might have your problem :)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now