Boost logo

Boost Users :

Subject: [Boost-users] Problem with boost::interprocess:basic_string : Error address out of bounds
From: samuel.gallard (samuel.gallard_at_[hidden])
Date: 2009-07-24 09:59:53


Hello,

I first present to you my code and I will explain later :

ShmString.h :

#include
#include
#include

namespace ip=boost::interprocess;

typedef ip::allocator CharAllocator;

class ShmString : public ip::basic_string, CharAllocator >
{
        public:
                ShmString ();
                virtual ~ShmString ();
       private:
               /**
                 * The segment to build the string
                 */
                static ip::managed_shared_memory* st_segment;
}

This class served to have some string in memory.

The following Element.h used it :

#include "ShmString.h"

class Element
{
        public:
                Element (const Element& elem);
                Element (const std::string dir, const std::string file, const std::string
type, const std::string newpath, const std::string newfile,
ip::managed_shared_memory * segment);
                virtual ~Element ();

        private:
                ShmString dir;
                ShmString file;
                ShmString target;
                ShmString type;
                ShmString newpath;
                ShmString newfile;
};

I have four attributes that declare it.

When I tried to define all the attributes in the constructor like this in
Element.cpp :

Element::Element (const string dir, const string file, const string type,
const string newpath, const string newfile, ip::managed_shared_memory
*segment){
        try{
                this->segment = segment;

                //test the type
                if(type == Request::st_rename){
                        this->type.assign(type.c_str());
                }
                else{
                        string res = "Class Element in constructor -> The type should be : rename
";
                        throw EventException(res.c_str(), __LINE__ );
                }
                this->dir.assign(dir.c_str());
                this->file.assign(file.c_str());
                this->target.assign("");
                this->newfile.assign(newfile.c_str());
                this->newpath.assign(newpath.c_str());
        }
        catch(ip::interprocess_exception &e){
                ostringstream oss;
                const std::string what(e.what());
                oss << "Class Element in constructor 3 -> ";
                oss << what;
                throw EventException(oss.str().c_str(), __LINE__ );
        }
}

If for example "const string dir " has the size more than 10 (the capacity
by default for a ShmString object), I obtained an error like this :

0x0807439f in boost::interprocess::basic_string,
boost::interprocess::allocator, 0u>, boost::interprocess::iset_index> >
>::assign (this=0xb6c4e274,
    s=0xb8af42d0 ) at
/usr/local/include/boost/interprocess/containers/string.hpp:988
#3 0x08071d26 in Element (this=0xb6c4e26c, elem=@0xb7fa224c) at
..Element.cpp:22

I tried to used some other constructors be 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 share 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/Problem-with-boost%3A%3Ainterprocess%3Abasic_string-%3A-Error-address-out-of-bounds-tp24645058p24645058.html
Sent from the Boost - Users mailing list archive at Nabble.com.



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net