Widget Button doesn't appear to be removing

I am calling a function I have in a separate lua file that creates the user interface on screen. When you call the method a second time, if any of the UI items already exist it is supposed to remove it and replace it with a new UI. This works great for vector objects and text, but doesn’t appear to work for buttons. Running this code:

[lua]if (titleText:removeSelf()) then print (“title text removed”); end[/lua]

returns true just fine, but this code:

[lua]if (backButton:removeSelf()) then print(“back button removed”); end[/lua]

returns false. I’ve tried removing the button individually, as well as removing the display group it is in to no avail. I’ve tried instantiating the button via:

[lua]backButton = widget.newButton { options }[/lua]

[lua]local backButton = widget.newButton { options }[/lua]

Any insight would be greatly appreciated!

Sean

instead of removeSelf()  try display.remove(backButton)

The function object:removeSelf() does not return a value.  See:  http://docs.coronalabs.com/api/type/DisplayObject/removeSelf.html

When you do:  

if (backButton:removeSelf()) then

you cause the removeSelf() method of the backButton object to execute removing the object.  But what you are also doing is checking the return value of that function to see if it is true or false.  It’s very likely that for non-widget objects, there is an undocumented/accidental true/false being returned where as the button object does not return anything which will cause your if statement to evaluate as false.

Per the documentation, do not expect removeSelf() to return anything.

Fair enough, should have looked into it further. The problem I am running into is that I have a button press on the screen that is supposed to remove a bunch of objects, and replace them with another set of objects. I have found that using display.remove does indeed remove the objects from the screen, but my memory usage keeps increasing. Is there a way to show what is still in memory?

You still have to nil the objects so that the garbage collector knows to do its thing.

instead of removeSelf()  try display.remove(backButton)

The function object:removeSelf() does not return a value.  See:  http://docs.coronalabs.com/api/type/DisplayObject/removeSelf.html

When you do:  

if (backButton:removeSelf()) then

you cause the removeSelf() method of the backButton object to execute removing the object.  But what you are also doing is checking the return value of that function to see if it is true or false.  It’s very likely that for non-widget objects, there is an undocumented/accidental true/false being returned where as the button object does not return anything which will cause your if statement to evaluate as false.

Per the documentation, do not expect removeSelf() to return anything.

Fair enough, should have looked into it further. The problem I am running into is that I have a button press on the screen that is supposed to remove a bunch of objects, and replace them with another set of objects. I have found that using display.remove does indeed remove the objects from the screen, but my memory usage keeps increasing. Is there a way to show what is still in memory?

You still have to nil the objects so that the garbage collector knows to do its thing.

im using corona v2015.2700

and i always get applcation crash if i want to remove widgetbutton

ex :

signup.bSignup = widget.newButton{
            id=_param.ID,
            isEnabled=true,
            width = _param.W,    height = _param.H,
            defaultFile = _param.DEFAULTFILE,    overFile = _param.OVERFILE,
            label = _param.LABEL, labelAlign = _param.LABELALIGN, labelColor= _param.LABELCOLOR,
            font = _param.FONTNAME, fontSize = _param.FONTSIZE,
            emboss = _param.EMBOSS,
            onEvent = _EVENTHANDLER
        };

signup.bSignup:removeEventListener( ‘onEvent’, playfabAccountListener );

display.remove(signup.bSignup);

This probably has nothing to do with your crash, but I doubt this is doing any good:

signup.bSignup:removeEventListener( ‘onEvent’, playfabAccountListener );

There is no “onEvent” event. Its a “touch” event. onEvent just passes the address of the function to the internal code which maintains this. You do not need to remove touch handlers on objects that will be destroyed. The only touch handlers that need removed are ones that are on the whole screen.

As for the crash, can you post the stack trace from your console window?

Rob

okay, i just change it with

and i still get crash and corona simulator stoped working

Problem signature: Problem Event Name: APPCRASH Application Name: Corona Simulator.exe Application Version: 15.0.2700.0 Application Timestamp: 55d83cdf Fault Module Name: Corona Simulator.exe Fault Module Version: 15.0.2700.0 Fault Module Timestamp: 55d83cdf Exception Code: c0000005 Exception Offset: 00070b22 OS Version: 6.1.7600.2.0.0.256.1 Locale ID: 1033 Additional Information 1: 0a9e Additional Information 2: 0a9e372d3b4ad19135b953a78882e789 Additional Information 3: 0a9e Additional Information 4: 0a9e372d3b4ad19135b953a78882e789 Read our privacy statement online: http://go.microsoft.com/fwlink/?linkid=104288&clcid=0x0409 If the online privacy statement is not available, please read our privacy statement offline: C:\Windows\system32\en-US\erofflps.txt

Any other errors before that?

Can you put together a small sample that causes this for me?

Thanks

Rob

im sorry, it was my mistake,

i just found some of my code doing something when i removing the display group

thank you

im using corona v2015.2700

and i always get applcation crash if i want to remove widgetbutton

ex :

signup.bSignup = widget.newButton{
            id=_param.ID,
            isEnabled=true,
            width = _param.W,    height = _param.H,
            defaultFile = _param.DEFAULTFILE,    overFile = _param.OVERFILE,
            label = _param.LABEL, labelAlign = _param.LABELALIGN, labelColor= _param.LABELCOLOR,
            font = _param.FONTNAME, fontSize = _param.FONTSIZE,
            emboss = _param.EMBOSS,
            onEvent = _EVENTHANDLER
        };

signup.bSignup:removeEventListener( ‘onEvent’, playfabAccountListener );

display.remove(signup.bSignup);

This probably has nothing to do with your crash, but I doubt this is doing any good:

signup.bSignup:removeEventListener( ‘onEvent’, playfabAccountListener );

There is no “onEvent” event. Its a “touch” event. onEvent just passes the address of the function to the internal code which maintains this. You do not need to remove touch handlers on objects that will be destroyed. The only touch handlers that need removed are ones that are on the whole screen.

As for the crash, can you post the stack trace from your console window?

Rob

okay, i just change it with

and i still get crash and corona simulator stoped working

Problem signature: Problem Event Name: APPCRASH Application Name: Corona Simulator.exe Application Version: 15.0.2700.0 Application Timestamp: 55d83cdf Fault Module Name: Corona Simulator.exe Fault Module Version: 15.0.2700.0 Fault Module Timestamp: 55d83cdf Exception Code: c0000005 Exception Offset: 00070b22 OS Version: 6.1.7600.2.0.0.256.1 Locale ID: 1033 Additional Information 1: 0a9e Additional Information 2: 0a9e372d3b4ad19135b953a78882e789 Additional Information 3: 0a9e Additional Information 4: 0a9e372d3b4ad19135b953a78882e789 Read our privacy statement online: http://go.microsoft.com/fwlink/?linkid=104288&clcid=0x0409 If the online privacy statement is not available, please read our privacy statement offline: C:\Windows\system32\en-US\erofflps.txt

Any other errors before that?

Can you put together a small sample that causes this for me?

Thanks

Rob

im sorry, it was my mistake,

i just found some of my code doing something when i removing the display group

thank you