Thread overview
Why is it a memory ERRO.
Jan 29, 2016
Dsby
Jan 29, 2016
Dsby
Jan 29, 2016
Mike Parker
Jan 29, 2016
Mike Parker
Jan 30, 2016
Dsby
Jan 30, 2016
Mike Parker
Jan 30, 2016
ZombineDev
January 29, 2016
the Code:
class MyClass
{
	this(){
		by = new ubyte[10000];
		++i;
	}
	~this(){
		GC.free(by.ptr);
		by = null;
		writeln("free");
	}
	void show(){
		writeln(i);
	};
private:
	ubyte[]   by;
	static i = 0;
};

void main()
{
	bool start = true;
	int i = 0;
	while(start){
		auto obj = new MyClass();
		obj.show();
		Thread.sleep(5.msecs);
		//obj.destroy;
		//GC.free(cast(void *)obj);
		++i;
		if (i > 20000)
			break;
	}
}

the code will be :
341
core.exception.InvalidMemoryOperationError@src/core/exception.d(679): Invalid memory operation
----------------
core.exception.InvalidMemoryOperationError@src/core/exception.d(679): Invalid memory operation
----------------

.why is it?
if < obj.destroy; > is exec. the code will not errno.

January 29, 2016
On Friday, 29 January 2016 at 12:43:53 UTC, Dsby wrote:
> the Code:
> class MyClass
> {
> 	this(){
> 		by = new ubyte[10000];
> 		++i;
> 	}
> 	~this(){
> 		GC.free(by.ptr);
> 		by = null;
> 		writeln("free");
> 	}
> 	void show(){
> 		writeln(i);
> 	};
> private:
> 	ubyte[]   by;
> 	static i = 0;
> };
>
> void main()
> {
> 	bool start = true;
> 	int i = 0;
> 	while(start){
> 		auto obj = new MyClass();
> 		obj.show();
> 		Thread.sleep(5.msecs);
> 		//obj.destroy;
> 		//GC.free(cast(void *)obj);
> 		++i;
> 		if (i > 20000)
> 			break;
> 	}
> }
>
> the code will be :
> 341
> core.exception.InvalidMemoryOperationError@src/core/exception.d(679): Invalid memory operation
> ----------------
> core.exception.InvalidMemoryOperationError@src/core/exception.d(679): Invalid memory operation
> ----------------
>
> .why is it?
> if < obj.destroy; > is exec. the code will not errno.

I am in 2.069, on opensuse leap 42.1 X86_64
dmd -v
DMD64 D Compiler v2.069
Copyright (c) 1999-2015 by Digital Mars written by Walter Bright

January 29, 2016
On Friday, 29 January 2016 at 12:43:53 UTC, Dsby wrote:
> the Code:

> 	~this(){
> 		GC.free(by.ptr);
> 		by = null;
> 		writeln("free");
> 	}

Your problem is probably that you are calling GC.free in the destructor. Don't do this. You don't need to call GC.free at all. The GC will collect both your object instance and the memory you allocated with new. Never, ever, manipulate GC memory in the destructor of a GC-managed object.
January 29, 2016
On Friday, 29 January 2016 at 13:22:07 UTC, Mike Parker wrote:

> Your problem is probably that you are calling GC.free in the destructor. Don't do this. You don't need to call GC.free at all. The GC will collect both your object instance and the memory you allocated with new. Never, ever, manipulate GC memory in the destructor of a GC-managed object.

Here's a modified version of your program. Every time it prints, you know the garbage collector has just run a collection cycle. Every class instance and every 'by' that is allocated will eventually be free. Not all destructors are guaranteed to run during the program's lifetime, though. Some will be run after main exits and the GC shuts down.

import core.thread;
import std.stdio;

class MyClass
 {
   this(){
       by = new ubyte[10000];
       id = i;
       ++i;
   }
   ~this() {
       writeln("freed #", id);
   }
 private:
   ubyte[]   by;
   int id;
   static int i;
 };

void main()
{
    while(true) {
       auto obj = new MyClass;
       Thread.sleep(5.msecs);
       if(MyClass.i == 2000)
        break;
    }
}
January 30, 2016
Ok.Thank you.
and i want to know how to know when the GC start runing?
January 30, 2016
On Saturday, 30 January 2016 at 05:50:33 UTC, Dsby wrote:
> Ok.Thank you.
> and i want to know how to know when the GC start runing?

For the current implementation, any time you allocate memory through the GC it will determine if a collection cycle is needed, but it will not run otherwise.
January 30, 2016
On Saturday, 30 January 2016 at 05:50:33 UTC, Dsby wrote:
> Ok.Thank you.
> and i want to know how to know when the GC start runing?

See also http://dlang.org/phobos/core_memory