This content has been marked as final. Show 2 replies
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()]
-- [<offspring "" 1 82147c0>] -- 1 pointer to the instance on gList
-- [<offspring "" 2 82147c0>] -- plus a pointer to the instanco on userData
-- [<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
-- [<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.
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