Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: [I::C] Why does it segfault? (NDEBUG?)

by tye (Sage)
on Mar 15, 2012 at 02:58 UTC ( [id://959700]=note: print w/replies, xml ) Need Help??


in reply to [I::C] Why does it segfault?

It looks to me like you do nothing to upgrade the bare SV to be of sufficient type to hold a PV. But then assert(SvTYPE(sv) >= SVt_PV); should have fired. But perhaps you are building in an environment that doesn't have assert()s enabled.

Also, the ref you return on the stack is not mortalized which probably means a memory leak.

Update: Yep, Eliya's reply confirms my first paragraph.

- tye        

Replies are listed 'Best First'.
Re^2: [I::C] Why does it segfault? (NDEBUG?)
by BrowserUk (Patriarch) on Mar 15, 2012 at 06:13 UTC
    you do nothing to upgrade the bare SV to be of sufficient type to hold a PV

    According to the docs:

    newSV

    Creates a new SV. A non-zero len parameter indicates the number of bytes of preallocated string space the SV should have. An extra byte for a trailing NUL is also reserved. (SvPOK is not set for the SV even if string space is allocated.) The reference count for the new SV is set to 1.

    Which kind of implies that it creates a PV-type SV -- given that it offers to preallocate some space to the PV.

    But I guess that it just fails to mention that if you give a zero parameter, not only does it not preallocate the space, it doesn't allocate the PV-type fields either :(

    perhaps you are building in an environment that doesn't have assert()s enabled.

    You're right. I had the session configured for /release instead of /debug.

    the ref you return on the stack is not mortalized

    I believe I::C will take care of that for me.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    The start of some sanity?

      Ah. I was reading code not documentation. :)

      SV * Perl_newSV(pTHX_ const STRLEN len) { dVAR; register SV *sv; new_SV(sv); if (len) { sv_upgrade(sv, SVt_PV); SvGROW(sv, len + 1); } return sv; }
      I believe I::C will take care of that for me.

      Indeed. Inline::C-Cookbook makes that clear (though doesn't take the credit):

      In this example the sv_2mortal call gets done under the hood by XS, because we declared the return type to be SV*.

      Thanks.

      - tye        

      Which kind of implies that it creates a PV-type SV

      ...when given "a non-zero len parameter", which doesn't apply to your code. You have a gave a zero len parameter, for which its documentation only says it "Creates a new SV." It creates the most basic of SVs:

      use Inline C => <<'__EOI__'; void testing() { SV* sv = newSV(0); sv_dump(sv); SvREFCNT_dec(sv); } __EOI__ testing();
      SV = NULL(0x0) at 0x8eec248 REFCNT = 1 FLAGS = ()

      This creates a PV:

      newSVpvn("", 0)

      I believe I::C will take care of that for me.

      Yes, though it's actually XS by means of the typemap that does it.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://959700]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2024-04-23 11:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found