www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - C linkage is fun

reply =?ISO-8859-1?Q?Alex_R=F8nne_Petersen?= <alex lycus.org> writes:
Just a little gotcha I ran into today:

import core.memory;

extern (C) void gc_collect()
{
     assert(false, "nope!");
}

void main()
{
     GC.collect();
}

-- 
Alex Rønne Petersen
alex lycus.org
http://lycus.org
May 24 2012
next sibling parent =?ISO-8859-1?Q?Alex_R=F8nne_Petersen?= <alex lycus.org> writes:
On 24-05-2012 14:06, Alex Rønne Petersen wrote:
 Just a little gotcha I ran into today:

 import core.memory;

 extern (C) void gc_collect()
 {
 assert(false, "nope!");
 }

 void main()
 {
 GC.collect();
 }
BTW, this is particularly fun if your gc_collect function has arguments and tries to read those! -- Alex Rønne Petersen alex lycus.org http://lycus.org
May 24 2012
prev sibling parent reply Robert Clipsham <robert octarineparrot.com> writes:
On 24/05/2012 13:06, Alex Rønne Petersen wrote:
 Just a little gotcha I ran into today:

 import core.memory;

 extern (C) void gc_collect()
 {
 assert(false, "nope!");
 }

 void main()
 {
 GC.collect();
 }
I believe this is by design - the linker will only look for gc_collect() in a library if it isn't found in object files you provide. This can be quite useful if you ever need to replace an internal function of something, it can easily go wrong though if your build process changes for some reason (or any number of other reasons!) -- Robert http://octarineparrot.com/
May 24 2012
parent "Paulo Pinto" <pjmlp progtools.org> writes:
On Thursday, 24 May 2012 at 12:18:14 UTC, Robert Clipsham wrote:
 On 24/05/2012 13:06, Alex Rønne Petersen wrote:
 Just a little gotcha I ran into today:

 import core.memory;

 extern (C) void gc_collect()
 {
 assert(false, "nope!");
 }

 void main()
 {
 GC.collect();
 }
I believe this is by design - the linker will only look for gc_collect() in a library if it isn't found in object files you provide. This can be quite useful if you ever need to replace an internal function of something, it can easily go wrong though if your build process changes for some reason (or any number of other reasons!)
That is how I've done my own malloc()/free() with memory usage object tracking, long before tools like Valgrind became available.
May 24 2012