Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-1066] Window.remove does not remove the view from memory

GitHub Issuen/a
TypeBug
PriorityMedium
StatusClosed
ResolutionFixed
Resolution Date2011-04-17T01:55:06.000+0000
Affected Version/sn/a
Fix Version/sRelease 1.4.0
ComponentsiOS
Labelsdefect, ios, iphone, remove, view
Reporterctredway
AssigneeReggie Seagraves
Created2011-04-15T02:43:02.000+0000
Updated2011-04-17T01:55:06.000+0000

Description

A customer reported that if he created a view in a window and then removed that view, he could add it back. I tried the following code provided from the customer and it does only remove the view from view, not from memory.

var room = Ti.UI.createView({
borderRadius:5,borderWidth:1,borderColor:'#336699',
backgroundColor:'#ffffff',
width:Titanium.App.Properties.getString("rwidth"),
height:Titanium.App.Properties.getString("rlength"),
left:5,
top:5
}); SplitViewPlain.detailWindow.add(room);

var alt2 = Ti.UI.createButton({
left:100,
bottom:5,
height:70,
width:70,
borderRadius:35,
title:'alt2',
backgroundColor:'#ff0000'
}); alt2.addEventListener('click', function()
{ SplitViewPlain.detailWindow.remove(room);
Ti.API.info(alt2);
}); var alt3 = Ti.UI.createButton({
left:180,
bottom:5,
height:70,
width:70,
borderRadius:35,
title:'alt2',
backgroundColor:'#ff0000'
}); alt3.addEventListener('click', function()
{ SplitViewPlain.detailWindow.add(room);
Ti.API.info(alt3);
});

Comments

  1. Stephen Tramer 2011-04-15

    Okay, there are two ways to take this:

    • Removing a view should destroy it forever, and it should never come back
    • Removing a view should release the memory associated with it

    (1) should never happen. (2) should only happen on a memory panic.

  2. Jeff Haynie 2011-04-15

    (from [c910323037d1ce1ef5d8cba6c816d77961beb1b6]) Closes #1066 (sort of): Views released on memory panic if we're able. http://github.com/appcelerator/titanium_mobile/commit/c910323037d1ce1ef5d8cba6c816d77961beb1b6"> http://github.com/appcelerator/titanium_mobile/commit/c910323037d1c...

  3. ctredway 2011-04-15

    Here is my opinion on #1. coming from a flex background, when I removed an object from a container, view, etc, I wanted it gone, not just partially removed. I know lots of people want this same functionality as well. Here is scenario. If I have a rather complex, heavy view/window, and I want it removed to free up memory, I want it gone, destroyed etc..

  4. Stephen Tramer 2011-04-15

    If you want to free up the memory that way, here's how it happens.

    • I remove the view from the screen with remove(). At this point, I can ASSUME, but do not need to know, that it has been removed from memory.
    • The view stays in memory, so that if it is re-added as a subview, there is no delay in doing so.
    • IF there is a memory panic, THEN the view is deallocated (as long as it's not being displayed somewhere).

    Even if we immediately deallocated the view upon remove(), this does NOT invalidate the object which was removed. If the user truly wishes to do that, they can use a remove()/delete combination.

    Think of remove() as lazy-unload. Unloading a view on demand is dangerous and could lead to performance problems.

  5. Blain Hamon 2011-04-15

    The lazy unload is actually very common, if not essential for the iPhone. That is, it's a perfectly valid action to remove a view, yet hold onto it, to put it into a different view, or to re-add it at a later time, releasing ONLY when there's a memory panic.

    You can see this in action in Apple's own code, such as tab views and navigation views, where the underlying action for a tab change or pushing or popping a view literally is to remove the previous view from, er, view, signaled by a -[viewDidDisappear:] method. The owning viewcontroller does not release the unattached views until a memory panic, triggering the -[viewDidUnload] method, which may never need to happen.

  6. ctredway 2011-04-15

    Ok, then remove is not really a true remove, but rather a hide until the os decides it needs to go.

    Tab and Navigation views are the exception in my mind. I relate those to a ViewStack or a TabNavigator in Flex in which we have a stack of views and each controls their own children. but if inside one of the stacks, I need to remove something, then it's gone.

    I can see not destroying windows, especially if they are linked to any type of navigation type group. I am mainly thinking of Views and controls. I will chalk this one up to just being a different platform and treat it as such.

JSON Source