Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Perl Internals Question

0 views
Skip to first unread message

Simon Parsons

unread,
Mar 14, 1995, 10:15:52 AM3/14/95
to

I am just starting down the road that "...goes ever on and on, down
from the door where it began".

Hopefully a simple question on the guts of perl - if I compile this
piece of code and run it, at the point of of the
'av_store(av, 0, sv2);' I get 'Attempt to free unreferenced scalar.'
For some reason after the av_unshift(), the first element of the array
is valid enough to be 'SvREFCNT_dec()'ed but it has a reference count
of 0. This occurs with both perl5.000 and perl5.001.

Can someone please tell me what I'm doing wrong.

------------------------------------------------------------
#include "INTERN.h"
#include "perl.h"

int
main(argc, argv, env)
int argc;
char **argv;
char **env;
{
SV* sv1 = newSViv(1);
SV* sv2 = newSViv(2);
AV* av = newAV();

av_unshift(av, 1);
av_store(av, 0, sv2);

return 0;
}
------------------------------------------------------------

Thanks in advance
Simon
--
===========================================================================
Simon Parsons (sim...@fulcrum.co.uk)
"You have to be trusted by the people that you lie to,
So that when they turn their backs on you,
You get the chance to put the knife in."
Pink Floyd
===========================================================================
FTEL., Solihull Parkway, Birmingham Business Park, Birmingham, ENGLAND.

Larry Wall

unread,
Mar 18, 1995, 6:27:59 PM3/18/95
to
In article <SIMONP.95M...@fabius.fulcrum.co.uk>,
Simon Parsons <sim...@fulcrum.co.uk> wrote:
:
: I am just starting down the road that "...goes ever on and on, down

: from the door where it began".
:
: Hopefully a simple question on the guts of perl - if I compile this
: piece of code and run it, at the point of of the
: 'av_store(av, 0, sv2);' I get 'Attempt to free unreferenced scalar.'
: For some reason after the av_unshift(), the first element of the array
: is valid enough to be 'SvREFCNT_dec()'ed but it has a reference count
: of 0. This occurs with both perl5.000 and perl5.001.
:
: Can someone please tell me what I'm doing wrong.
:
: ------------------------------------------------------------
: #include "INTERN.h"
: #include "perl.h"
:
: int
: main(argc, argv, env)
: int argc;
: char **argv;
: char **env;
: {
: SV* sv1 = newSViv(1);
: SV* sv2 = newSViv(2);
: AV* av = newAV();
:
: av_unshift(av, 1);
: av_store(av, 0, sv2);
:
: return 0;
: }
: ------------------------------------------------------------

You're using values that haven't been initialized because you've never
called perl_construct. In particular, your av_unshift() puts a reference
to sv_undef in the beginning av, and the av_store() overwrites that.
Since sv_undef has not been initialized, it doesn't have the READONLY bit
set, and since the READONLY bit is not set, sv_free() treats it as
an ordinary value, and attempts to free it. You're just lucky that
the reference count is already 0, or it would try to free unmalloced
memory and core dump, instead of warning you politely.

Many Perl routines expect to be run in the context of "the current
interpreter". If you don't supply one, you'll have an interesting life.

Larry

0 new messages