Memory allocation failed. Why?

Basile B. via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Nov 20 10:58:04 PST 2016


On Sunday, 20 November 2016 at 17:47:50 UTC, MGW wrote:
> import core.sys.windows.windows: MessageBoxA;
>
> void test() {
> 	for(int i; i != 10; i++) {
> 		ubyte[] buf;
> 		for(int j; j != 100000000; j++) buf ~= 65;
> 		MessageBoxA(null, "--on for--".ptr, "".ptr, 0);
> 		// delete buf; // if ON - then memory delete in step
> 	}
> 	MessageBoxA(null, "--off for--".ptr, "".ptr, 0);
> }
>
> void main() {
> 	test();
> 	MessageBoxA(null, "--The end--".ptr, "".ptr, 0);
> }
>
> ----------------
> core.exception.OutOfMemoryError at src\core\exception.d(693): 
> Memory allocation failed
> ----------------
>
> Simple program and error. Why?  Windows 7 (32) dmd 2.072.0

For me there's no exception. Maybe the GC is poluted. Try to add 
this after each iteration in the first test loop:

         import core.memory: GC;
         GC.collect();

Also note that the text you pas in the message box should be null 
terminated, eg:

         MessageBoxA(null, "--The end--\0".ptr, "".ptr, 0);


More information about the Digitalmars-d-learn mailing list