2

I am getting an odd segmentation error on these lines of code.

OutputHandler(std::string const& path) throw(std::exception);

MyClass::MyClass() 
: basepath(boost::filesystem::canonical("./").string() + "/"),
outputHandler(basepath + "log.log") {} // Error traces back to this line (line 11)

The segmentation error:

#0  0x00007ffff6fd512b in std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(std::string const&) ()
   from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#1  0x0000000000423bd7 in std::operator+<char, std::char_traits<char>, std::allocator<char> > (__lhs=..., __rhs=0x4659a1 "log.log")
    at /usr/include/c++/4.8/bits/basic_string.h:2405
#2  0x0000000000414466 in MyClass::MyClass (this=0x7fffffffd6f0) at ./source/MyClass.cpp:11
#3  0x0000000000464a28 in main () at ./source/main.cpp:10

What is causing this error and how do I fix it? I am guessing something is going wrong when converting the basepath + "log.log" to a std::string?

5
  • Your member variable basepath, is it by any chance a reference? (Accidentially, of course) Commented Oct 22, 2013 at 17:41
  • I never stress enough that "questions concerning problems with code you've written must [...] include valid code to reproduce it". Commented Oct 22, 2013 at 17:42
  • MyClass::basepath is a simple std::string declared as ` std::string basepath;` inside the class declaration. Commented Oct 22, 2013 at 17:43
  • 1
    Next Q: In the class definition, does basepath stand before or after outputHandler? (And do you have warnings enabled?) Commented Oct 22, 2013 at 17:45
  • @DanielFrey Warnings are off, Mark's answer solved it for me. Thanks for all your help. I really thought the problem would have something to do with the string construction. DX Commented Oct 22, 2013 at 17:52

1 Answer 1

3

The order of initialization of member variables is the order they're declared in the class definition, not the order they're listed in the initializer. The basepath string is probably not initialized yet.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.