質問編集履歴
3
書式
    
        title	
    CHANGED
    
    | 
         
            File without changes
         
     | 
    
        body	
    CHANGED
    
    | 
         @@ -153,4 +153,4 @@ 
     | 
|
| 
       153 
153 
     | 
    
         
             
            }
         
     | 
| 
       154 
154 
     | 
    
         
             
            ```
         
     | 
| 
       155 
155 
     | 
    
         | 
| 
       156 
     | 
    
         
            -
            12/24 19:08に更新
         
     | 
| 
      
 156 
     | 
    
         
            +
            12/24 19:08 Chironianさんの回答元に更新しました。
         
     | 
2
コードの修正
    
        title	
    CHANGED
    
    | 
         
            File without changes
         
     | 
    
        body	
    CHANGED
    
    | 
         @@ -3,12 +3,17 @@ 
     | 
|
| 
       3 
3 
     | 
    
         | 
| 
       4 
4 
     | 
    
         
             
            しかし、問題があり、
         
     | 
| 
       5 
5 
     | 
    
         
             
            ```
         
     | 
| 
      
 6 
     | 
    
         
            +
                vcap >> mat;
         
     | 
| 
      
 7 
     | 
    
         
            +
             
     | 
| 
       6 
8 
     | 
    
         
             
                MatToBytes(mat, &pBuf );
         
     | 
| 
       7 
9 
     | 
    
         
             
                std::string sName;
         
     | 
| 
       8 
     | 
    
         
            -
                myshmvector->push_back(sName( 
     | 
| 
      
 10 
     | 
    
         
            +
                myshmvector->push_back(sName(pBuf));、
         
     | 
| 
       9 
11 
     | 
    
         
             
            ```
         
     | 
| 
       10 
12 
     | 
    
         
             
            というコードで、MatToBytesにMatと pBufというunsigned charの二重ポインタを引数でとり、格納し、memcpyしたいのですが、
         
     | 
| 
       11 
     | 
    
         
            -
            共有メモリのVectorにてStringで格納する際、 
     | 
| 
      
 13 
     | 
    
         
            +
            共有メモリのVectorにてStringで格納する際、
         
     | 
| 
      
 14 
     | 
    
         
            +
            コンパイルエラーでno match for call to ‘(std::string {aka std::basic_string<char>}) (uchar*&)’
         
     | 
| 
      
 15 
     | 
    
         
            +
               myshmvector->push_back(sName(pBuf));
         
     | 
| 
      
 16 
     | 
    
         
            +
             
     | 
| 
       12 
17 
     | 
    
         
             
            二重ポインタのせいなのか、std::stringがbasic_stringに合わないのかわかりません。
         
     | 
| 
       13 
18 
     | 
    
         | 
| 
       14 
19 
     | 
    
         
             
            どなたかご存じありませんか。どうかよろしくお願いします。
         
     | 
| 
         @@ -17,127 +22,135 @@ 
     | 
|
| 
       17 
22 
     | 
    
         
             
            (追記)
         
     | 
| 
       18 
23 
     | 
    
         
             
            子プロセスのイテレータのも問題があるようで
         
     | 
| 
       19 
24 
     | 
    
         
             
            ```
         
     | 
| 
      
 25 
     | 
    
         
            +
                  MyShmStringVector::iterator it;
         
     | 
| 
      
 26 
     | 
    
         
            +
                  cv::Mat mat;
         
     | 
| 
      
 27 
     | 
    
         
            +
                  cv::VideoCapture vcap(0);
         
     | 
| 
      
 28 
     | 
    
         
            +
                  uchar* pBuf = NULL;
         
     | 
| 
      
 29 
     | 
    
         
            +
             
     | 
| 
       20 
     | 
    
         
            -
             
     | 
| 
      
 30 
     | 
    
         
            +
                  for(it = myvector->begin(); it !=myvector->end(); ++it){
         
     | 
| 
       21 
     | 
    
         
            -
             
     | 
| 
      
 31 
     | 
    
         
            +
                        pBuf = (unsigned char*)(*it).c_str();
         
     | 
| 
       22 
     | 
    
         
            -
             
     | 
| 
      
 32 
     | 
    
         
            +
                        std::memcpy(mat, pBuf, it->size());
         
     | 
| 
       23 
     | 
    
         
            -
             
     | 
| 
      
 33 
     | 
    
         
            +
                        cv::imshow("window1", mat);
         
     | 
| 
       24 
     | 
    
         
            -
             
     | 
| 
      
 34 
     | 
    
         
            +
                  }
         
     | 
| 
       25 
35 
     | 
    
         
             
            ```
         
     | 
| 
       26 
     | 
    
         
            -
             
     | 
| 
      
 36 
     | 
    
         
            +
            エラーでは
         
     | 
| 
      
 37 
     | 
    
         
            +
            ‘cv::Mat’ to ‘void*’ for argument ‘1’ to ‘void* memcpy(void*, const void*, size_t)’
         
     | 
| 
       27 
     | 
    
         
            -
            it 
     | 
| 
      
 38 
     | 
    
         
            +
                std::memcpy(mat, pBuf, it->size());
         
     | 
| 
      
 39 
     | 
    
         
            +
            が出力されます。
         
     | 
| 
       28 
40 
     | 
    
         
             
            ごちゃごちゃしていてわからないと思いますが、ご教授の方お願いします。
         
     | 
| 
       29 
41 
     | 
    
         | 
| 
       30 
42 
     | 
    
         
             
            全体のコードになります。(解りずらいと思いますが、説明下手と状況把握が不十分なので)
         
     | 
| 
       31 
43 
     | 
    
         
             
            ```
         
     | 
| 
       32 
     | 
    
         
            -
             
     | 
| 
      
 44 
     | 
    
         
            +
            #include <boost/interprocess/managed_shared_memory.hpp>
         
     | 
| 
       33 
     | 
    
         
            -
             
     | 
| 
      
 45 
     | 
    
         
            +
            #include <boost/interprocess/containers/vector.hpp>
         
     | 
| 
       34 
     | 
    
         
            -
             
     | 
| 
      
 46 
     | 
    
         
            +
            #include <boost/interprocess/containers/string.hpp>
         
     | 
| 
       35 
     | 
    
         
            -
             
     | 
| 
      
 47 
     | 
    
         
            +
            #include <boost/interprocess/allocators/allocator.hpp>
         
     | 
| 
       36 
     | 
    
         
            -
             
     | 
| 
      
 48 
     | 
    
         
            +
            #include <string>
         
     | 
| 
       37 
     | 
    
         
            -
             
     | 
| 
      
 49 
     | 
    
         
            +
            #include <opencv2/core/core.hpp>
         
     | 
| 
       38 
     | 
    
         
            -
             
     | 
| 
      
 50 
     | 
    
         
            +
            #include <opencv2/highgui/highgui.hpp>
         
     | 
| 
       39 
     | 
    
         
            -
             
     | 
| 
      
 51 
     | 
    
         
            +
            #include <cstdlib> // std::system
         
     | 
| 
       40 
     | 
    
         
            -
             
     | 
| 
      
 52 
     | 
    
         
            +
             
     | 
| 
       41 
     | 
    
         
            -
             
     | 
| 
      
 53 
     | 
    
         
            +
            using namespace boost::interprocess;
         
     | 
| 
       42 
     | 
    
         
            -
             
     | 
| 
      
 54 
     | 
    
         
            +
             
     | 
| 
       43 
     | 
    
         
            -
             
     | 
| 
      
 55 
     | 
    
         
            +
            typedef unsigned char uchar;
         
     | 
| 
       44 
     | 
    
         
            -
             
     | 
| 
      
 56 
     | 
    
         
            +
             
     | 
| 
       45 
     | 
    
         
            -
             
     | 
| 
      
 57 
     | 
    
         
            +
            typedef allocator<char, managed_shared_memory::segment_manager> CharAllocator;
         
     | 
| 
       46 
     | 
    
         
            -
             
     | 
| 
      
 58 
     | 
    
         
            +
             
     | 
| 
       47 
     | 
    
         
            -
             
     | 
| 
      
 59 
     | 
    
         
            +
            typedef basic_string<char, std::char_traits<char>, CharAllocator> MyShmString;
         
     | 
| 
       48 
     | 
    
         
            -
             
     | 
| 
      
 60 
     | 
    
         
            +
             
     | 
| 
       49 
     | 
    
         
            -
             
     | 
| 
      
 61 
     | 
    
         
            +
            typedef allocator<MyShmString, managed_shared_memory::segment_manager> StringAllocator;
         
     | 
| 
       50 
     | 
    
         
            -
             
     | 
| 
      
 62 
     | 
    
         
            +
             
     | 
| 
       51 
     | 
    
         
            -
             
     | 
| 
      
 63 
     | 
    
         
            +
            typedef vector<MyShmString, StringAllocator> MyShmStringVector;
         
     | 
| 
       52 
     | 
    
         
            -
             
     | 
| 
      
 64 
     | 
    
         
            +
             
     | 
| 
       53 
     | 
    
         
            -
             
     | 
| 
      
 65 
     | 
    
         
            +
            int MatToBytes(cv::Mat image, uchar ** pimage_uchar)
         
     | 
| 
      
 66 
     | 
    
         
            +
            {
         
     | 
| 
      
 67 
     | 
    
         
            +
                uchar * image_uchar = * pimage_uchar;
         
     | 
| 
      
 68 
     | 
    
         
            +
                // class data members of ints
         
     | 
| 
      
 69 
     | 
    
         
            +
                int image_rows = image.rows;
         
     | 
| 
      
 70 
     | 
    
         
            +
                int image_cols = image.cols;
         
     | 
| 
      
 71 
     | 
    
         
            +
                int image_type = image.type();
         
     | 
| 
      
 72 
     | 
    
         
            +
             
     | 
| 
      
 73 
     | 
    
         
            +
                int image_size = image.total() * image.elemSize();
         
     | 
| 
      
 74 
     | 
    
         
            +
                image_uchar = new uchar[image_size];
         
     | 
| 
      
 75 
     | 
    
         
            +
                std::memcpy(image_uchar, image.data, image_size * sizeof(uchar));
         
     | 
| 
      
 76 
     | 
    
         
            +
                return 1;
         
     | 
| 
      
 77 
     | 
    
         
            +
            }
         
     | 
| 
      
 78 
     | 
    
         
            +
             
     | 
| 
      
 79 
     | 
    
         
            +
            int main(int argc, char* argv[])
         
     | 
| 
      
 80 
     | 
    
         
            +
            {
         
     | 
| 
      
 81 
     | 
    
         
            +
             
     | 
| 
      
 82 
     | 
    
         
            +
                struct shm_remove
         
     | 
| 
       54 
83 
     | 
    
         
             
                {
         
     | 
| 
      
 84 
     | 
    
         
            +
                    shm_remove(){shared_memory_object::remove("MySharedMemory");}
         
     | 
| 
      
 85 
     | 
    
         
            +
                    ~shm_remove(){shared_memory_object::remove("MySharedMemory");}
         
     | 
| 
      
 86 
     | 
    
         
            +
                }remover;
         
     | 
| 
      
 87 
     | 
    
         
            +
             
     | 
| 
      
 88 
     | 
    
         
            +
             
     | 
| 
      
 89 
     | 
    
         
            +
                if(argc == 1){ //Parent process
         
     | 
| 
      
 90 
     | 
    
         
            +
                    // Remove shared memory on construction and destruction
         
     | 
| 
      
 91 
     | 
    
         
            +
             
     | 
| 
      
 92 
     | 
    
         
            +
                // Create a new segment with given name and size
         
     | 
| 
      
 93 
     | 
    
         
            +
                managed_shared_memory segment(create_only,"MySharedMemory",65536);
         
     | 
| 
      
 94 
     | 
    
         
            +
             
     | 
| 
      
 95 
     | 
    
         
            +
                //Initialize shared memory STL-compatible allocator
         
     | 
| 
      
 96 
     | 
    
         
            +
                CharAllocator charallocator(segment.get_segment_manager());
         
     | 
| 
      
 97 
     | 
    
         
            +
                StringAllocator stringallocator(segment.get_segment_manager());
         
     | 
| 
      
 98 
     | 
    
         
            +
                //Construct a vector named "MyVector" in shared memory with argument alloc_inst
         
     | 
| 
      
 99 
     | 
    
         
            +
                MyShmStringVector *myshmvector = segment.construct<MyShmStringVector>("MyVector")(stringallocator);
         
     | 
| 
      
 100 
     | 
    
         
            +
             
     | 
| 
      
 101 
     | 
    
         
            +
             
     | 
| 
      
 102 
     | 
    
         
            +
                cv::Mat InputImage;
         
     | 
| 
      
 103 
     | 
    
         
            +
                cv::Mat mat;
         
     | 
| 
      
 104 
     | 
    
         
            +
                cv::VideoCapture vcap(0);
         
     | 
| 
      
 105 
     | 
    
         
            +
                uchar* pBuf = NULL;
         
     | 
| 
      
 106 
     | 
    
         
            +
             
     | 
| 
      
 107 
     | 
    
         
            +
             
     | 
| 
      
 108 
     | 
    
         
            +
                if (!vcap.isOpened())
         
     | 
| 
      
 109 
     | 
    
         
            +
                    return -1;
         
     | 
| 
      
 110 
     | 
    
         
            +
             
     | 
| 
      
 111 
     | 
    
         
            +
                while (1) {
         
     | 
| 
      
 112 
     | 
    
         
            +
                    vcap >> mat;
         
     | 
| 
      
 113 
     | 
    
         
            +
             
     | 
| 
      
 114 
     | 
    
         
            +
                    MatToBytes(mat, &pBuf );
         
     | 
| 
      
 115 
     | 
    
         
            +
                    std::string sName;
         
     | 
| 
       55 
     | 
    
         
            -
                     
     | 
| 
      
 116 
     | 
    
         
            +
                    myshmvector->push_back(sName(pBuf));
         
     | 
| 
      
 117 
     | 
    
         
            +
                    //Launch child process
         
     | 
| 
      
 118 
     | 
    
         
            +
                    if(cv::waitKey(30) >= 0) break;
         
     | 
| 
      
 119 
     | 
    
         
            +
                }
         
     | 
| 
       56 
     | 
    
         
            -
             
     | 
| 
      
 120 
     | 
    
         
            +
                // Launch child process
         
     | 
| 
       57 
     | 
    
         
            -
                    int image_rows = image.rows;
         
     | 
| 
       58 
     | 
    
         
            -
             
     | 
| 
      
 121 
     | 
    
         
            +
                std::string s(argv[0]); s += " child";
         
     | 
| 
       59 
     | 
    
         
            -
             
     | 
| 
      
 122 
     | 
    
         
            +
                if(0 != std::system(s.c_str()))
         
     | 
| 
       60 
     | 
    
         
            -
                 
         
     | 
| 
       61 
     | 
    
         
            -
                    int image_size = image.total() * image.elemSize();
         
     | 
| 
       62 
     | 
    
         
            -
                    image_uchar = new uchar[image_size];
         
     | 
| 
       63 
     | 
    
         
            -
                    std::memcpy(image_uchar, image.data, image_size * sizeof(uchar));
         
     | 
| 
       64 
123 
     | 
    
         
             
                    return 1;
         
     | 
| 
      
 124 
     | 
    
         
            +
             
     | 
| 
      
 125 
     | 
    
         
            +
                // check child has destroyed the vector
         
     | 
| 
      
 126 
     | 
    
         
            +
                if(segment.find<MyShmStringVector>("MyVector").first)
         
     | 
| 
      
 127 
     | 
    
         
            +
                    return 1;
         
     | 
| 
       65 
128 
     | 
    
         
             
                }
         
     | 
| 
       66 
     | 
    
         
            -
                 
         
     | 
| 
       67 
     | 
    
         
            -
                 
         
     | 
| 
       68 
     | 
    
         
            -
                 
         
     | 
| 
       69 
     | 
    
         
            -
                int main(int argc, char* argv[])
         
     | 
| 
       70 
     | 
    
         
            -
                {
         
     | 
| 
       71 
     | 
    
         
            -
             
     | 
| 
      
 129 
     | 
    
         
            +
                else{// child process
         
     | 
| 
       72 
     | 
    
         
            -
             
     | 
| 
      
 130 
     | 
    
         
            +
                    // Open the managed segment
         
     | 
| 
      
 131 
     | 
    
         
            +
                    managed_shared_memory segment(open_only, "MySharedMemory");
         
     | 
| 
       73 
132 
     | 
    
         | 
| 
       74 
     | 
    
         
            -
                        /* 初期化など*/
         
     | 
| 
       75 
     | 
    
         
            -
                        struct shm_remove
         
     | 
| 
       76 
     | 
    
         
            -
                        {
         
     | 
| 
       77 
     | 
    
         
            -
                            shm_remove(){shared_memory_object::remove("MySharedMemory");}
         
     | 
| 
       78 
     | 
    
         
            -
                            ~shm_remove(){shared_memory_object::remove("MySharedMemory");}
         
     | 
| 
       79 
     | 
    
         
            -
                        }remover;
         
     | 
| 
       80 
     | 
    
         
            -
                 
         
     | 
| 
       81 
     | 
    
         
            -
                    //  
     | 
| 
      
 133 
     | 
    
         
            +
                    // Find the vector using c-string name
         
     | 
| 
       82 
     | 
    
         
            -
                    managed_shared_memory segment(create_only,"MySharedMemory",65536);
         
     | 
| 
       83 
     | 
    
         
            -
                     
         
     | 
| 
       84 
     | 
    
         
            -
                    //Initialize shared memory STL-compatible allocator
         
     | 
| 
       85 
     | 
    
         
            -
                    CharAllocator charallocator(segment.get_segment_manager());
         
     | 
| 
       86 
     | 
    
         
            -
                    StringAllocator stringallocator(segment.get_segment_manager());
         
     | 
| 
       87 
     | 
    
         
            -
                    //Construct a vector named "MyVector" in shared memory with argument alloc_inst
         
     | 
| 
       88 
     | 
    
         
            -
                    MyShmStringVector * 
     | 
| 
      
 134 
     | 
    
         
            +
                    MyShmStringVector *myvector = segment.find<MyShmStringVector>("MyVector").first;
         
     | 
| 
      
 135 
     | 
    
         
            +
             
     | 
| 
      
 136 
     | 
    
         
            +
                    // Use vector in reverse order
         
     | 
| 
      
 137 
     | 
    
         
            +
             
     | 
| 
       89 
     | 
    
         
            -
                     
     | 
| 
      
 138 
     | 
    
         
            +
                    MyShmStringVector::iterator it;
         
     | 
| 
       90 
139 
     | 
    
         
             
                    cv::Mat mat;
         
     | 
| 
       91 
140 
     | 
    
         
             
                    cv::VideoCapture vcap(0);
         
     | 
| 
       92 
141 
     | 
    
         
             
                    uchar* pBuf = NULL;
         
     | 
| 
       93 
     | 
    
         
            -
                 
         
     | 
| 
       94 
     | 
    
         
            -
                    /* 初期化終了 */
         
     | 
| 
       95 
142 
     | 
    
         | 
| 
      
 143 
     | 
    
         
            +
                    for(it = myvector->begin(); it !=myvector->end(); ++it){
         
     | 
| 
      
 144 
     | 
    
         
            +
                        pBuf = (unsigned char*)(*it).c_str();
         
     | 
| 
      
 145 
     | 
    
         
            +
                        std::memcpy(mat, pBuf, it->size());
         
     | 
| 
      
 146 
     | 
    
         
            +
                        cv::imshow("window1", mat);
         
     | 
| 
      
 147 
     | 
    
         
            +
                    }
         
     | 
| 
       96 
148 
     | 
    
         | 
| 
       97 
     | 
    
         
            -
                    if (!vcap.isOpened()) // もちろんカメラは開いてる
         
     | 
| 
       98 
     | 
    
         
            -
                        return -1;
         
     | 
| 
       99 
     | 
    
         
            -
                 
         
     | 
| 
       100 
     | 
    
         
            -
                    // キーボード押すまでMatを取り続けるんだ
         
     | 
| 
       101 
     | 
    
         
            -
                    while (1) {
         
     | 
| 
       102 
     | 
    
         
            -
                        vcap >> mat;
         
     | 
| 
       103 
     | 
    
         
            -
                         
         
     | 
| 
       104 
     | 
    
         
            -
                        MatToBytes(mat, &pBuf );
         
     | 
| 
       105 
     | 
    
         
            -
                        std::string sName;
         
     | 
| 
       106 
     | 
    
         
            -
                        myshmvector->push_back(sName(*pBuf)); <-- ココがエラー
         
     | 
| 
       107 
     | 
    
         
            -
                        if(cv::waitKey(30) >= 0) break;
         
     | 
| 
       108 
     | 
    
         
            -
                    }
         
     | 
| 
       109 
     | 
    
         
            -
                    // Launch child process
         
     | 
| 
       110 
     | 
    
         
            -
                    std::string s(argv[0]); s += " child";
         
     | 
| 
       111 
     | 
    
         
            -
                    if(0 != std::system(s.c_str()))
         
     | 
| 
       112 
     | 
    
         
            -
                        return 1;
         
     | 
| 
       113 
     | 
    
         
            -
                 
         
     | 
| 
       114 
     | 
    
         
            -
                    // check child has destroyed the vector
         
     | 
| 
       115 
     | 
    
         
            -
                    if(segment.find<MyShmStringVector>("MyVector").first)
         
     | 
| 
       116 
     | 
    
         
            -
                        return 1;
         
     | 
| 
       117 
     | 
    
         
            -
                    }
         
     | 
| 
       118 
     | 
    
         
            -
                    else{// child process
         
     | 
| 
       119 
     | 
    
         
            -
                        // Open the managed segment
         
     | 
| 
       120 
     | 
    
         
            -
                        managed_shared_memory segment(open_only, "MySharedMemory");
         
     | 
| 
       121 
     | 
    
         
            -
                 
         
     | 
| 
       122 
     | 
    
         
            -
                        // Find the vector using c-string name
         
     | 
| 
       123 
     | 
    
         
            -
                        MyShmStringVector *myvector = segment.find<MyShmStringVector>("MyVector").first;
         
     | 
| 
       124 
     | 
    
         
            -
                 
         
     | 
| 
       125 
     | 
    
         
            -
                        // Use vector in reverse order
         
     | 
| 
       126 
     | 
    
         
            -
                 
         
     | 
| 
       127 
     | 
    
         
            -
                        MyShmStringVector::iterator it;
         
     | 
| 
       128 
     | 
    
         
            -
                        cv::Mat mat;
         
     | 
| 
       129 
     | 
    
         
            -
                        cv::VideoCapture vcap(0);
         
     | 
| 
       130 
     | 
    
         
            -
                        uchar* pBuf = NULL;
         
     | 
| 
       131 
     | 
    
         
            -
                 
         
     | 
| 
       132 
     | 
    
         
            -
                        for(it = myvector->begin(); it !=myvector->end(); ++it){
         
     | 
| 
       133 
     | 
    
         
            -
                            pBuf = (unsigned char*)*it.c_str();
         
     | 
| 
       134 
     | 
    
         
            -
                            std::memcpy(mat, pBuf, *it->size());
         
     | 
| 
       135 
     | 
    
         
            -
                            cv::imshow("window1", mat);
         
     | 
| 
       136 
     | 
    
         
            -
                        }
         
     | 
| 
       137 
     | 
    
         
            -
                 
         
     | 
| 
       138 
     | 
    
         
            -
             
     | 
| 
      
 149 
     | 
    
         
            +
                    segment.destroy<MyShmStringVector>("MyVector");
         
     | 
| 
       139 
     | 
    
         
            -
                 
         
     | 
| 
       140 
     | 
    
         
            -
             
     | 
| 
      
 150 
     | 
    
         
            +
             
     | 
| 
       141 
     | 
    
         
            -
                    return 0;
         
     | 
| 
       142 
151 
     | 
    
         
             
                }
         
     | 
| 
      
 152 
     | 
    
         
            +
                return 0;
         
     | 
| 
      
 153 
     | 
    
         
            +
            }
         
     | 
| 
       143 
     | 
    
         
            -
            ```
         
     | 
| 
      
 154 
     | 
    
         
            +
            ```
         
     | 
| 
      
 155 
     | 
    
         
            +
             
     | 
| 
      
 156 
     | 
    
         
            +
            12/24 19:08に更新
         
     | 
1
初心者マーク
    
        title	
    CHANGED
    
    | 
         
            File without changes
         
     | 
    
        body	
    CHANGED
    
    | 
         
            File without changes
         
     |