www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - How do I use the ScopedAllocator?

reply Yuxuan Shui <yshuiv7 gmail.com> writes:
Is it OK to use makeArray with ScopedAllocator to allocate 
something with a destructor?

Because the destructor is not called when ScopedAllocator goes 
out of scope. And if I manually call dispose, I get a double free 
error:

	struct A {
		int a;

		~this() nothrow {
		}
	}
	void test(){
		import std.experimental.allocator.mallocator : Mallocator;
		import 
std.experimental.allocator.building_blocks.scoped_allocator : 
ScopedAllocator;
		import std.experimental.allocator;
		ScopedAllocator!Mallocator alloc;

		auto b = alloc.makeArray!A(10, A(1));
		auto c = alloc.makeArray!A(2, A(2));


		alloc.dispose(b);
		alloc.dispose(c);

	}

	void main() {
		test();
	}
Jul 21 2016
parent reply Nick Treleaven <ntrel-pub mybtinternet.com> writes:
On Friday, 22 July 2016 at 01:54:28 UTC, Yuxuan Shui wrote:
 		auto b = alloc.makeArray!A(10, A(1));
 		auto c = alloc.makeArray!A(2, A(2));
I noticed (using dpaste) if you edit the 'b' line to only allocate 5 structs, and it won't crash. 6 or more, crash. Maybe a bug?
Jul 24 2016
parent reply Yuxuan Shui <yshuiv7 gmail.com> writes:
On Sunday, 24 July 2016 at 20:48:55 UTC, Nick Treleaven wrote:
 On Friday, 22 July 2016 at 01:54:28 UTC, Yuxuan Shui wrote:
 		auto b = alloc.makeArray!A(10, A(1));
 		auto c = alloc.makeArray!A(2, A(2));
I noticed (using dpaste) if you edit the 'b' line to only allocate 5 structs, and it won't crash. 6 or more, crash. Maybe a bug?
Valgrind reports double free anyways. I think it's just the inaccuracy of the glibc double free detection.
Jul 25 2016
parent reply Nick Treleaven <ntrel-pub mybtinternet.com> writes:
On Monday, 25 July 2016 at 07:33:25 UTC, Yuxuan Shui wrote:
 Valgrind reports double free anyways. I think it's just the 
 inaccuracy of the glibc double free detection.
Please file a bug - thanks! https://issues.dlang.org/ I found this, maybe related: https://issues.dlang.org/show_bug.cgi?id=16046
Jul 28 2016
parent Yuxuan Shui <yshuiv7 gmail.com> writes:
On Thursday, 28 July 2016 at 11:48:40 UTC, Nick Treleaven wrote:
 On Monday, 25 July 2016 at 07:33:25 UTC, Yuxuan Shui wrote:
 Valgrind reports double free anyways. I think it's just the 
 inaccuracy of the glibc double free detection.
Please file a bug - thanks! https://issues.dlang.org/ I found this, maybe related: https://issues.dlang.org/show_bug.cgi?id=16046
Fix: https://github.com/dlang/phobos/pull/4702
Aug 02 2016