Re: Help with growing memory

2006-03-24 Thread Salvador Fandino
Marcus Holland-Moritz wrote:
 On 2006-03-23, at 15:22:01 +, Alberto Manuel Brandão Simões wrote:
 
 
Hi

I am working with XS, and creating an array. It is basically something like:

 array = newAV();
 av_push(array, newSVuv(dictionary_get_occ(D, wid)));

 for (j = 0; j  MAXENTRY; j++) {
  twid = 0;
  prob = 0.0;
 twid = dictionary_get_id(D, wid, j);
 if (twid) {
  prob = dictionary_get_val(D, wid, j);
  tword = word_list_get_by_id(T, twid);
  av_push(array, newSVpv(tword, strlen(tword)));
  av_push(array, newSVnv(prob));
 }
 }
 RETVAL = newRV_noinc((SV*)array);

The question is: the objects created here (newSVpv, newSVnv and the 
array) are freed in case they are no more used?
 
 
 Yes. :-)
 
 When in doubt, it's usually a good idea to check the reference counts
 with Devel::Peek:

also, inside the gdb debugger, SVs, AVs and HVs can be dumped with this
command:

  call Perl_sv_dump(my_value)

or on a threaded perl

  call Perl_sv_dump(Perl_get_context(), my_value)


It's easy to set a breakpoint at the start of the XS subroutine (the
real name appears on the .c file generated from the XS), and then
single-step through the code. BTW, having a version of perl compiled
with debugging support and with optimizations turned off also helps.

Cheers,

  - Salva


Re: Help with growing memory

2006-03-23 Thread Marcus Holland-Moritz
On 2006-03-23, at 15:22:01 +, Alberto Manuel Brandão Simões wrote:

 Hi
 
 I am working with XS, and creating an array. It is basically something like:
 
  array = newAV();
  av_push(array, newSVuv(dictionary_get_occ(D, wid)));
 
  for (j = 0; j  MAXENTRY; j++) {
   twid = 0;
   prob = 0.0;
  twid = dictionary_get_id(D, wid, j);
  if (twid) {
   prob = dictionary_get_val(D, wid, j);
   tword = word_list_get_by_id(T, twid);
   av_push(array, newSVpv(tword, strlen(tword)));
   av_push(array, newSVnv(prob));
  }
  }
  RETVAL = newRV_noinc((SV*)array);
 
 The question is: the objects created here (newSVpv, newSVnv and the 
 array) are freed in case they are no more used?

Yes. :-)

When in doubt, it's usually a good idea to check the reference counts
with Devel::Peek:

  use Devel::Peek;
  $a = some_xsub_call();
  Dump $a;

For example:

[EMAIL PROTECTED] ~ $ perl -MConvert::Binary::C -MDevel::Peek -e'$a = 
Convert::Binary::C-new-parse(typedef int a[];)-unpack(a, xx12); Dump 
$a'
SV = RV(0x819ada0) at 0x8148fb4
  REFCNT = 1
  FLAGS = (ROK)
  RV = 0x8148e1c
  SV = PVAV(0x814dedc) at 0x8148e1c
REFCNT = 1
FLAGS = ()
IV = 0
NV = 0
ARRAY = 0x81612e8
FILL = 2
MAX = 3
ARYLEN = 0x0
FLAGS = (REAL)
Elt No. 0
SV = IV(0x815e80c) at 0x8148fd8
  REFCNT = 1
  FLAGS = (IOK,pIOK)
  IV = 2021161080
Elt No. 1
SV = IV(0x815e810) at 0x8163e40
  REFCNT = 1
  FLAGS = (IOK,pIOK)
  IV = 2021161080
Elt No. 2
SV = IV(0x815e814) at 0x8163df8
  REFCNT = 1
  FLAGS = (IOK,pIOK)
  IV = 2021161080

If all refcounts are 1, the objects will be freed as soon as the last
reference to them ($a) vanishes.

Marcus

-- 
This file will self-destruct in five minutes.