www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - auto objects, finally, and C signal

reply "Carlos Santander B." <carlos8294 msn.com> writes:
I want to know what you guys think about this behavior:

import signal;

void main ()
{
    signal.signal (SIGINT, cast(sigfunc) & al_fin );

    auto A a = new A;
    while (true) {}
}

extern (C)
void al_fin ( int parm )
{
    printf("bu\n");
    raise(SIGTERM);
}

class A
{
    ~this ()
    {
        printf("hey\n");
    }
}

It never prints "hey". If I change the auto class to a try-finally
construct, it's exactly the same. Is it because signal is a C function?

-----------------------
Carlos Santander Bernal
Jun 24 2004
parent reply J C Calvarese <jcc7 cox.net> writes:
Carlos Santander B. wrote:
 I want to know what you guys think about this behavior:
 
 import signal;
What's in the signal module? I re-wrote it, so I could compile it (DMD 0.94), and it doesn't behave like you mentioned. (It's probably because I messed something up with the removal of the signal module stuff.) void main () { auto A a = new A; al_fin(0); while (true) {} } extern (C) void al_fin ( int parm ) { printf("bu\n"); assert(0); } class A { ~this () { printf("hey\n"); } } Output... bu hey Error: AssertError Failure signal.d(14)
 void main ()
 {
     signal.signal (SIGINT, cast(sigfunc) & al_fin );
 
     auto A a = new A;
     while (true) {}
 }
 
 extern (C)
 void al_fin ( int parm )
 {
     printf("bu\n");
     raise(SIGTERM);
 }
 
 class A
 {
     ~this ()
     {
         printf("hey\n");
     }
 }
 
 It never prints "hey". If I change the auto class to a try-finally
 construct, it's exactly the same. Is it because signal is a C function?
 
 -----------------------
 Carlos Santander Bernal
-- Justin (a/k/a jcc7) http://jcc_7.tripod.com/d/
Jun 27 2004
parent "Carlos Santander B." <carlos8294 msn.com> writes:
"J C Calvarese" <jcc7 cox.net> escribió en el mensaje
news:cbnt8u$216q$1 digitaldaemon.com
| Carlos Santander B. wrote:
|| I want to know what you guys think about this behavior:
||
|| import signal;
|
| What's in the signal module?
|
| I re-wrote it, so I could compile it (DMD 0.94), and it doesn't behave
| like you mentioned. (It's probably because I messed something up with
| the removal of the signal module stuff.)
|
| ...
|
|| void main ()
|| {
||     signal.signal (SIGINT, cast(sigfunc) & al_fin );
||
||     auto A a = new A;
||     while (true) {}
|| }
||
|| extern (C)
|| void al_fin ( int parm )
|| {
||     printf("bu\n");
||     raise(SIGTERM);
|| }
||
|| class A
|| {
||     ~this ()
||     {
||         printf("hey\n");
||     }
|| }
||
|| It never prints "hey". If I change the auto class to a try-finally
|| construct, it's exactly the same. Is it because signal is a C function?
||
|| -----------------------
|| Carlos Santander Bernal
|
|
| --
| Justin (a/k/a jcc7)
| http://jcc_7.tripod.com/d/

signal is a normal, simplified version of signal.h, posted by someone whose
name I can't remember a long time ago (sorry for the indentation, or lack of
it). See below.
Anyway, my point was if auto objects should be destroyed (and finally blocks
be executed) even when catching signals.

[signal.d]

extern (C)
{

//constants
const int SIGINT=2;
const int SIGILL=4;
const int SIGFPE=8;
const int SIGSEGV=11;
const int SIGTERM=15;
const int SIGBREAK=21;
const int SIGABRT=22;

//just so my mind doesn't hurt
alias void function(int) sigfunc;

//predefined
const sigfunc SIG_DFL=cast(sigfunc)0;
const sigfunc SIG_IGN=cast(sigfunc)1;
const sigfunc SIG_SGE=cast(sigfunc)3;
const sigfunc SIG_ACK=cast(sigfunc)4;
const sigfunc SIG_ERR=cast(sigfunc)-1;

/* Function prototypes */
sigfunc signal(int sig, sigfunc handler);
int raise(int sig);

}


-----------------------
Carlos Santander Bernal
Jun 27 2004