Quantcast

Boost::interprocess::string problems

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Boost::interprocess::string problems

this_thread
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<Foo, boost::interprocess::managed_shared_memory::segment_manager> FooAllocator;

Then create a special vector which uses that allocator:

typedef boost::interprocess::vector<Foo, FooAllocator> FooInterprocessVector;

This works great, provided I remember to find<FooInterprocessVector> on the opposite process before accessing the vector during periods when it may have been modified.

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;

Using this, so long as I only write a short string to the shared memory, things work fine, but anything over a few characters quickly clobbers other data.  Has anyone used Interprocess::Strings before and could possibly point me in the direction I'm going wrong?  Thank you all!
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Boost::interprocess::string problems

Ion Gaztañaga
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<typename CharT, typename Traits, typename A>
class basic_string;

so defint it like this:

using namespace boost::interprocess;
typedef basic_string<char, std::char_traits<char>, allocator_type>
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.

Best,

Ion
_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Boost::interprocess::string problems

Boris Schäling
In reply to this post by this_thread
On Fri, 23 Jul 2010 15:45:55 +0200, this_thread <[hidden email]>  
wrote:

> [...]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

_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Boost::interprocess::string problems

this_thread
In reply to this post by Ion Gaztañaga
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<char, std::char_traits<char>, allocator_type>
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<char, std::char_traits<char>, boost::interprocess::allocation_type> BoostString;


AND:


typedef boost::interprocess::allocator<char, boost::interprocess::managed_shared_memory::segment_manager> CharAllocator;
typedef boost::interprocess::basic_string<char, std::char_traits<char>, boost::interprocess::CharAllocator> BoostString;


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?
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Boost::interprocess::string problems

this_thread
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...
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Boost::interprocess::string problems

Ion Gaztañaga
In reply to this post by this_thread
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<char, std::char_traits<char>,
> boost::interprocess::allocation_type>  BoostString;

allocation_type does not exist, it must be any shared memory allocator
of type char.

> AND:
>
>
> typedef boost::interprocess::allocator<char,
> boost::interprocess::managed_shared_memory::segment_manager>  CharAllocator;
> typedef boost::interprocess::basic_string<char, std::char_traits<char>,
> boost::interprocess::CharAllocator>  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
_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Boost::interprocess::string problems

Ion Gaztañaga
In reply to this post by this_thread
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
_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Boost::interprocess::string problems

this_thread
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...
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Boost::interprocess::string problems

this_thread

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...
Loading...