On Sat, Feb 25, 2017 at 12:13 PM Paul Molodowitch <[email protected]> wrote:
> The net result is that it's possible for python to destroy the > MScriptUtil, and it's associated C++ objects... but still have a ptr object > created by that MScriptUtil, which tries to use that now-freed space. > In all fairness though, the documentation does say the pointers are only valid for the lifetime of the MScriptUtil instance :-) > > As an example, code like this can cause a crash: > > def getPtr(): > return MScriptUtil().asDoublePtr() > > ptr = getPtr() > maya.OpenMaya.someFuncThatNeedsADoublePtr(ptr) > > > The problem is that the MScriptUtil object created within the function > will get deleted as soon as the function exits... but the returned pointer > object still points at the storage associated with that (now freed) > MScriptUtil. This is probably one of the many reasons why it was removed > completely in the python 2.0 api (yay!) > > However, because we still need to use the 1.0 api for many things... pymel > contains a small wrapper class for dealing with MScriptUtil pointers. > Basically, it just holds a (python) ref to the MScriptUtil object along > with the pointer, to ensure that it isn't destroyed prematurely. Full > details are in the docs / comments in pymel.api.allapi.SafeApiPtr > <https://github.com/LumaPictures/pymel/blob/master/pymel/api/allapi.py#L30>, > but here's some example usage: > > ptr = pymel.api.allapi.SafeApiPtr('double') > maya.OpenMaya.someFuncThatNeedsADoublePtr(ptr()) > > > Note that you need to manually get the pointer object by "calling" it - > the general idea is that you should hold onto the SafeApiPtr object in > python, and get the "raw" MScriptUtil pointer only immediately before > passing to the OpenMaya call. However, the SafeApiPtr still can't > guarantee that you won't get in trouble - for instance, you can't do this: > > maya.OpenMaya.someFuncThatNeedsADoublePtr(SafeApiPtr('double')()) > > ...as the SafeApiPtr, and thus the MScriptUtil, may be destroyed by the > time the OpenMaya function is called. Essentially, you need to make sure > the SafeApiPtr object is kept around until the OpenMaya C++ functions are > "done with it". > > - Paul > > On Fri, Feb 24, 2017 at 2:02 AM Justin Israel <[email protected]> > wrote: > > > > On Thu, Feb 23, 2017, 9:04 PM justin hidair <[email protected]> > wrote: > > Quick questions here : > > How do you properly destroy your objects and MScriptutils.. ? > > > I assume this is a C++ question, since Python is garbage collected? > > "When an MScriptUtil object is destroyed any pointers to its data > immediately become invalid." > > So any data being managed by MScriptUtil should get cleaned up when the > dtor is called. Is that what you are after? > > > Any optimization tricks ? > > > What specifically are you after? > > > Finally how many of you guys use Python API 2.O ? > > -- > You received this message because you are subscribed to the Google Groups > "Python Programming for Autodesk Maya" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To view this discussion on the web visit > https://groups.google.com/d/msgid/python_inside_maya/08bf227c-8566-4016-8520-625a04a0f46e%40googlegroups.com > <https://groups.google.com/d/msgid/python_inside_maya/08bf227c-8566-4016-8520-625a04a0f46e%40googlegroups.com?utm_medium=email&utm_source=footer> > . > For more options, visit https://groups.google.com/d/optout. > > -- > You received this message because you are subscribed to the Google Groups > "Python Programming for Autodesk Maya" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > > To view this discussion on the web visit > https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA2p3%2ByouTDi-d83XMubKfce_UY_B5-8V4rMtYQbFxAvdw%40mail.gmail.com > <https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA2p3%2ByouTDi-d83XMubKfce_UY_B5-8V4rMtYQbFxAvdw%40mail.gmail.com?utm_medium=email&utm_source=footer> > . > > > For more options, visit https://groups.google.com/d/optout. > > -- > You received this message because you are subscribed to the Google Groups > "Python Programming for Autodesk Maya" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To view this discussion on the web visit > https://groups.google.com/d/msgid/python_inside_maya/CAAssL7aGz%2B8RE5Cu9DpkaticQOvq1OR2s1yB5kmQ_%3DeLCAK%2B0g%40mail.gmail.com > <https://groups.google.com/d/msgid/python_inside_maya/CAAssL7aGz%2B8RE5Cu9DpkaticQOvq1OR2s1yB5kmQ_%3DeLCAK%2B0g%40mail.gmail.com?utm_medium=email&utm_source=footer> > . > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA1wPdh7KsDMBnCHzyNzakiAYSSdk%3Du4bU8jPRS4gVWatw%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.
