[prev in list] [next in list] [prev in thread] [next in thread] 

List:       php-internals
Subject:    Re: [PHP-DEV] destroying an array and using a new one
From:       Yader Hernandez <yader.hernandez () gmail ! com>
Date:       2012-06-21 5:05:01
Message-ID: CABaGD72K4MiRiCjYC0zs7Dce-gOxq5KWa6C1ftic=RJYGhZnJQ () mail ! gmail ! com
[Download RAW message or body]


On Wed, Jun 20, 2012 at 8:51 PM, Laruence <laruence@php.net> wrote:

> On Thu, Jun 21, 2012 at 11:39 AM, Yader Hernandez
> <yader.hernandez@gmail.com> wrote:
> > I'm trying to reverse an array but I keep getting bizarre results, such
> as
> > seg faults
> >
> > 0x00000000006d0940 in gc_remove_from_buffer (zv=0x7fffffffb550) at
> > /home/yaderbh/php-5.4.3/Zend/zend_gc.h:189
> > 189 root->next->prev = root->prev;
> >
> > or I'll get "PHP Fatal error:  Allowed memory size of" errors when trying
> > to manipulate the array in a script.
> >
> > Basically what I'm doing is creating a pointer to zval, allocating and
> > creating the zval into an array, traversing the existing array and
> > calling add_index_zval. The position of the index is the current size and
> > work my way down.
> >
> >
> >    hash_table = Z_ARRVAL_P(intern->elements);
> >
> >
> >
> >    ALLOC_INIT_ZVAL(new_elements);
> >
> >    array_init(new_elements);
> >
> >
> >
> >    zend_hash_internal_pointer_reset_ex(hash_table, &pos);
> >
> >
> >
> >    i = intern->size;
> >
> >
> >
> >    while (zend_hash_get_current_data_ex(hash_table, (void **)&current,
> > &pos) == SUCCESS) {
> >        tmp = **current;
>
> Hi:
> tmp is a stack allocted variable.  when this scope is dead,  *(&tmp)
> is undefined value.
>
> you should use a heap allocated variable, or just add_ref the current zval
>
> thanks
> >
> >
> >
> >        zval_copy_ctor(&tmp);
> >
> >
> >
> >        add_index_zval(new_elements, i, &tmp);
> >
> >        i--;
> >
> >
> >
> >        zend_hash_move_forward_ex(hash_table, &pos);
> >
> >    }
> >
> >
> >
> >    zval_ptr_dtor(&intern->elements);
> >
> >
> >
> >    intern->elements = new_elements;
> >
> >
> > Can anyone spot the problem?
>
>
>
> --
> Laruence  Xinchen Hui
> http://www.laruence.com/
>

Of course, I forgot to use Z_ADDREF_P. But I'm still having an issue. All
of the elements in the new array are now null. zend_hash_index_find is
returning SUCCESS.

Apart from adding Z_ADDREF_P, is there anything I should be doing?

thanks


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic