www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Why is it a memory ERRO.

reply Dsby <dushibaiyu yahoo.com> writes:
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.
Jan 29
next sibling parent Dsby <dushibaiyu yahoo.com> writes:
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
Jan 29
prev sibling parent reply Mike Parker <aldacron gmail.com> writes:
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.
Jan 29
parent reply Mike Parker <aldacron gmail.com> writes:
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; } }
Jan 29
parent reply Dsby <dushibaiyu yahoo.com> writes:
Ok.Thank you.
and i want to know how to know when the GC start runing?
Jan 29
next sibling parent Mike Parker <aldacron gmail.com> writes:
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.
Jan 29
prev sibling parent ZombineDev <valid_email he.re> writes:
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
Jan 30