cannot create a mongocxx::instance object if one has already been createdPretty-print C++ STL containersHow to implement the factory method pattern in C++ correctlycall_once with once_flag shared across DLLsIs legal use initializer_list to initialize an object with derived types?Is there a way to use a std::pair<> in a class constructorCreating an object via a lambda that captures variables(__imp_? Name decorationmove copy and/or assign a mongodb cxx cursorQt break down by MongoDB C++ initializationError while loading shared libraries: libbsoncxx.so._noabi: cannot open shared object file: No such file or directory

12V lead acid charger with LM317 not charging

Getting an entry level IT position later in life

What could prevent players from leaving an island?

Does the length of a password for Wi-Fi affect speed?

What is the German idiom or expression for when someone is being hypocritical against their own teachings?

Unexpected route on a flight from USA to Europe

Why don't the open notes matter in guitar chords?

How to continue a line in Latex in math mode?

Traveling from Germany to other countries by train?

Can I enter a rental property without giving notice if I'm afraid a tenant may be hurt?

Purchased new computer from DELL with pre-installed Ubuntu. Won't boot. Should assume its an error from DELL?

Is this cheap "air conditioner" able to cool a room?

Why can I log in to my Facebook account with a misspelled email/password?

Why do proponents of guns oppose gun competency tests?

Is DC heating faster than AC heating?

Is it a bad idea to offer variants of a final exam based on the type of allowed calculators?

Is Odin inconsistent about the powers of Mjolnir?

Will a paper be retracted if a flaw in released software code invalidates its central idea?

Determine Beckett Grading Service (BGS) Final Grade

How would a family travel from Indiana to Texas in 1911?

Our group keeps dying during the Lost Mine of Phandelver campaign. What are we doing wrong?

Decode a variable-length quantity

Does this smartphone photo show Mars just below the Sun?

How to halve redstone signal strength?



cannot create a mongocxx::instance object if one has already been created


Pretty-print C++ STL containersHow to implement the factory method pattern in C++ correctlycall_once with once_flag shared across DLLsIs legal use initializer_list to initialize an object with derived types?Is there a way to use a std::pair<> in a class constructorCreating an object via a lambda that captures variables(__imp_? Name decorationmove copy and/or assign a mongodb cxx cursorQt break down by MongoDB C++ initializationError while loading shared libraries: libbsoncxx.so._noabi: cannot open shared object file: No such file or directory






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








4















I made an class to invoke and test mongo-cxx-driver and I create an mongocxx::instance in my constructor.



MongoDBHelper::MongoDBHelper()

mongocxx::instance inst;



The problem is when I init the second instance of my class object, it turns out:



cannot create a mongocxx::instance object if one has already been created


it would be a little strange if I put mongocxx::instance in the global scope or use std::call_once. How can I fix it?










share|improve this question
























  • Why not make it a class static variable if global feels strange? It seems like it wants to be treated as a singleton instance.

    – kmdreko
    Mar 27 at 6:44











  • @kmdreko thanks

    – Peiwen_hao
    Mar 27 at 9:07

















4















I made an class to invoke and test mongo-cxx-driver and I create an mongocxx::instance in my constructor.



MongoDBHelper::MongoDBHelper()

mongocxx::instance inst;



The problem is when I init the second instance of my class object, it turns out:



cannot create a mongocxx::instance object if one has already been created


it would be a little strange if I put mongocxx::instance in the global scope or use std::call_once. How can I fix it?










share|improve this question
























  • Why not make it a class static variable if global feels strange? It seems like it wants to be treated as a singleton instance.

    – kmdreko
    Mar 27 at 6:44











  • @kmdreko thanks

    – Peiwen_hao
    Mar 27 at 9:07













4












4








4








I made an class to invoke and test mongo-cxx-driver and I create an mongocxx::instance in my constructor.



MongoDBHelper::MongoDBHelper()

mongocxx::instance inst;



The problem is when I init the second instance of my class object, it turns out:



cannot create a mongocxx::instance object if one has already been created


it would be a little strange if I put mongocxx::instance in the global scope or use std::call_once. How can I fix it?










share|improve this question














I made an class to invoke and test mongo-cxx-driver and I create an mongocxx::instance in my constructor.



MongoDBHelper::MongoDBHelper()

mongocxx::instance inst;



The problem is when I init the second instance of my class object, it turns out:



cannot create a mongocxx::instance object if one has already been created


it would be a little strange if I put mongocxx::instance in the global scope or use std::call_once. How can I fix it?







c++ mongodb






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 27 at 5:21









Peiwen_haoPeiwen_hao

212 bronze badges




212 bronze badges















  • Why not make it a class static variable if global feels strange? It seems like it wants to be treated as a singleton instance.

    – kmdreko
    Mar 27 at 6:44











  • @kmdreko thanks

    – Peiwen_hao
    Mar 27 at 9:07

















  • Why not make it a class static variable if global feels strange? It seems like it wants to be treated as a singleton instance.

    – kmdreko
    Mar 27 at 6:44











  • @kmdreko thanks

    – Peiwen_hao
    Mar 27 at 9:07
















Why not make it a class static variable if global feels strange? It seems like it wants to be treated as a singleton instance.

– kmdreko
Mar 27 at 6:44





Why not make it a class static variable if global feels strange? It seems like it wants to be treated as a singleton instance.

– kmdreko
Mar 27 at 6:44













@kmdreko thanks

– Peiwen_hao
Mar 27 at 9:07





@kmdreko thanks

– Peiwen_hao
Mar 27 at 9:07












1 Answer
1






active

oldest

votes


















3














from : http://mongocxx.org/api/current/classmongocxx_1_1instance.html




Class representing an instance of the MongoDB driver.



The constructor and destructor initialize and shut down the driver, respectively. Therefore, an instance must be created before using the driver and must remain alive until all other mongocxx objects are destroyed. After the instance destructor runs, the driver may not be used.



Exactly one instance must be created in a given program. Not constructing an instance or constructing more than one instance in a program are errors, even if the multiple instances have non-overlapping lifetimes.




It seems you can't have 2 instances, but maybe you can create/destroy one then recreate/destroy an other.



Where your first instance come from?






share|improve this answer




















  • 1





    "even if the multiple instances have non-overlapping lifetimes" means it won't let you recreate it

    – kmdreko
    Mar 27 at 7:38












  • Well, so static/singleton/global is forced by design

    – Martin Morterol
    Mar 27 at 7:55











  • Unfortunately, yes. Libraries that mongocxx depends on in turn enforce singleton initialization semantics, so there is little we can do. I would have much preferred to design a stateless library, but I was forced to compromise.

    – acm
    Mar 27 at 19:46






  • 1





    At one point, you actually could destroy and re-create the instance, but that feature was removed by a subsequent maintainer because apparently the underlying C driver can't handle that: github.com/mongodb/mongo-cxx-driver/commit/…

    – acm
    Mar 27 at 19:48











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%2f55370281%2fcannot-create-a-mongocxxinstance-object-if-one-has-already-been-created%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














from : http://mongocxx.org/api/current/classmongocxx_1_1instance.html




Class representing an instance of the MongoDB driver.



The constructor and destructor initialize and shut down the driver, respectively. Therefore, an instance must be created before using the driver and must remain alive until all other mongocxx objects are destroyed. After the instance destructor runs, the driver may not be used.



Exactly one instance must be created in a given program. Not constructing an instance or constructing more than one instance in a program are errors, even if the multiple instances have non-overlapping lifetimes.




It seems you can't have 2 instances, but maybe you can create/destroy one then recreate/destroy an other.



Where your first instance come from?






share|improve this answer




















  • 1





    "even if the multiple instances have non-overlapping lifetimes" means it won't let you recreate it

    – kmdreko
    Mar 27 at 7:38












  • Well, so static/singleton/global is forced by design

    – Martin Morterol
    Mar 27 at 7:55











  • Unfortunately, yes. Libraries that mongocxx depends on in turn enforce singleton initialization semantics, so there is little we can do. I would have much preferred to design a stateless library, but I was forced to compromise.

    – acm
    Mar 27 at 19:46






  • 1





    At one point, you actually could destroy and re-create the instance, but that feature was removed by a subsequent maintainer because apparently the underlying C driver can't handle that: github.com/mongodb/mongo-cxx-driver/commit/…

    – acm
    Mar 27 at 19:48
















3














from : http://mongocxx.org/api/current/classmongocxx_1_1instance.html




Class representing an instance of the MongoDB driver.



The constructor and destructor initialize and shut down the driver, respectively. Therefore, an instance must be created before using the driver and must remain alive until all other mongocxx objects are destroyed. After the instance destructor runs, the driver may not be used.



Exactly one instance must be created in a given program. Not constructing an instance or constructing more than one instance in a program are errors, even if the multiple instances have non-overlapping lifetimes.




It seems you can't have 2 instances, but maybe you can create/destroy one then recreate/destroy an other.



Where your first instance come from?






share|improve this answer




















  • 1





    "even if the multiple instances have non-overlapping lifetimes" means it won't let you recreate it

    – kmdreko
    Mar 27 at 7:38












  • Well, so static/singleton/global is forced by design

    – Martin Morterol
    Mar 27 at 7:55











  • Unfortunately, yes. Libraries that mongocxx depends on in turn enforce singleton initialization semantics, so there is little we can do. I would have much preferred to design a stateless library, but I was forced to compromise.

    – acm
    Mar 27 at 19:46






  • 1





    At one point, you actually could destroy and re-create the instance, but that feature was removed by a subsequent maintainer because apparently the underlying C driver can't handle that: github.com/mongodb/mongo-cxx-driver/commit/…

    – acm
    Mar 27 at 19:48














3












3








3







from : http://mongocxx.org/api/current/classmongocxx_1_1instance.html




Class representing an instance of the MongoDB driver.



The constructor and destructor initialize and shut down the driver, respectively. Therefore, an instance must be created before using the driver and must remain alive until all other mongocxx objects are destroyed. After the instance destructor runs, the driver may not be used.



Exactly one instance must be created in a given program. Not constructing an instance or constructing more than one instance in a program are errors, even if the multiple instances have non-overlapping lifetimes.




It seems you can't have 2 instances, but maybe you can create/destroy one then recreate/destroy an other.



Where your first instance come from?






share|improve this answer













from : http://mongocxx.org/api/current/classmongocxx_1_1instance.html




Class representing an instance of the MongoDB driver.



The constructor and destructor initialize and shut down the driver, respectively. Therefore, an instance must be created before using the driver and must remain alive until all other mongocxx objects are destroyed. After the instance destructor runs, the driver may not be used.



Exactly one instance must be created in a given program. Not constructing an instance or constructing more than one instance in a program are errors, even if the multiple instances have non-overlapping lifetimes.




It seems you can't have 2 instances, but maybe you can create/destroy one then recreate/destroy an other.



Where your first instance come from?







share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 27 at 6:48









Martin MorterolMartin Morterol

1,1311 gold badge2 silver badges8 bronze badges




1,1311 gold badge2 silver badges8 bronze badges










  • 1





    "even if the multiple instances have non-overlapping lifetimes" means it won't let you recreate it

    – kmdreko
    Mar 27 at 7:38












  • Well, so static/singleton/global is forced by design

    – Martin Morterol
    Mar 27 at 7:55











  • Unfortunately, yes. Libraries that mongocxx depends on in turn enforce singleton initialization semantics, so there is little we can do. I would have much preferred to design a stateless library, but I was forced to compromise.

    – acm
    Mar 27 at 19:46






  • 1





    At one point, you actually could destroy and re-create the instance, but that feature was removed by a subsequent maintainer because apparently the underlying C driver can't handle that: github.com/mongodb/mongo-cxx-driver/commit/…

    – acm
    Mar 27 at 19:48













  • 1





    "even if the multiple instances have non-overlapping lifetimes" means it won't let you recreate it

    – kmdreko
    Mar 27 at 7:38












  • Well, so static/singleton/global is forced by design

    – Martin Morterol
    Mar 27 at 7:55











  • Unfortunately, yes. Libraries that mongocxx depends on in turn enforce singleton initialization semantics, so there is little we can do. I would have much preferred to design a stateless library, but I was forced to compromise.

    – acm
    Mar 27 at 19:46






  • 1





    At one point, you actually could destroy and re-create the instance, but that feature was removed by a subsequent maintainer because apparently the underlying C driver can't handle that: github.com/mongodb/mongo-cxx-driver/commit/…

    – acm
    Mar 27 at 19:48








1




1





"even if the multiple instances have non-overlapping lifetimes" means it won't let you recreate it

– kmdreko
Mar 27 at 7:38






"even if the multiple instances have non-overlapping lifetimes" means it won't let you recreate it

– kmdreko
Mar 27 at 7:38














Well, so static/singleton/global is forced by design

– Martin Morterol
Mar 27 at 7:55





Well, so static/singleton/global is forced by design

– Martin Morterol
Mar 27 at 7:55













Unfortunately, yes. Libraries that mongocxx depends on in turn enforce singleton initialization semantics, so there is little we can do. I would have much preferred to design a stateless library, but I was forced to compromise.

– acm
Mar 27 at 19:46





Unfortunately, yes. Libraries that mongocxx depends on in turn enforce singleton initialization semantics, so there is little we can do. I would have much preferred to design a stateless library, but I was forced to compromise.

– acm
Mar 27 at 19:46




1




1





At one point, you actually could destroy and re-create the instance, but that feature was removed by a subsequent maintainer because apparently the underlying C driver can't handle that: github.com/mongodb/mongo-cxx-driver/commit/…

– acm
Mar 27 at 19:48






At one point, you actually could destroy and re-create the instance, but that feature was removed by a subsequent maintainer because apparently the underlying C driver can't handle that: github.com/mongodb/mongo-cxx-driver/commit/…

– acm
Mar 27 at 19:48









Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















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%2f55370281%2fcannot-create-a-mongocxxinstance-object-if-one-has-already-been-created%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

How to preserve electronics (computers, tablets and phones) for hundreds of years Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) The network's official Twitter account is up and running again. What content…How might modern humans leave a message for 50,000 years?How would an aquatic race develop computers?How do we make computers care for human life?Keeping supplies for 1000 yearsWhat will cell phones become in 100 years?What body armor protects against a laser?Could a society attain 1800s technology if limited to using copper and bronze?Electricity power (magic) and electronicsScavenging metal resources in post-apocalypse after 100 yearsWhy would precursors create devices that can survive and still work after hundreds of years?

155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해

Convert JSON to MapSimplest way to convert a string representing JSON array into Java array List of key value pairsHow to take String JSON values into HashMapConvert a JSON String to a HashMapCreating BSON object from JSON stringJSONObject - How to get a value ?Better Map ConstructorReading JSON from Url javaUpdate columns if input values are not null otherwise ignore and keep the existing values of column in databaseHow to store object represented as Json string into Couchbase Lite?Go JSON into MapHow do I efficiently iterate over each entry in a Java Map?Sort a Map<Key, Value> by valuesHow to round a number to n decimal places in JavaCan comments be used in JSON?How do I read / convert an InputStream into a String in Java?How can I pretty-print JSON in a shell script?What is the correct JSON content type?How can I deserialize JSON to a simple Dictionary<string,string> in ASP.NET?Why does Google prepend while(1); to their JSON responses?Parse JSON in JavaScript?