Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8949621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:15:39+00:00 2026-06-15T13:15:39+00:00

I haven’t used boost before, so forgive me if I am doing something silly.

  • 0

I haven’t used boost before, so forgive me if I am doing something silly. I have a class which holds a lua_State. I have a boost::shared_ptr vector which I push_back new states like so:

class Lua_State
{
lua_State *L;
std::string m_scr;

public:
Lua_State() : L(luaL_newstate())
{
    lua_register(L, "test", test);
    luaL_openlibs(L);
}

~Lua_State() {
    lua_close(L);
}

inline bool LoadScript(const char* script)
{
    if (!boost::filesystem::exists(script))
        return false;

    m_scr = fs::path(std::string(script)).filename().string();

    chdir(fs::path(scr).parent_path().string().c_str());

    if (luaL_loadfile(L, m_scr.c_str()))
        return false;

    // prime
    if (lua_pcall(L, 0, 0, 0))
        return false;

    return true;
}
};

typedef boost::shared_ptr<Lua_State> LUASTATEPTR;
class Scripts
{
private:
std::vector<LUASTATEPTR> m_Scripts;
public:
    Scripts() { }

    void TestLoad()
{
    m_Scripts.push_back(LUASTATEPTR(new Lua_State()));
    LUASTATEPTR pState = m_Scripts[0];
    pState->LoadScript("C:/test.lua");
}
};

The code works and the Lua state is added, but after a few seconds the application crashes. I am at a loss as to why this is happening. It works fine when I do it manually (without shared_ptrs and manually dereferencing).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T13:15:40+00:00Added an answer on June 15, 2026 at 1:15 pm

    You have violated the rule of 31. You created a non-trivial destructor and allocating constructor without disabling or writing a copy constructor and operator=.

    Probably when you create the shared_ptr you are doing a copy of the above class. The temporary is then discarded and things go boom.

    So, first disable LuaState::operator=(LuaState const&) and LuaState(LuaState const&) constructor (make a private non-implemented version, or in C++11 delete it), or implement it.

    Next, use make_shared<LuaState>() to create your shared_ptr<LuaState> instances. This will create them “in place” and remove the copy.

    1 What is this Rule of 3 I speak of? See these links: Rule of Three (Wikipedia), What is the Rule of Three?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I haven't used the STL much before, but I started to on this huffman
haven't used regex replaces much and am not sure if how I have done
Haven't seen this before so hopefully someone has a easy solve, I have a
I haven't used ARC yet other than to deal with it when it forces
I haven't used multithreading in Clojure at all so am unsure where to start.
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Haven't been programming in JS for a while. Now, I have following thing: <html>
Haven't done as much of Javascript as I should have done. I am trying
Haven't seen many Geneva related questions yet, I have posted this question in the
Haven't seen anything, but I have seen that you can create albums in the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.