Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

AS 5.8 + PDK + threads + ActiveX

by Nitrox (Chaplain)
on Nov 23, 2002 at 19:22 UTC ( #215410=perlquestion: print w/replies, xml ) Need Help??

Nitrox has asked for the wisdom of the Perl Monks concerning the following question:

The short program below is a test stub that I've written to re-create an issue I'm having with an ActiveX control I'm developing with ActiveState Perl 5.8 and the Perl Dev Kit (both beta).

When compiled, the stub runs fine until I exit the 3rd-party app that called CreateObject() on my DLL. At that point Dr.Watson pops and the application crashes.

Debugging an ActiveX control is proving to be a bit difficult and is the reason why I redirect stdout/err to a file at the beggining of my test stub.

This is the basic "life" of my DLL:
1) VB app calls CreateObject() on my DLL
2) VB app calls InitIO()
This is where I spawn a thread that just sits in a while() loop and detach it.
3) When the VB app is closed it calls ShutdownIO()
This is where I set the shared variable $running to '0' so that the child will exit it's while() loop and allow the thread to close.

These are the log results with the code as is below:

A thread exited while 2 other threads were still running.

You'll notice that the DLL object is destroyed before the child thread has a chance to exit the while() loop. The 'print "Exiting...\n";' never gets evaluated and it displays an message about other threads exisiting.

If I uncomment the sleep() call in ShutdownIO() then the debug shows that the child thread exits it's while() loop...

Free to wrong pool 1283b00 not 849b58 during global destruction.

But then the "Free to wrong pool..." messages appear.

Does anyone see something painfully obvious that I'm doing wrong?


use threads; use threads::shared; use strict; use warnings; use Win32; open(STDOUT, ">>cp_std.log") || die "Can't redirect stdout"; open(STDERR, ">&STDOUT") || die "Can't dup stdout"; select(STDERR); $| = 1; select(STDOUT); $| = 1; my $running = 0; share($running); exit; sub main_loop { while($running){ Win32::Sleep(250); } print "Exiting...\n"; } sub ShutdownIO { print "ShutdownIO()...\n"; $running = 0; #Win32::Sleep(2000); return; } sub InitIO { if(!$running){ $running = 1; my $poll_thrd = threads->create("main_loop",""); $poll_thrd->detach; } return(""); }

Replies are listed 'Best First'.
Re: AS 5.8 + PDK + threads + ActiveX
by pg (Canon) on Nov 23, 2002 at 20:01 UTC
    Everything seems just normal to me. Couple of points:

    • Your code is not complete. You have three subs, but you never called InitIO, and Shutdown IO. (It is fine with main_loop, if you call InitIO, it will then call main_loop). I assume that's just copy/paste type problem. So I changed your exit statement to:
      InitIO(); ShutdownIO(); exit;
    • You said, the print "Exiting ..." never being printed. That is quite normal. When you called exit, you exit the process not the thread, so your main_loop sub, which is running on a detached thread, just being suddenly stoped before it was evaluated to its end. exit always stop the process, not the calling thread, doesn't matter whether the calling thread is the main thread, or a created thread.
    • You have a detached thread, and you didn't try anything to sync your threads, so don't expect any sync'd behavior among your threads.
    • If you want sync among threads, play with join, cond_wait, cond_signal, cond_broadcast.
    Re: AS 5.8 + PDK + threads + ActiveX
    by Nitrox (Chaplain) on Nov 23, 2002 at 21:13 UTC
      PG, the code isn't supposed to have any calls to StartIO() or ShutdownIO(), those are methods that are invoked by the VB application after it creates an object out of my DLL.

      I will look more in syncing the threads, I need a way to delay 'global destruction' of the process until the child has exited. I can't use 'join' since that halts the return from InitIO() until the child thread returns and the VB app is looking for a return after calling that method before it continues.


    Log In?

    What's my password?
    Create A New User
    Domain Nodelet?
    Node Status?
    node history
    Node Type: perlquestion [id://215410]
    Approved by simon.proctor
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others meditating upon the Monastery: (2)
    As of 2023-05-30 23:57 GMT
    Find Nodes?
      Voting Booth?

      No recent polls found