dataTableSettings not correctly identifying: Leaking Memory
dataTableSettings not correctly identifying: Leaking Memory
gutzofter
Posts: 58Questions: 0Answers: 0
I started a discussion here: http://datatables.net/forums/comments.php?DiscussionID=2306&page=1#Item_0
Here is my failing tests using your framework:
[code]
// DATA_TEMPLATE: dom_data
oTest.fnStart( "Identification Bug" );
$(document).ready( function () {
var tableHolder = $('#demo').html();
var testDataTable = $('#example').dataTable();
/* Bug for identification checks */
oTest.fnTest(
"Bug identified removing table and adding it back - will PASS with bug",
function () {
},
function () {
$('#demo').remove();
$('').appendTo('#container');
$('#demo').html(tableHolder);
testDataTable = $('#example').dataTable({ bDestory: true });
return testDataTable.dataTableSettings.length == 2; }
);
oTest.fnTest(
"Bug identified removing table and adding it back - will FAIL with bug",
function () {
},
function () {
$('#demo').remove();
$('').appendTo('#container');
$('#demo').html(tableHolder);
testDataTable = $('#example').dataTable({ bDestory: true });
return testDataTable.dataTableSettings.length == 1; }
);
oTest.fnComplete();
} );
[/code]
Failure is in both firefox and ie.
Please help!
Here is my failing tests using your framework:
[code]
// DATA_TEMPLATE: dom_data
oTest.fnStart( "Identification Bug" );
$(document).ready( function () {
var tableHolder = $('#demo').html();
var testDataTable = $('#example').dataTable();
/* Bug for identification checks */
oTest.fnTest(
"Bug identified removing table and adding it back - will PASS with bug",
function () {
},
function () {
$('#demo').remove();
$('').appendTo('#container');
$('#demo').html(tableHolder);
testDataTable = $('#example').dataTable({ bDestory: true });
return testDataTable.dataTableSettings.length == 2; }
);
oTest.fnTest(
"Bug identified removing table and adding it back - will FAIL with bug",
function () {
},
function () {
$('#demo').remove();
$('').appendTo('#container');
$('#demo').html(tableHolder);
testDataTable = $('#example').dataTable({ bDestory: true });
return testDataTable.dataTableSettings.length == 1; }
);
oTest.fnComplete();
} );
[/code]
Failure is in both firefox and ie.
Please help!
This discussion has been closed.
Replies
I noticed this as I added more unit tests to my code the tests got slower and slower.
Note this occurs using beta and stable version.
Thanks very much for this! Showing the issue in the test harness - nice one :-)
It's a very interesting point that, and not something I had considered - having the table removed from the DOM by and action not under DataTables control. While obviously fair enough - this will remove the table itself, but not any other elements which have been injected by DataTables, and leave the event handlers in place, which isn't really what is wanted I would imagine. To some extent this is got around in your test above by removing the parent element of the table - it will still leave a few things knocking around that you don't want (the memory use as you indicate for example!).
One way around this would be to call fnDestroy() before removing the element from the DOM, which will release everything, and then allow you to take the actions you require.
I'm slightly hesitant to include a check for the ID as well as the table node, since the table has been removed in a "unexpected way". The same theory applies to using fnUpdate to update information in a row, rather than just writing the information content to the element directly - it's under DataTables' control, so the action should be performed through DataTables. That's not to say 'm adverse to the idea, but I'd like to consider it and it's consequences before including that logic :-). Any thoughts on it are very welcome!
I've also spotted that I've got a typo in the 'bDestory' property :-/. This will be corrected in the next beta (or release if that happens first), so something that will require a small code update here...
Regards,
Allan
[code]
.remove();
[/code]
[code]
.html('new content');
[/code]
This happens by the. User selecting content them going to some other content. Them back to the original content. So if I use datatables in my other content I keep hanging onto these memory structures until a refresh.
You are right. Really needd some thought on dealing with this.
Side note: in the eighties I worked on a system that when it errored it would output on a teletype: FAT AL ERROR. I became known as the Fat Albert system. So when talking about dstroying datatables we are actually de-storying them.
Thanks for your input.
I heard that it might be possible to bind a listener to the jquery code that
[code]
.remove();
[/code]
[code]
.html('new content');
[/code]
This happens by the. User selecting content them going to some other content. Them back to the original content. So if I use datatables in my other content I keep hanging onto these memory structures until a refresh.
You are right. Really needd some thought on dealing with this.
Side note: in the eighties I worked on a system that when it errored it would output on a teletype: FAT AL ERROR. I became known as the Fat Albert system. So when talking about dstroying datatables we are actually de-storying them.
Thanks for your input.
I heard that it might be possible to bind a listener to the jquery code that
[code]
if (tables.length > 0) {
tables.fnDestroy();
}
[/code]
It will reinitialize one of the tables. So I have to do this:
[code]
if (tables.length > 0) {
tables.each(function() {
$(this).dataTable().fnDestroy();
});
}
[/code]
Is this normal?
It's not ideal, and will probably change when it comes time to think about DataTables 2, but this is expected behaviour at the moment.
Still thinking about the best way of dealing with the memory leak you've pointed out - thinking that it will be a good check to make and deal with. The key thing will be to make it efficient :-)
Regards,
Allan
oops! found an artifact in my code. The testing for the tables length > 0. This came from trying to destroy tables, the code that I use is:
[code]
removeDataTables: function() {
tables.each(function() {
$(this).dataTable().fnDestroy();
});
}
[/code]
Looking at the above code, I'm entirely satisfied with it. The each explicitly tells me what is going on: I'm iterating over a list of tables.
A side note concerning the logging. For me using alerts is just crazy. I'm replacing that with exception generation.
I use an automated testing framework, so i need exceptions instead of something that stops code execution and waits for user input.
Maybe generate an exception when trying to remove the HTML.
Regards,
Allan
1> make change to code
2> Go look at display of web page.
Most of my development is through TDD. The only time that I'm looking at the actual web page is when I'm finished with my functionality. So I'm writing unit tests all the time. I have my browser set up with a program called XRefresh that when I save my code it will automatically refresh my browser (my unit tests get run). I need the unit test to tell me that there is a problem. I don't want to to have to click on anything during my execution of unit tests.
Allan