Since you cannot change the ownership of an SV from one interpreter to another, this creates the SV in the receiver.

#! perl -slw use strict; package O; #use Inline qw( FORCE NOISY NOCLEAN ); use Inline C => <<'END_C', NAME => 'xso'; typedef struct { SV *sv; } O; void *mem( size_t size ) { void *p; Newx( p, size, char ); return p; } SV *new( char *package ) { O *o = (O*)mem( sizeof( O ) ); SV *oh = newSVuv( (UV)o ); SV *rv = newRV( oh ); o->sv = newSV(0); sv_bless( rv, gv_stashpv( package, 0 ) ); SvREADONLY_on( oh ); SvREADONLY_on( rv ); printf( "Creating oh:%p o:%p o->sv:%p\n", oh, o, o->sv ); return rv; } int set( SV *rv, SV *in ) { SV *oh = SvRV( rv ); O *o = (O*)SvUV( oh ); sv_setsv( o->sv, in ); printf( "Setting oh:%p o:%p o->sv:%p\n", oh, o, o->sv ); return 1; } SV *get( SV *rv ) { SV *oh = SvRV( rv ); O *o = (O*)SvUV( oh ); printf( "Getting from oh:%p o:%p o->sv:%p\n", oh, o, o->sv ); return newSVsv( o->sv ); } void DESTROY( SV *rv ) { SV *oh = SvRV( rv ); O *o = (O*)SvUV( oh ); printf( "Destroying oh:%p o:%p o->sv:%p\n", oh, o, o->sv ); /* TODO: We can only destroy oh and o in the original. SvREFCNT_dec( o->sv ); free(o); */ } END_C package main; use threads; use Devel::Peek; my $o = O->new(); $o->set( "abcde" ); print $o->get(); print "\nthreaded\n"; async { $o->set( "12345" ); }->join; #<>; print $o->get(); #<>;

In reply to Re^7: semi-panic: attempt to dup freed string? by ikegami
in thread semi-panic: attempt to dup freed string? by BrowserUk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.