How is a display object removed when its parent group is removed?

Hi,

I would like to know how is the display object removed when its parent group is removed.
Are there any calls made to it?

I would like to inject some of my code into the process, so that I can call some additional stuff when object is removed.

I’ve tried using metatables to hijack function calls, but it seems nothing is called on the object :confused:

Here’s my sample code:

[lua]local function proxy(object)
local proxy = { }
proxy._class = object._class
proxy._proxy = object._proxy
proxy.raw = object
– Define metatable
local metatable = {
__index = function ( t, key )
print(“call to” … tostring(key))
if key == “raw” then
return object
end
if type(object[key]) == ‘function’ then
if key == “removeSelf” and object.__removeMe then
object.__removeMe()
end
return function(…) arg[1] = object; objectkey end
else
return object[key]
end

end,
__newindex = function ( t, key, value )
object[key] = value
end
}
setmetatable ( proxy, metatable )
return proxy
end

local function tst()
print(“test”)
end
local grp = display.newGroup()

local secondGrp = display.newGroup()
secondGrp = proxy(secondGrp)
secondGrp.__removeMe = tst
grp:insert(secondGrp)

print(“second group”)
secondGrp:removeSelf()

local thirdGroup = display.newGroup()
thirdGroup = proxy(thirdGroup)
thirdGroup.__removeMe = tst
print(“third group”)
grp:removeSelf()[/lua]

when you run it, you’ll see that when :removeSelf() is called my custom function will be called, but when I remove the parent group, nothing happens on the object [__index is not called]. [import]uid: 109453 topic_id: 34611 reply_id: 334611[/import]

I’m guessing that child display-object destruction and garbage collection happens internally to Corona when the parent is deleted. It would be nice to have hooks but why not just overload the removeSelf methods on the parent group and let it handle the child cleanup??

As another option, you could register your child objects and have an enterFrameHandler fire delete events when it sees they are gone…that’s a more painful kluge but unless CoronaLabs provides a different answer, these are probably your easiest options. [import]uid: 6175 topic_id: 34611 reply_id: 137674[/import]

The trouble with hooks is, that if I wanted to do with what you have suggested, I would have to create proxy for every group used in my game. I don’t want to do that, that’s just too error prone.

adding enter frame handler for every child is a waste of cycles in my case [a lot of code is running there already, I had to limit how often it runs by running it only on odd frames].

It would be awesome if someone from Corona could tell me if this is possible.
I think it is, I ran into an issue some time ago, where I would have a function :remove() on my display object and it would get called from internal corona code.

It’s just that it’s not documented, thus I don’t believe it’s stable - going to stay longterm. [import]uid: 109453 topic_id: 34611 reply_id: 137696[/import]

No, you dont need a proxy…you just do this:

display._newGroup = display.newGroup

local function newGroupOverride()
local ng = display._newGroup()
ng._removeSelf = ng.removeSelf
ng.removeSelf = someFuncWhichCallsChildrenCleanupMethods

return ng
end

display.newGroup = newGroupOverride [import]uid: 6175 topic_id: 34611 reply_id: 137826[/import]

Thanks for your suggestions.

Doesn’t matter if it’s proxy or the way you suggested.
I would still have to modify all of the code of my game + remember about it in the future + make sure other devs remember.

nope… things like this bite in the ass sooner or later.
[import]uid: 109453 topic_id: 34611 reply_id: 137937[/import]

I’m guessing that child display-object destruction and garbage collection happens internally to Corona when the parent is deleted. It would be nice to have hooks but why not just overload the removeSelf methods on the parent group and let it handle the child cleanup??

As another option, you could register your child objects and have an enterFrameHandler fire delete events when it sees they are gone…that’s a more painful kluge but unless CoronaLabs provides a different answer, these are probably your easiest options. [import]uid: 6175 topic_id: 34611 reply_id: 137674[/import]

The trouble with hooks is, that if I wanted to do with what you have suggested, I would have to create proxy for every group used in my game. I don’t want to do that, that’s just too error prone.

adding enter frame handler for every child is a waste of cycles in my case [a lot of code is running there already, I had to limit how often it runs by running it only on odd frames].

It would be awesome if someone from Corona could tell me if this is possible.
I think it is, I ran into an issue some time ago, where I would have a function :remove() on my display object and it would get called from internal corona code.

It’s just that it’s not documented, thus I don’t believe it’s stable - going to stay longterm. [import]uid: 109453 topic_id: 34611 reply_id: 137696[/import]

No, you dont need a proxy…you just do this:

display._newGroup = display.newGroup

local function newGroupOverride()
local ng = display._newGroup()
ng._removeSelf = ng.removeSelf
ng.removeSelf = someFuncWhichCallsChildrenCleanupMethods

return ng
end

display.newGroup = newGroupOverride [import]uid: 6175 topic_id: 34611 reply_id: 137826[/import]

Thanks for your suggestions.

Doesn’t matter if it’s proxy or the way you suggested.
I would still have to modify all of the code of my game + remember about it in the future + make sure other devs remember.

nope… things like this bite in the ass sooner or later.
[import]uid: 109453 topic_id: 34611 reply_id: 137937[/import]