We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Segmentation fault in stringstream initialization

sunnycoder
sunnycoder asked
on
Medium Priority
3,310 Views
Last Modified: 2012-05-06
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

Comment
Watch Question

jkr
CERTIFIED EXPERT
Top Expert 2012
Commented:
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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
CERTIFIED EXPERT
Top Expert 2009

Commented:
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 :)
CERTIFIED EXPERT
Top Expert 2006

Author

Commented:
thanks jkr ... unfortunately, that is what the core says ... am running it in valgrind now in case there is a memory corruption.
jkr
CERTIFIED EXPERT
Top Expert 2012

Commented:
Any chance that the string's buffers got screwed up?
CERTIFIED EXPERT
Top Expert 2006

Author

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?
jkr
CERTIFIED EXPERT
Top Expert 2012

Commented:
Are you changing the strings by accessing their buffers directly or casting away the 'const' from 'string::c_str()'?
CERTIFIED EXPERT
Top Expert 2009
Commented:
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 :)
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.