Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:60911 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 9996 invoked from network); 21 Jun 2012 03:52:16 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 21 Jun 2012 03:52:16 -0000 Authentication-Results: pb1.pair.com smtp.mail=laruence@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=laruence@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.212.42 as permitted sender) X-PHP-List-Original-Sender: laruence@gmail.com X-Host-Fingerprint: 209.85.212.42 mail-vb0-f42.google.com Received: from [209.85.212.42] ([209.85.212.42:50841] helo=mail-vb0-f42.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 76/00-09612-E6A92EF4 for ; Wed, 20 Jun 2012 23:52:14 -0400 Received: by vbbfs19 with SMTP id fs19so121135vbb.29 for ; Wed, 20 Jun 2012 20:52:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:in-reply-to:references:from:date :x-google-sender-auth:message-id:subject:to:cc:content-type :content-transfer-encoding; bh=LAAnHr+imSsMY9O/OYaFdYHGB6PnJZS6+3Kusfhxfeo=; b=lJZdK4XObmzNenZ0apGNJT62FGttvw9/jBRZUkgPX7dXZL4fMDVSICV8j8pMXkqpdM wlARvGo6z8V3opyFeTjh4RT/OqxDoFWjgloWBiC48XwqOk5zNSdM87iBrhMAd8vFXpDY CuOYtg4k5iexv4B4tDUDvp2rtlXEBXje2nN7lNOyZj2gI3kJFb3wTdmY8RY+NXEbUXIf lqs9vOCr8PrisTIlFR4q1BRDGDSU0xbHxVf8EBHEBEe1UbFeIzgOJ9lAPzqSHE4qXxs1 rCa73Pl9+gLhtQ3uaRqUeGSpF+9KrS81g/8VeJ7cLQoRbTzedryvkkEH98Jvysl0+BPd HYAw== Received: by 10.52.98.8 with SMTP id ee8mr10447266vdb.58.1340250728263; Wed, 20 Jun 2012 20:52:08 -0700 (PDT) MIME-Version: 1.0 Sender: laruence@gmail.com Received: by 10.220.162.141 with HTTP; Wed, 20 Jun 2012 20:51:47 -0700 (PDT) In-Reply-To: References: Date: Thu, 21 Jun 2012 11:51:47 +0800 X-Google-Sender-Auth: 7kDsw_ubfjRfwqX6DxAIF1AhPFs Message-ID: To: Yader Hernandez Cc: PHP Developers Mailing List Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] destroying an array and using a new one From: laruence@php.net (Laruence) On Thu, Jun 21, 2012 at 11:39 AM, Yader Hernandez wrote: > I'm trying to reverse an array but I keep getting bizarre results, such a= s > seg faults > > 0x00000000006d0940 in gc_remove_from_buffer (zv=3D0x7fffffffb550) at > /home/yaderbh/php-5.4.3/Zend/zend_gc.h:189 > 189 root->next->prev =3D root->prev; > > or I'll get "PHP Fatal error: =C2=A0Allowed memory size of" errors when t= rying > 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. > > > =C2=A0 =C2=A0hash_table =3D Z_ARRVAL_P(intern->elements); > > > > =C2=A0 =C2=A0ALLOC_INIT_ZVAL(new_elements); > > =C2=A0 =C2=A0array_init(new_elements); > > > > =C2=A0 =C2=A0zend_hash_internal_pointer_reset_ex(hash_table, &pos); > > > > =C2=A0 =C2=A0i =3D intern->size; > > > > =C2=A0 =C2=A0while (zend_hash_get_current_data_ex(hash_table, (void **)&c= urrent, > &pos) =3D=3D SUCCESS) { > =C2=A0 =C2=A0 =C2=A0 =C2=A0tmp =3D **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 > > > > =C2=A0 =C2=A0 =C2=A0 =C2=A0zval_copy_ctor(&tmp); > > > > =C2=A0 =C2=A0 =C2=A0 =C2=A0add_index_zval(new_elements, i, &tmp); > > =C2=A0 =C2=A0 =C2=A0 =C2=A0i--; > > > > =C2=A0 =C2=A0 =C2=A0 =C2=A0zend_hash_move_forward_ex(hash_table, &pos); > > =C2=A0 =C2=A0} > > > > =C2=A0 =C2=A0zval_ptr_dtor(&intern->elements); > > > > =C2=A0 =C2=A0intern->elements =3D new_elements; > > > Can anyone spot the problem? --=20 Laruence =C2=A0Xinchen Hui http://www.laruence.com/