Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

Several issues - posix.unistd, core.Memory

Moderators: larsivi kris

Posted: 04/08/08 18:55:14

hm, not my day I guess :)

first things first, following code then errors

1.

module gc_test;

import tango.io.Stdout;
import tango.stdc.posix.unistd;

class A {
    int x;
    
    this() {
        Stdout.formatln("Calling constructor of A");
    }
    ~this() {
        Stdout.formatln("Calling destructor of A");
    }
}

void main() {
    
    A foo = new A;
    usleep(50000);
    sleep(5);
    
}

/*
C:\d_programming\tango\import\tango\stdc\posix\unistd.d(30): Error: identifier 'uid_t' is not defined
C:\d_programming\tango\import\tango\stdc\posix\unistd.d(30): Error: uid_t is used as a type
C:\d_programming\tango\import\tango\stdc\posix\unistd.d(30): Error: cannot have parameter of type void
C:\d_programming\tango\import\tango\stdc\posix\unistd.d(30): Error: identifier 'gid_t' is not defined
C:\d_programming\tango\import\tango\stdc\posix\unistd.d(30): Error: gid_t is used as a type
C:\d_programming\tango\import\tango\stdc\posix\unistd.d(30): Error: cannot have parameter of type void
C:\d_programming\tango\import\tango\stdc\posix\unistd.d(42): Error: identifier 'uid_t' is not defined
C:\d_programming\tango\import\tango\stdc\posix\unistd.d(42): Error: uid_t is used as a type
C:\d_programming\tango\import\tango\stdc\posix\unistd.d(42): Error: cannot have parameter of type void
C:\d_programming\tango\import\tango\stdc\posix\unistd.d(42): Error: identifier 'gid_t' is not defined
C:\d_programming\tango\import\tango\stdc\posix\unistd.d(42): Error: gid_t is used as a type
C:\d_programming\tango\import\tango\stdc\posix\unistd.d(42): Error: cannot have parameter of type void
C:\d_programming\tango\import\tango\stdc\posix\unistd.d(43): Error: identifier 'pid_t' is not defined
C:\d_programming\tango\import\tango\stdc\posix\unistd.d(43): Error: pid_t is used as a type
C:\d_programming\tango\import\tango\stdc\posix\unistd.d(45): Error: identifier 'off_t' is not defined
C:\d_programming\tango\import\tango\stdc\posix\unistd.d(45): Error: off_t is used as a type
C:\d_programming\tango\import\tango\stdc\posix\unistd.d(45): Error: cannot have parameter of type void
C:\d_programming\tango\import\tango\stdc\posix\unistd.d(47): Error: identifier 'gid_t' is not defined
C:\d_programming\tango\import\tango\stdc\posix\unistd.d(47): Error: gid_t is used as a type
C:\d_programming\tango\import\tango\stdc\posix\unistd.d(48): Error: identifier 'uid_t' is not defined
C:\d_programming\tango\import\tango\stdc\posix\unistd.d(48): Error: uid_t is used as a type
C:\d_programming\tango\import\tango\stdc\posix\unistd.d(49): Error: identifier 'gid_t' is not defined
C:\d_programming\tango\import\tango\stdc\posix\unistd.d(49): Error: gid_t is used as a type
C:\d_programming\tango\import\tango\stdc\posix\unistd.d(50): Error: identifier 'gid_t' is not defined
C:\d_programming\tango\import\tango\stdc\posix\unistd.d(50): Error: gid_t is used as a type
C:\d_programming\tango\import\tango\stdc\posix\unistd.d(55): Error: identifier 'pid_t' is not defined
C:\d_programming\tango\import\tango\stdc\posix\unistd.d(55): Error: pid_t is used as a type
C:\d_programming\tango\import\tango\stdc\posix\unistd.d(56): Error: identifier 'pid_t' is not defined
C:\d_programming\tango\import\tango\stdc\posix\unistd.d(56): Error: pid_t is used as a type
C:\d_programming\tango\import\tango\stdc\posix\unistd.d(57): Error: identifier 'pid_t' is not defined
C:\d_programming\tango\import\tango\stdc\posix\unistd.d(57): Error: pid_t is used as a type
C:\d_programming\tango\import\tango\stdc\posix\unistd.d(58): Error: identifier 'uid_t' is not defined
C:\d_programming\tango\import\tango\stdc\posix\unistd.d(58): Error: uid_t is used as a type
C:\d_programming\tango\import\tango\stdc\posix\unistd.d(61): Error: identifier 'off_t' is not defined
C:\d_programming\tango\import\tango\stdc\posix\unistd.d(61): Error: off_t is used as a type
C:\d_programming\tango\import\tango\stdc\posix\unistd.d(61): Error: identifier 'off_t' is not defined
C:\d_programming\tango\import\tango\stdc\posix\unistd.d(61): Error: off_t is used as a type
C:\d_programming\tango\import\tango\stdc\posix\unistd.d(61): Error: cannot have parameter of type void
C:\d_programming\tango\import\tango\stdc\posix\unistd.d(65): Error: identifier 'ssize_t' is not defined
C:\d_programming\tango\import\tango\stdc\posix\unistd.d(65): Error: ssize_t is used as a type
C:\d_programming\tango\import\tango\stdc\posix\unistd.d(66): Error: identifier 'ssize_t' is not defined
C:\d_programming\tango\import\tango\stdc\posix\unistd.d(66): Error: ssize_t is used as a type
C:\d_programming\tango\import\tango\stdc\posix\unistd.d(68): Error: identifier 'gid_t' is not defined
C:\d_programming\tango\import\tango\stdc\posix\unistd.d(68): Error: gid_t is used as a type
C:\d_programming\tango\import\tango\stdc\posix\unistd.d(68): Error: cannot have parameter of type void
C:\d_programming\tango\import\tango\stdc\posix\unistd.d(69): Error: identifier 'uid_t' is not defined
C:\d_programming\tango\import\tango\stdc\posix\unistd.d(69): Error: uid_t is used as a type
C:\d_programming\tango\import\tango\stdc\posix\unistd.d(69): Error: cannot have parameter of type void
C:\d_programming\tango\import\tango\stdc\posix\unistd.d(70): Error: identifier 'gid_t' is not defined
C:\d_programming\tango\import\tango\stdc\posix\unistd.d(70): Error: gid_t is used as a type
C:\d_programming\tango\import\tango\stdc\posix\unistd.d(70): Error: cannot have parameter of type void
C:\d_programming\tango\import\tango\stdc\posix\unistd.d(71): Error: identifier 'pid_t' is not defined
C:\d_programming\tango\import\tango\stdc\posix\unistd.d(71): Error: pid_t is used as a type
C:\d_programming\tango\import\tango\stdc\posix\unistd.d(71): Error: cannot have parameter of type void
C:\d_programming\tango\import\tango\stdc\posix\unistd.d(71): Error: identifier 'pid_t' is not defined
C:\d_programming\tango\import\tango\stdc\posix\unistd.d(71): Error: pid_t is used as a type
C:\d_programming\tango\import\tango\stdc\posix\unistd.d(71): Error: cannot have parameter of type void
C:\d_programming\tango\import\tango\stdc\posix\unistd.d(72): Error: identifier 'pid_t' is not defined
C:\d_programming\tango\import\tango\stdc\posix\unistd.d(72): Error: pid_t is used as a type
C:\d_programming\tango\import\tango\stdc\posix\unistd.d(73): Error: identifier 'uid_t' is not defined
C:\d_programming\tango\import\tango\stdc\posix\unistd.d(73): Error: uid_t is used as a type
C:\d_programming\tango\import\tango\stdc\posix\unistd.d(73): Error: cannot have parameter of type void
C:\d_programming\tango\import\tango\stdc\posix\unistd.d(77): Error: identifier 'pid_t' is not defined
C:\d_programming\tango\import\tango\stdc\posix\unistd.d(77): Error: pid_t is used as a type
C:\d_programming\tango\import\tango\stdc\posix\unistd.d(78): Error: identifier 'pid_t' is not defined
C:\d_programming\tango\import\tango\stdc\posix\unistd.d(78): Error: pid_t is used as a type
C:\d_programming\tango\import\tango\stdc\posix\unistd.d(78): Error: cannot have parameter of type void
C:\d_programming\tango\import\tango\stdc\posix\unistd.d(82): Error: identifier 'ssize_t' is not defined
C:\d_programming\tango\import\tango\stdc\posix\unistd.d(82): Error: ssize_t is used as a type
*/

this is what I found in tango.stdc.posix.sys.types:

//
// Required
//
/*
blkcnt_t
blksize_t
dev_t
gid_t
ino_t
mode_t
nlink_t
off_t
pid_t
size_t
ssize_t
time_t
uid_t
*/

so, umm wth? how am I supposed to use sleep then? For now I'm using Thread.sleep

next issue

2.

module gc_test;

import tango.io.Stdout;
import tango.core.Thread;

class A {
	int x;
	
	this() {
		Stdout.formatln("Calling constructor of A");
	}
	~this() {
		Stdout.formatln("Calling destructor of A");
	}
}

void main() {
	
	A foo = new A;
	
	Thread.sleep(2);

	A bar = new A;	

	Thread.sleep(2);
	
}

output is:

Calling constructor of A
Calling constructor of A
Calling destructor of A

I believe there should be two calls for destructor, no?

and a variation on the theme which confuses me the most:

module gc_test;

import tango.io.Stdout;
import tango.core.Thread;
import tango.core.Memory;

class A {
	int x;
	
	this() {
		Stdout.formatln("Calling constructor of A");
	}
	~this() {
		Stdout.formatln("Calling destructor of A");
	}
}

void main() {
	
	GC.disable();
	
	A foo = new A;
	
	Thread.sleep(2);

	A bar = new A;	

	Thread.sleep(2);
	
}

output is the same as previous, I though now there shouldn't be ANY call to destructor, or if there is (exiting application scope) there should be, again, two of them, no?

and finally:

module gc_test;

import tango.io.Stdout;
import tango.core.Thread;
import tango.core.Memory;

class A {
	int x;
	
	this() {
		Stdout.formatln("Calling constructor of A");
	}
	~this() {
		Stdout.formatln("Calling destructor of A");
	}
}

void main() {
	
	GC.disable();
	
	A foo = new A;
	
	Thread.sleep(2);

	A bar = new A;	

	Thread.sleep(2);
	
	delete foo;
	delete bar;
	
}

output:

Calling constructor of A
Calling constructor of A
Calling destructor of A
Calling destructor of A

this works as it should.

Author Message

Posted: 04/08/08 21:03:23

that's weird with the usleep() problem ... you're using -version=Posix also? Try using Thread.sleep() in the meantime. .. The GC behavior shown is expected since it collects on an allocation request, and there's no promise of dtor invocation at termination time. GC.disable never did work in phobos, but used to be operational in Tango. Perhaps got broken when merging in walter's changes along the way?

Posted: 04/11/08 20:18:50

1. What OS are you using? uid_t, etc, are currently defined for linux, darwin (ie. OSX) and freebsd. That said, you should use Thread.sleep() to sleep anyway. I suggest only resorting to the C routines if (a) you're porting a C app, or (b) you have specific requirements which aren't met by the corresponding Tango API call.

2. I've noticed that the GC often won't collect the most recently constructed object, even if the reference has been reassigned. I think this is because there is likely a register somewhere that still contains the address of this object, but I haven't investigated enough to be sure. The Tango GC is much modified in some ways from the Phobos GC, but it's still the Phobos GC at heart and I haven't taken the time to figure out all of its little quirks. Regarding the GC.disable() bit--that only applies to implicit collections during an allocation. When the app exits, an explicit collection is performed to clean up as much old data as possible. This is likely what's collecting the object.