c++ - Sharing class pointers between processes (UPDATED) -
i have server library client executable injects remote process. server's responsibility set sort of ipc/rpc implementation allow client seamlessly communicate remote process.
update
take @ following server-side header:
#include <boost/interprocess/managed_shared_memory.hpp> #include <boost/interprocess/mapped_region.hpp> #include <boost/interprocess/containers/vector.hpp> #include <boost/interprocess/containers/string.hpp> #include <boost/interprocess/allocators/allocator.hpp> using namespace boost::interprocess; typedef allocator<int, managed_shared_memory::segment_manager> shmintallocator; typedef vector<int, shmintallocator> intvector; class { public: a(); a(string str, offset_ptr<intvector> ints) : m_str(string(str)), m_ints(ints) {}; ~a(); string m_str; offset_ptr<intvector> m_ints; }; class b { public: b(); b(offset_ptr<a> obj) : m_obj(obj); ~b(); void dosomethinguseful() { messageboxa(null, m_obj->m_str.c_str(), "somethinguseful", mb_iconinformation); } offset_ptr<a> m_obj; };
and here's server-side implementation:
managed_shared_memory g_shm; offset_ptr<a> g_obja = nullptr; pvoid g_hmem = nullptr; bool startserver() // create shared memory pool try { g_shm = managed_shared_memory(create_only, "mysharedmem", ovr_mapsize); } catch(interprocess_exception& e) { std::string msg(e.what()); messageboxa(null, msg.c_str(), "error", mb_iconerror); return false; } // construct local class instance const shmintallocator alloc_intvector (g_shm.get_segment_manager()); offset_ptr<intvector> ints = g_shm.construct<intvector>(unique_instance)(alloc_intvector); ints->push_back(10); ints->push_back(20); ints->push_back(30); g_obja = new a("testing", ints); b objb(g_obja); // copy data shared memory size_t len = sizeof(objb); // <-- doesn't seem make difference if set higher g_hmem = g_shm.allocate(len); std::memcpy(g_hmem, &objb, len); return true; } void stopserver() { // free used resources if(g_obja) { delete g_obja; g_obja = nullptr; } try{ g_shm.destroy<b>(unique_instance); g_shm.deallocate(g_hmem); g_hmem = nullptr; shared_memory_object::remove("mysharedmem"); } catch(interprocess_exception& e) { std::string msg(e.what()); messageboxa(null, msg.c_str(), "error", mb_iconerror); } }
and client implementation:
bool connect() { // grab shared memory pool , extract class managed_shared_memory shm(open_only, "mysharedmem"); std::pair<b*, std::size_t> ret = shm.find<b>(unique_instance); // <-- ends being 0x00000000! b *objb = static_cast<b*>(ret.first); if(!objb) return false; objb->dosomethinguseful(); return true; }
you'll notice managed_shared_memory::find()
fails return valid pointer client. far can tell, code valid. there no compiler warnings or errors, , appears run smoothly until point.
so why failing? how can work expected?
you trying find b supposed created unique instance construction. boost documentation says
the find function obtains pointer object of type t can created using "unique instance" mechanism.
but in code allocating raw memory , copy b object. b not created unique instance
so suggest change code following: try use
b &objb = *g_shm.construct<b>(unique_instance) (g_obja);
instead of
b objb(g_obja);
Comments
Post a Comment