Large table access from c++ functions with boost::python
<p>I'm generating a very large lookup table in C++ and using it from a variety of C++ functions. These functions are exposed to python using boost::python. </p>

<p>When not used as part of a class the desired behaviour is achieved. When I try and use the functions as part of a class written in just python I run in to issues accessing the lookup table.</p>

<pre><code>    ------------------------------------
    C++ for some numerical heavy lifting
    ------------------------------------
    include &lt;boost/python&gt;

    short really_big_array[133784630]

    void fill_the_array(){
      //Do some things which populate the array
    }

    int use_the_array(std::string thing){
      //Lookup a few million things in the array depending on thing
      //Return some int dependent on the values found
    }

    BOOST_PYTHON_MODULE(bigarray){
      def("use_the_array", use_the_array)
      def("fill_the_array", fill_the_array)
    }

    ---------
    PYTHON Working as desired
    ---------
    import bigarray

    if __name__ == "__main__"
        bigarray.fill_the_array()
        bigarray.use_the_array("Some Way")
        bigarray.use_the_array("Some Other way")
    #All works fine

    ---------
    PYTHON Not working as desired
    ---------
    import bigarray

    class BigArrayThingDoer():
    """Class to do stuff which uses the big array,
    use_the_array() in a few different ways
    """
    def __init__(self,other_stuff):
        bigarray.fill_the_array()
        self.other_stuff = other_stuff

    def use_one_way(thing):
        return bigarray.use_the_array(thing)

    if __name__ == "__main__"
        big_array_thing_doer = BigArrayThingDoer()
        big_array_thing_doer.use_one_way(thing)
    #Segfaults or random data
</code></pre>

<p>I think I am probably not exposing enough to python to make sure the array is accessible at the right times, but im not quite sure exactly what I shoudld be exposing. Equally likely that there is some sort of problem involving ownership of the lookup table.</p>

<p>I dont ever need to manipulate the lookup table except via the other c++ functions.</p>

<p>Missing a self in a definition. Used key word arguments where I should probably have used keyword only arguments so got no error when running.</p>

python c++ boost-python