Can't free memory on exiting?

breezes wangyuanzju at gmail.com
Tue Nov 1 00:27:30 PDT 2011


Thanks Andrej. That bug says that you can not alloc memory during GC. However i
don't alloc but free memory in ~this. But anyway, as you said, I should use
malloc/free in core.stdc.stdlib to manage memory manually. I modified the code to
use that malloc/free, and it works without crashing.

However, the following little bit more complex test got a Bus error.

import core.stdc.stdlib;

class Allocator {
	void* alloc(size_t size) {
		return malloc(size);
	}

	void free(void *block) {
		core.stdc.stdlib.free(block);
	}
}

class Pages {
	this(Allocator allocator) {
		_allocator = allocator;
		void *p = _allocator.alloc(1000);
		_pages ~= p;
	}

	~this() {
		_allocator.free(_pages[0]);// Bus error there
	}

	Allocator	_allocator;
	void*[]		_pages;
	size_t		_a;
	size_t		_b;
	size_t		_c;
	size_t		_d;
	size_t		_e;
	size_t		_f;
}

void main() {
	auto a = new Allocator();
	auto pg = new Pages(a);
}

I got the following bus error:
Bus error: 10

What's the problem? Does it mean that I can not access _allocator during the
deconstruction of pg? And the most mythical thing is that if I comment out the
declaration of _a to _f of Pages, then the bus error will go.

(I use the most recent dmd 2.0.056.)


More information about the Digitalmars-d-learn mailing list