www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - GC and outside references to D-code

reply teo <teo.ubuntu yahoo.com> writes:
Hi *, I'm concerned about the garbage collector moving an instance of an object
to another location.

From D Specification (page 171):
“... compacting the remaining used memory by copying the allocated objects
(called a
 copying collector)”

Suppose I do following:

alias extern(C) void function(void *context) callback;
extern(C) void xyz(callback handler, void *context);
class A
{
	void foo()
	{
		// some stuff goes here
	}
	static extern(C) void handler(void *context)
	{
		A a = cast(A)context;
		a.foo();
	}
	void test()
	{
		// call xyz - both handler address and this will be stored internally in the
C-library
		xyz(&handler, cast(void*)this);
	}
}

Should I std.gc.addRoot to the address of the callback function in order to
prevent the GC from moving the object? Furthermore, will the GC honor this root
if it points to a member method of an instance of an object? And last question
- is this one correct: std.gc.addRoot(cast(void*)this)?

If above is answered somewhere, please point me to it. I searched this forum,
but to no avail.
Thanks,
-- teo
Jul 05 2007
parent reply Kirk McDonald <kirklin.mcdonald gmail.com> writes:
teo wrote:
 Hi *, I'm concerned about the garbage collector moving an instance of an
object to another location.
 
 From D Specification (page 171):
 “... compacting the remaining used memory by copying the allocated objects
(called a
  copying collector)”
 
Page 171? However, the current GC implementation is not a copying collector. If it ever becomes one, you should expect an API for "pinning" allocated memory (so that it won't be moved) to be added. -- Kirk McDonald http://kirkmcdonald.blogspot.com Pyd: Connecting D and Python http://pyd.dsource.org
Jul 05 2007
parent teo <teo.ubuntu yahoo.com> writes:
Kirk McDonald Wrote:

 teo wrote:
 Hi *, I'm concerned about the garbage collector moving an instance of an
object to another location.
 
 From D Specification (page 171):
 “... compacting the remaining used memory by copying the allocated objects
(called a
  copying collector)”
 
Page 171? However, the current GC implementation is not a copying collector. If it ever becomes one, you should expect an API for "pinning" allocated memory (so that it won't be moved) to be added.
That's why I thought about addRoot. Anyway thanks Kirk.
Jul 06 2007