boost::interprocess:basic_string : Capacity no more than 10
Hello,
I first present to you my code and I will explain my problem with the code .
ShmString.h :
namespace ip=boost::interprocess;
typedef ip::allocator
::assign (this=0xb7428274,
s=0xb9adf154
) I tried to used the other constructors by default for basic_string but it remains the same. This code only works when in the constructors the string size are below 10. Could you someone explain me why I obtain this? Do I have some faulty code? Could you provide some examples on how store string with interprocess::basic_string in shared memory for a size for more than 10 characters? I will be very grateful because I am lost... Thanks you in advance Regards Samuel -- View this message in context: http://www.nabble.com/boost%3A%3Ainterprocess%3Abasic_string-%3A-Capacity-no... Sent from the Boost - Users mailing list archive at Nabble.com.samuel.gallard escribió:
Hello,
I first present to you my code and I will explain my problem with the code .
First of all, you can't use virtual functions in shared memory, this is explained in the documentation "Limitations When Constructing Objects In Mapped Regions" http://www.boost.org/doc/libs/1_39_0/doc/html/interprocess/sharedmemorybetwe... the 10 limit is probably because string has an internal buffer before dynamically allocating anything (an optimization known as Small String Optimization). Everytying is write until you try to allocate memory See "doc_cont.cpp" example to see how containers (and strings) are placed in shared memory. Best, Ion
I finally managed to find which was my error. In my class Element, I used a constructor to copy an Element like this : Element::Element (const Element& elem){ try{ this->dir.assign(elem.getDir()); this->file.assign(elem.getFile()); this->type.assign(elem.getType()); } catch(ip::interprocess_exception &e){ ostringstream oss; const std::string what(e.what()); oss << "Class Element in constructor by recopy -> "; oss << what; throw EventException(oss.str().c_str(), __LINE__ ); } } If I put it in comment, I am able to use it without problems in interprocess and define the ShmString with a length upper than 10. But with threads (boost::thread), I am still limited to 10 if I used them with this constructor. If it is more than 10, I obtained an error when I create an element like this : Element("/home/sdfqsd/","titi.txt",Request::st_change) The error message is : 0xb7ca3283 in strlen () from /lib/tls/i686/cmov/libc.so.6 (gdb) bt #0 0xb7ca3283 in strlen () from /lib/tls/i686/cmov/libc.so.6 #1 0x080600c9 in std::char_traits<char>::length ( __s=0xb8aa1130
) at /usr/include/c++/4.2/bits/char_traits.h:258 #2 0x0807c0bf in boost::interprocess::basic_string::assign (this=0xb6bfb288, s=0xb8aa1130
) at /usr/local/include/boost/interprocess/containers/string.hpp:988 #3 0x08079b59 in Element (this=0xb6bfb280, elem=@0xb7f4f0d0) at ../Events/MessageQueue/Element.cpp:21
The line 21 in Element.cpp is the first line in the constructor by copy : this->dir.assign(elem.getDir()); I don't understand why it works like that. If you could give me a clue, I will appreciate. About "doc_cont.cpp", it will be great to have the same example with a vector of a class that contained basic_string attributes instead of a vector of int. Because it is easy to understand with a basic type like int but a little tricky to apply it to a custom type. Regards -- View this message in context: http://www.nabble.com/boost%3A%3Ainterprocess%3Abasic_string-%3A-Capacity-no... Sent from the Boost - Users mailing list archive at Nabble.com.
participants (2)
-
Ion Gaztañaga
-
samuel.gallard