Lodle
Lodle

Reputation: 32187

stop python object going out of scope in c++

Is there a way to transfer a new class instance (python class that inherits c++ class) into c++ with out having to hold on to the object return and just treat it as a c++ pointer.

For example:

C++

object pyInstance = GetLocalDict()["makeNewGamePlay"]();
CGEPYGameMode* m_pGameMode = extract< CGEPYGameMode* >( pyInstance );

pyth:

class Alpha(CGEPYGameMode):
  def someFunct(self):
    pass

def makeNewGamePlay():
  return Alpha()

pyInstance is the python class instance and m_pGameMode is a pointer to the c++ baseclass of the same instance. However if i store the pointer and let the object go out of scope, the python object is cleaned up. Is there any way to only have the c++ pointer with out the object getting cleaned up?

More info: python object to native c++ pointer

Upvotes: 0

Views: 420

Answers (1)

Aaron Digulla
Aaron Digulla

Reputation: 328536

You must increment the reference count of the pyInstance. That will prevent Python from deleting it. When you are ready to delete it, you can simply decrement the reference count and Python will clean it up for you.

Upvotes: 2

Related Questions