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 6557769
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:07:00+00:00 2026-05-25T13:07:00+00:00

I got a strange problem while used luabind to return a stl::vector::iterator to lua

  • 0

I got a strange problem while used luabind to return a stl::vector::iterator to lua script.

Below is the code:

1) I created two function they are called by lua script:

std::vector<car*> get_car_list()
{
    std::vector<car*>* vec = new std::vector<car*>();
    vec->push_back(new car("I'm the 1st"));
    vec->push_back(new car("I'm the 2nd")); 
    return *vec;
}

void output(const std::string& msg)
{
    std::cout << "lua:" << msg << std::endl;
}

2) I bind the function to lua

luabind::module(L)
[
    luabind::def("get_car_list", &get_car_list, luabind::return_stl_iterator)
];

luabind::module(L)
[
    luabind::def("output", &output)
];

3) I do the script like below:

function test()
    items  = get_car_list();
    for item in items do
        output(item:get_name());
    end
end

4) The result is:
In the output window, It only show:

lua:I'm the 1st

And the program is break in the luabind/policy.hpp:754

template <>
struct default_converter<std::string>
  : native_converter_base<std::string>
{
    .....

    void to(lua_State* L, std::string const& value)
    {
        lua_pushlstring(L, value.data(), value.size()); // !!Break Here with Error EXC_BAD_ACCESS
    }
};

I want to display all the elements in the std::vector, but it only show the first one and crash.

Thanks you very much! 🙂

Jason

  • 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-05-25T13:07:00+00:00Added an answer on May 25, 2026 at 1:07 pm

    I see two problems:

    You use pointers and new like if we were in Java but it’s C++. You will have clear memory leaks if you use C++ this way.

    Except if you have particular reasons, it should be:

    std::vector<car> get_car_list() {
        std::vector<car> vec;
        vec->push_back( car("I'm the 1st"));
        vec->push_back( car("I'm the 2nd")); 
        return vec; }
    

    But enters the second problem with your code:

    I seems return_stl_iterator assumes that the stl container still exists when you use it and only stores the iterator to this container.

    You then can’t return a copy of a container the way you do because the container will not exist anymore when you want to use the iterator. It is like if you’re using a reference to a temporary container.

    As seen in this example luabind doc the idea with return_stl_iterator is to have a container that is still accessible. In the example, the container exists in a struct. It is not a temporary.

    You might be tempted to allocate the vector with new and return a reference to this vector in your get_car_list function. But don’t do this: when will you free your container then?

    If you want to return a vector that does not exist somewhere else ( a temporary copy of a vector), then you should not use the return_stl_iterator policy, it seems not made for this.

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

Sidebar

Related Questions

I've got a strange problem. I created a custom form element. I created it
I've got a strange problem with SQL Server 2000, and I just can't think
I've got a strange problem with indexing PDF files in SQL Server 2005, and
got a strange problem: I create a TPanele at runtime and change its color
I got a strange problem with php mail and exchange. When I use the
I've got a strange problem trying to convert from unichar to int: I have
I am having a strange problem in my code. I have many asserts scattered
I got a strange problem here: i have a potential large (as in up
I have got a strange and rare problem here. I am using NSTimer event
I've got a strange problem which I just can't figure out. I have currencies

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.