2 Replies Latest reply on Feb 14, 2007 1:15 AM by popgodabubble

    killing scripts

      Hi All,

      If I add a script to a models userdata like so: myModel.userData.addProp(#pScript, new(script "myScript", me)

      will the script instance be completly deleted via calling reset world(). This is the only ref made to the script, so I asume it to be so, it looks like it does but checking just in case.

      Cheers, pop
        • 1. killing scripts
          James Newton, ACP Level 3
          It is very easy to create a memory leak by not properly disposing of pointers to instances or models before calling resetWorld(). In your case, maintaining a pointer to myModel at the point that resetWorld() is called will cause a memory leak.

          Try this test in the Message window. It assumes that you are aware of the following

          1) An instance reference indicates how many pointers there are to the given instance.
          Example: -- <offspring "" 2 734daf0> has 2 pointers to it, as shown by the 2 after the
          string script name.
          2) When you use "put" for an instance in the Message window, the Message window
          creates a temporary pointer to the instance, thus giving a number of pointers 1 higher
          than what you might expect.
          3) To workaround this, you can add the instance to a list, and use put on the list.
          The Message window will create an additional temporary pointer to the list, but will
          now show the expected number of pointers for the instance.

          gMember = new(#shockwave3D)
          gModel = vMember.newModel("Plane", gMember.modelResource(1))

          gScript = new(#script)
          gScript.scriptText = "--" -- so it compiles
          gList = [script(gScript).new()]
          put gList
          -- [<offspring "" 1 82147c0>] -- 1 pointer to the instance on gList

          gModel.userData.addProp(#script, gList[1])
          put gList
          -- [<offspring "" 2 82147c0>] -- plus a pointer to the instanco on userData

          put gList
          -- [<offspring "" 2 82147c0>]

          -- There are still two pointers. Perhaps this is because gModel still holds a pointer to the now-deleted model, so its userData list still exists.

          gModel = VOID
          put gList
          -- [<offspring "" 2 82147c0>]

          As you can see, there are still two pointers: one on gList, and one which is now inaccessible. We have created a memory leak.

          In other words, you must be very careful to delete the instance from the userData list, or to delete all pointers to the userData list and the model it is attached to, before you use resetWorld() on the member. You should use either...

          gModel.userData.deleteAll() -- to delete the instance from the userData list
          gModel = VOID -- to delete the additional pointer to the model

          ... before you call gMember.resetWorld() .

          I strongly recommended that you write the code that disposes of an instance at the same time that you write the code to create it, and that you take care to dispose of all pointers in a logical order, so that none become orphaned and inaccessible.
          • 2. Re: killing scripts
            popgodabubble Level 1
            Hi, thanks for your reply, its just the info I was looking for.

            Upon implimentation of a script by adding it to userdata as detailed, with a ref stored as a list entry indicates 2 pointers within the script instance name.

            This sugguests that if there is no additional ref ( model self contained within w3d, not created) that if upon reseting the world the userdata is removed and thus also all referance to the script.

            Ill do a bit more testing, but for now have implimented via your suggestions and works great. I was not aware that put added a tempory ref, thanks loads for that info.

            Cheers for time and all, pop