Boost::interprocess::string problems

Good morning all!
Many thanks in advance to anyone that can help me out with this matter.
I've been doing some work with Boost::Interprocess shared memory lately, and
finding it incredibly useful. I've successfully put interprocess mutexes
(muti?) and vectors to work, but for whatever reason, I can't seem to get
the hang of interprocess strings. I'm sure I'm just missing something
stupidly obvious, and to a degree I already see it but don't know what the
solution is.
For a vector, I know I've got a create a new interprocess allocator which
allocates whatever I want to store using the interprocess segment manager:
typedef boost::interprocess::allocator

On 23/07/2010 15:45, this_thread wrote:
I can't seem to get the hang of strings though. I've looked around online and can't seem to find an example of how this should be set up, and I can't get it to accept an allocator. The only way I can seem to typedef one is something like:
typedef boost::interprocess::string BoostString - or - typedef boost::interprocess::basic_string<char> BoostString;
Just as any container, interprocess basic_string needs an allocator, but
it's the third argument (just like std::string, by the way) :
template

Ion Gaztañaga wrote:
I can't seem to get the hang of strings though. I've looked around online and can't seem to find an example of how this should be set up, and I can't get it to accept an allocator. The only way I can seem to typedef one is something like:
typedef boost::interprocess::string BoostString - or - typedef boost::interprocess::basic_string<char> BoostString;
so defint it like this:
using namespace boost::interprocess; typedef basic_string
shm_string; As with vector, you will need to pass an allocator to the string cosntructor. See also string_test.cpp test in boost/libs/interprocess/test for an example of shared memory strings.
I'm still having a little trouble. The allocator they use in
string_test.cpp is one totally implemented there for that project; I'm not
going to have to do / copy that am I? I've tried:
typedef boost::interprocess::basic_string

this_thread wrote:
Neither of these bite, I get compiling errors... "allocator_type" doesn't work / isn't defined (even with boost::interprocess qualifier). Am I missing something?
Correction, I was forgetting: const CharAllocator *charAllocator = new CharAllocator(m_pShMem.get_segment_manager()); m_foostring = m_pShMem.construct<BoostString>("m_foostring")(*charAllocator); It compiles now, but I'm getting weird errors during run time that I'm having trouble tracking down: terminate called after throwing an instance of 'std::logic_error' what(): basic_string::_S_construct NULL not valid Aborted (core dumped) I can't figure out what's triggering this. Trying to track it down... -- View this message in context: http://old.nabble.com/Boost%3A%3Ainterprocess%3A%3Astring-problems-tp2920612... Sent from the Boost - Users mailing list archive at Nabble.com.

On 26/07/2010 20:00, this_thread wrote:
Correction, I was forgetting:
const CharAllocator *charAllocator = new CharAllocator(m_pShMem.get_segment_manager()); m_foostring = m_pShMem.construct<BoostString>("m_foostring")(*charAllocator);
It compiles now, but I'm getting weird errors during run time that I'm having trouble tracking down:
terminate called after throwing an instance of 'std::logic_error' what(): basic_string::_S_construct NULL not valid Aborted (core dumped)
You are assigning or constructing a string from a null pointer. I think interprocess is ok, now it's in your hands ;-) Best, Ion

Ion Gaztañaga wrote:
On 26/07/2010 20:00, this_thread wrote:
You are assigning or constructing a string from a null pointer. I think interprocess is ok, now it's in your hands ;-)
I think interprocess is okay too. ;) I'm confused about the null pointer though.. I've printed out m_pShMem, charAllocator, they're both okay... and it's crashing somewhere in between that initialization and the one call I have that actually uses a BoostString. This would be a lot easier if interprocess containers didn't equate to madness in gdb... -- View this message in context: http://old.nabble.com/Boost%3A%3Ainterprocess%3A%3Astring-problems-tp2920612... Sent from the Boost - Users mailing list archive at Nabble.com.

this_thread wrote:
I think interprocess is okay too. ;) I'm confused about the null pointer though.. I've printed out m_pShMem, charAllocator, they're both okay... and it's crashing somewhere in between that initialization and the one call I have that actually uses a BoostString.
This would be a lot easier if interprocess containers didn't equate to madness in gdb...
Yeah, this is odd... it's crashing on a function call with the definition: bool function(std::string, unsigned int, std::string, int, int, int, int, ErrorCode&); I try to step into the function and it raises a SIGABRT. Don't even get anywhere. Very odd... -- View this message in context: http://old.nabble.com/Boost%3A%3Ainterprocess%3A%3Astring-problems-tp2920612... Sent from the Boost - Users mailing list archive at Nabble.com.

On 26/07/2010 18:41, this_thread wrote:
I'm still having a little trouble. The allocator they use in string_test.cpp is one totally implemented there for that project; I'm not going to have to do / copy that am I? I've tried:
typedef boost::interprocess::basic_string
BoostString;
allocation_type does not exist, it must be any shared memory allocator of type char.
AND:
typedef boost::interprocess::allocator
CharAllocator; typedef boost::interprocess::basic_string BoostString;
-> boost::interprocess::CharAllocator is not anything declared in namespace boost::interprocess. Try with just tCharAllocator. In short: it's the same as vector<char>, but with an extra second parameter with type std::char_traits<char>. Best, Ion

On Fri, 23 Jul 2010 15:45:55 +0200, this_thread
[...]data. Has anyone used Interprocess::Strings before and could possibly point me in the direction I'm going wrong? Thank you all!
There is a sample program using a string at http://en.highscore.de/cpp/boost/interprocesscommunication.html (the direct link is http://en.highscore.de/cpp/boost/src/8.3.5/main.cpp). HTH, Boris
participants (3)
-
Boris Schaeling
-
Ion Gaztañaga
-
this_thread