Modifying an object in Boost shared_memory












0















I'm learning shared memory of boost to apply it to my OpenCV project.
These are two snippets for testing.



class.h



#ifndef _class_H_
#define _class_H_
class Myclass{
public:
Myclass() : number(0){}
int number;
};
#endif


write.cpp



#include <boost/interprocess/managed_shared_memory.hpp>
#include <cstdlib>
#include <iostream>
#include "include/class.h"

int main(int argc, char *argv){
using namespace boost::interprocess;
struct shm_remove{
shm_remove() { shared_memory_object::remove("MySharedMemory"); }
~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }
} remover;
managed_shared_memory segment(create_only, "MySharedMemory", 65536);
Myclass *instance = segment.construct<Myclass>("instance1")();
instance = new Myclass();
std::system("read.exe");
std::cout << instance->number << std::endl;
return 0;
}


read.cpp



#include <boost/interprocess/managed_shared_memory.hpp>
#include <cstdlib>
#include <iostream>
#include "include/class.h"

int main(int argc, char *argv){
using namespace boost::interprocess;
managed_shared_memory segment(open_only, "MySharedMemory");
std::pair<Myclass*, managed_shared_memory::size_type> res;
res = segment.find<Myclass> ("instance1");
res.first->number = 3;
return 0;
}


Surely the output must be 3, but it is 0, which means that the class is not modified.
How can I modify an object in shared memory?










share|improve this question

























  • You are not using the instance created in the shared memory, instead you are creating a local one and outputting its value (instance = new Myclass();).

    – user10605163
    Nov 24 '18 at 16:04













  • There's not a single line of OpenCV code here. Please, don't use irrelevant tags.

    – Dan Mašek
    Nov 24 '18 at 16:52











  • @DanMašek My mistake. I forgot to remove it

    – Wang chen
    Nov 24 '18 at 17:15











  • @eukaryota I made a silly mistake. Thanks!

    – Wang chen
    Nov 24 '18 at 17:20
















0















I'm learning shared memory of boost to apply it to my OpenCV project.
These are two snippets for testing.



class.h



#ifndef _class_H_
#define _class_H_
class Myclass{
public:
Myclass() : number(0){}
int number;
};
#endif


write.cpp



#include <boost/interprocess/managed_shared_memory.hpp>
#include <cstdlib>
#include <iostream>
#include "include/class.h"

int main(int argc, char *argv){
using namespace boost::interprocess;
struct shm_remove{
shm_remove() { shared_memory_object::remove("MySharedMemory"); }
~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }
} remover;
managed_shared_memory segment(create_only, "MySharedMemory", 65536);
Myclass *instance = segment.construct<Myclass>("instance1")();
instance = new Myclass();
std::system("read.exe");
std::cout << instance->number << std::endl;
return 0;
}


read.cpp



#include <boost/interprocess/managed_shared_memory.hpp>
#include <cstdlib>
#include <iostream>
#include "include/class.h"

int main(int argc, char *argv){
using namespace boost::interprocess;
managed_shared_memory segment(open_only, "MySharedMemory");
std::pair<Myclass*, managed_shared_memory::size_type> res;
res = segment.find<Myclass> ("instance1");
res.first->number = 3;
return 0;
}


Surely the output must be 3, but it is 0, which means that the class is not modified.
How can I modify an object in shared memory?










share|improve this question

























  • You are not using the instance created in the shared memory, instead you are creating a local one and outputting its value (instance = new Myclass();).

    – user10605163
    Nov 24 '18 at 16:04













  • There's not a single line of OpenCV code here. Please, don't use irrelevant tags.

    – Dan Mašek
    Nov 24 '18 at 16:52











  • @DanMašek My mistake. I forgot to remove it

    – Wang chen
    Nov 24 '18 at 17:15











  • @eukaryota I made a silly mistake. Thanks!

    – Wang chen
    Nov 24 '18 at 17:20














0












0








0








I'm learning shared memory of boost to apply it to my OpenCV project.
These are two snippets for testing.



class.h



#ifndef _class_H_
#define _class_H_
class Myclass{
public:
Myclass() : number(0){}
int number;
};
#endif


write.cpp



#include <boost/interprocess/managed_shared_memory.hpp>
#include <cstdlib>
#include <iostream>
#include "include/class.h"

int main(int argc, char *argv){
using namespace boost::interprocess;
struct shm_remove{
shm_remove() { shared_memory_object::remove("MySharedMemory"); }
~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }
} remover;
managed_shared_memory segment(create_only, "MySharedMemory", 65536);
Myclass *instance = segment.construct<Myclass>("instance1")();
instance = new Myclass();
std::system("read.exe");
std::cout << instance->number << std::endl;
return 0;
}


read.cpp



#include <boost/interprocess/managed_shared_memory.hpp>
#include <cstdlib>
#include <iostream>
#include "include/class.h"

int main(int argc, char *argv){
using namespace boost::interprocess;
managed_shared_memory segment(open_only, "MySharedMemory");
std::pair<Myclass*, managed_shared_memory::size_type> res;
res = segment.find<Myclass> ("instance1");
res.first->number = 3;
return 0;
}


Surely the output must be 3, but it is 0, which means that the class is not modified.
How can I modify an object in shared memory?










share|improve this question
















I'm learning shared memory of boost to apply it to my OpenCV project.
These are two snippets for testing.



class.h



#ifndef _class_H_
#define _class_H_
class Myclass{
public:
Myclass() : number(0){}
int number;
};
#endif


write.cpp



#include <boost/interprocess/managed_shared_memory.hpp>
#include <cstdlib>
#include <iostream>
#include "include/class.h"

int main(int argc, char *argv){
using namespace boost::interprocess;
struct shm_remove{
shm_remove() { shared_memory_object::remove("MySharedMemory"); }
~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }
} remover;
managed_shared_memory segment(create_only, "MySharedMemory", 65536);
Myclass *instance = segment.construct<Myclass>("instance1")();
instance = new Myclass();
std::system("read.exe");
std::cout << instance->number << std::endl;
return 0;
}


read.cpp



#include <boost/interprocess/managed_shared_memory.hpp>
#include <cstdlib>
#include <iostream>
#include "include/class.h"

int main(int argc, char *argv){
using namespace boost::interprocess;
managed_shared_memory segment(open_only, "MySharedMemory");
std::pair<Myclass*, managed_shared_memory::size_type> res;
res = segment.find<Myclass> ("instance1");
res.first->number = 3;
return 0;
}


Surely the output must be 3, but it is 0, which means that the class is not modified.
How can I modify an object in shared memory?







c++ boost-interprocess






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 24 '18 at 17:15









user10605163

2,838624




2,838624










asked Nov 24 '18 at 15:58









Wang chenWang chen

11




11













  • You are not using the instance created in the shared memory, instead you are creating a local one and outputting its value (instance = new Myclass();).

    – user10605163
    Nov 24 '18 at 16:04













  • There's not a single line of OpenCV code here. Please, don't use irrelevant tags.

    – Dan Mašek
    Nov 24 '18 at 16:52











  • @DanMašek My mistake. I forgot to remove it

    – Wang chen
    Nov 24 '18 at 17:15











  • @eukaryota I made a silly mistake. Thanks!

    – Wang chen
    Nov 24 '18 at 17:20



















  • You are not using the instance created in the shared memory, instead you are creating a local one and outputting its value (instance = new Myclass();).

    – user10605163
    Nov 24 '18 at 16:04













  • There's not a single line of OpenCV code here. Please, don't use irrelevant tags.

    – Dan Mašek
    Nov 24 '18 at 16:52











  • @DanMašek My mistake. I forgot to remove it

    – Wang chen
    Nov 24 '18 at 17:15











  • @eukaryota I made a silly mistake. Thanks!

    – Wang chen
    Nov 24 '18 at 17:20

















You are not using the instance created in the shared memory, instead you are creating a local one and outputting its value (instance = new Myclass();).

– user10605163
Nov 24 '18 at 16:04







You are not using the instance created in the shared memory, instead you are creating a local one and outputting its value (instance = new Myclass();).

– user10605163
Nov 24 '18 at 16:04















There's not a single line of OpenCV code here. Please, don't use irrelevant tags.

– Dan Mašek
Nov 24 '18 at 16:52





There's not a single line of OpenCV code here. Please, don't use irrelevant tags.

– Dan Mašek
Nov 24 '18 at 16:52













@DanMašek My mistake. I forgot to remove it

– Wang chen
Nov 24 '18 at 17:15





@DanMašek My mistake. I forgot to remove it

– Wang chen
Nov 24 '18 at 17:15













@eukaryota I made a silly mistake. Thanks!

– Wang chen
Nov 24 '18 at 17:20





@eukaryota I made a silly mistake. Thanks!

– Wang chen
Nov 24 '18 at 17:20












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53459889%2fmodifying-an-object-in-boost-shared-memory%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53459889%2fmodifying-an-object-in-boost-shared-memory%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Contact image not getting when fetch all contact list from iPhone by CNContact

count number of partitions of a set with n elements into k subsets

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks