Can't free memory on exiting?

breezes wangyuanzju at gmail.com
Mon Oct 31 21:10:08 PDT 2011


I wrote the following program to test malloc/free:

import std.stdio;
import core.memory;

class ExternalMemoryHandle {
	void *p;

	this() {
		p = GC.malloc(1024);
	}

	~this() {
		GC.free(p);
	}
}

void main() {
	auto test = new ExternalMemoryHandle();
	writeln("exiting");
}


The philosophy of this program is use class ExternalMemoryHandle as a handle
to some manually managed memory. If a ExternalMemoryHandle is constructed, it
alloc some memory. When this ExternalMemoryHandle is garbage collected, it
free those memory.

However it will fail with OutOfMemoryError. I got the following error when
running this program:

exiting
core.exception.OutOfMemoryError

What's wrong with this tiny program?


More information about the Digitalmars-d-learn mailing list