Forum: GNU Radio What conditions will cause block destructor to be skipped?

0d057c642b38689bfa090206eec844b8?d=identicon&s=25 Monahan-Mitchell, Tim (Guest)
on 2013-10-12 00:25
(Received via mailing list)
Rarely, I'm seeing evidence that my custom source block destructor is
not being called.

My flowgraph was built with "No GUI" and "Prompt for Exit" options (GR
3.7.1+)

The condition is that my source block's work function has moved to a
state returning WORK_DONE, and will stay in that state.

When I press Enter to end the flowgraph, I sometimes don't see the usual
ending printf() outputs. Fortunately, it does not happen often.

This article cites such a case:
http://lists.gnu.org/archive/html/discuss-gnuradio...,
but my flowgraph only has my custom source block and a File Sink block.

Regards,
Tim
7d89a70df32c0ae27c1235016f9e5441?d=identicon&s=25 "Marcus Müller" <marcus@hostalia.de> (Guest)
on 2013-10-12 11:01
(Received via mailing list)
Hi Tim,

you're entering a minefield there...
Since you've built your flowgraph using GRC, you have a
python-constructed flowgraph. That's why things start to get confusing
for the c++ developer.
First of all, destructors of python objects are called whenever the
python runtime feels like it - this often happens when you do something
like
object_name = None
but it doesn't necessarily has to happen right away. The Garbage
Collector can choose when to do it, and sometimes, it decides not to do
it at all - if your program terminates first, this might be the case.
Furthermore, if it decides that at the end of runtime, it should
destruct its output stream file handles before destructing your block,
your printf output might be lost.
Then: You have a C++ object wrapped by SWIG. So, what you're seeing in
Python is not actually an instance of your block, it is a SWIG object
holding a reference to such an instance. Usually, when that Swig
Object's python destructor is called, it should delete the block
instance, too, but then again, if I knew what really happens inside
SWIG, I'd feel a lot wiser.
Lastly: Destructors doing actual work is a tricky thing. Mostly,
because, what does the runtime (both, c++ or python) do when a
destructor throws an exception? In that case, should the runtime ignore
it and just think "meh, whatever, it's dead anyway" or should it cause
the program to exit with a failure state? And: what if a C++ destructor
fails that got called by the python garbage collector? That shouldn't
crash the global python interpreter, should it?

So these are the pitfalls I could think of. Maybe the situation is a lot
easier, though :) Post some code, maybe a github gist or a pastebin!

Happy Hacking,
Marcus
0d057c642b38689bfa090206eec844b8?d=identicon&s=25 Monahan-Mitchell, Tim (Guest)
on 2013-10-12 18:50
(Received via mailing list)
> you're entering a minefield there...

Thanks for helping me navigate :)

> First of all, destructors of python objects are called whenever the python
runtime feels like it - this often happens when you do something like 
object_name
= None but it doesn't necessarily has to happen right away.
> The Garbage Collector can choose when to do it, and sometimes, it decides not to
do it at all - if your program terminates first, this might be the case.
> Furthermore, if it decides that at the end of runtime, it should destruct its
output stream file handles before destructing your block, your printf output 
might
be lost.

So I know it isn't just dropping printf output, as other "clean-up" code
does not execute.

> So these are the pitfalls I could think of. Maybe the situation is a lot easier,
though :) Post some code, maybe a github gist or a pastebin!

All excellent ideas, especially the GC that rightfully  turns up its
nose at any destructor complaints.

So it is good to know that it is a bit out of my hands, but I can
certainly move my "clean-up" code to the point where the work() method
signals WORK_DONE. Sorry, I can't possibly air my dirty laundry on a
pastebin :(.
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.