Repository: toml-rs/toml (language: rust)

Issue:
Memory error when running tests on Windows
When I run the tests on Windows, the test runner crashes in `test_insert_nonleaf_table` with a read access violation. I tried to debug and got the following stack trace:

```
test_edit-e9fa5d7815bf14cc.exe!core::cell::Cell<intrusive_collections::linked_list::NodePtr>::get<intrusive_collections::linked_list::NodePtr>() Line 212
test_edit-e9fa5d7815bf14cc.exe!intrusive_collections::linked_list::NodePtr::next() Line 130
test_edit-e9fa5d7815bf14cc.exe!intrusive_collections::linked_list::LinkedList<toml_edit::document::intrusive::TableAdapter>::clear<toml_edit::document::intrusive::TableAdapter>() Line 728
test_edit-e9fa5d7815bf14cc.exe!intrusive_collections::linked_list::{{impl}}::drop<toml_edit::document::intrusive::TableAdapter>(intrusive_collections::linked_list::LinkedList<toml_edit::document::intrusive::TableAdapter> * self) Line 804
test_edit-e9fa5d7815bf14cc.exe!core::ptr::drop_in_place<intrusive_collections::linked_list::LinkedList<toml_edit::document::intrusive::TableAdapter>>(intrusive_collections::linked_list::LinkedList<toml_edit::document::intrusive::TableAdapter> *) Line 61
test_edit-e9fa5d7815bf14cc.exe!core::ptr::drop_in_place<toml_edit::document::DocumentInner>(toml_edit::document::DocumentInner *) Line 61
test_edit-e9fa5d7815bf14cc.exe!core::ptr::drop_in_place<alloc::boxed::Box<toml_edit::document::DocumentInner>>(toml_edit::document::DocumentInner * *) Line 61
test_edit-e9fa5d7815bf14cc.exe!core::ptr::drop_in_place<toml_edit::document::Document>(toml_edit::document::Document *) Line 61
test_edit-e9fa5d7815bf14cc.exe!test_edit::test_insert_nonleaf_table() Line 77
```

So it looks like the problem is probably coming from `intrusive_collections::linked_list` (i.e., not your fault, @ordian). Maybe the raw pointer in `NodePtr` is invalid ... but I didn't investigate further.


Discussion / hints:
Thanks for the report!

Looks like the default drop impl for the document is incorrect, because rust does not specify drop order of fields. It is strange, that appveyor CI tests are passing. Are you using 32bit windows?

Probably resolving #19 would eliminate the need for unsafe code.

Fix patch:
diff --git a/src/document.rs b/src/document.rs
index 760d070f..66f7eca0 100644
--- a/src/document.rs
+++ b/src/document.rs
@@ -14,7 +14,7 @@
 // See comments in the Table struct for details about safety.
 use intrusive_collections::{LinkedList, LinkedListLink, UnsafeRef};
 use std::str::FromStr;
-use std::mem::size_of;
+use std::mem;
 use typed_arena::Arena;
 use table::{Header, HeaderKind, Table, TableChild};
 use decor::{InternalString, Repr};
@@ -61,7 +61,7 @@ impl Document {
     pub fn new() -> Self {
         let list = LinkedList::new(intrusive::TableAdapter::new());
         // reserve space for 10 tables
-        let arena = Arena::with_capacity(size_of::<Table>() * 10);
+        let arena = Arena::with_capacity(mem::size_of::<Table>() * 10);
         let inner = DocumentInner::new(arena, list);
         let mut doc = Box::new(inner);
         let header = Header {
@@ -131,6 +131,12 @@ impl DocumentInner {
     }
 }
 
+impl Drop for DocumentInner {
+    fn drop(&mut self) {
+        self.list.clear();
+    }
+}
+
 impl FromStr for Document {
     type Err = parser::Error;
 


Test patch: