Alien-libssh
view release on metacpan or search on metacpan
.claude/skills/perl-xs/references/objects-and-memory.md view on Meta::CPAN
Dereferencing `ST(0)` inside the XSUB is safe: the typemap's INPUT block has already
croaked unless `SvROK(sv) && SvMAGICAL(SvRV(sv))`, and OUTPUT overwrites `ST(0)`
only afterwards.
## When the C library frees your children behind your back
Some teardown calls free objects the Perl side still holds pointers to â
`foolib_close()` frees every handle the connection owns. A NULL check cannot see it:
the pointer is unchanged and now points at freed memory.
The fix is a **generation counter**. The parent counts the events that invalidate
children; each child stores the count it was born under:
```c
/* parent, before the invalidating call */
self->generation++;
foolib_close(self->conn);
/* child, before touching its handle */
static int foo_conn_stale(pTHX_ SV *conn_sv, unsigned int generation) {
MAGIC *mg = conn_sv && SvMAGICAL(conn_sv)
( run in 0.732 second using v1.01-cache-2.11-cpan-d01c6094234 )