Professional Web Applications Themes

PHP5, foreach and memory corruption - PHP Development

Hi! I have just fixed my CMS so it works on PHP5 (it worked without problems on PHP4 but gave weird errors on PHP5). The problem lied in this construct: echo ("BEFORE: ".count($this->someLargeArrayWithObjects)); // outputs 11 foreach ($this->someLargeArrayWithObjects as $key=>$value) { $a=$value['object']->someFunction(); }; echo ("AFTER: ".count($this->someLargeArrayWithObjects)); // outputs _1_ (one)! This is of course pseudo-code - and no, function was not changing anything. This solved the problem: echo ("BEFORE: ".count($this->someLargeArrayWithObjects)); // outputs 11 $keys=array_keys($this->someLargeArrayWithObjects); foreach ($keys as $key) { $value=&$this->someLargeArrayWithObjects[$key]; $a=$value['object']->someFunction(); }; echo ("AFTER: ".count($this->someLargeArrayWithObjects)); // outputs 11 Has anybody else had these kind of problems? The zend.ze1_compatibility_mode directive didn't ...

  1. #1

    Default PHP5, foreach and memory corruption

    Hi!

    I have just fixed my CMS so it works on PHP5 (it worked without problems on
    PHP4 but gave weird errors on PHP5). The problem lied in this construct:

    echo ("BEFORE: ".count($this->someLargeArrayWithObjects)); // outputs 11
    foreach ($this->someLargeArrayWithObjects as $key=>$value)
    {
    $a=$value['object']->someFunction();
    };
    echo ("AFTER: ".count($this->someLargeArrayWithObjects)); // outputs _1_
    (one)!

    This is of course pseudo-code - and no, function was not changing anything.
    This solved the problem:

    echo ("BEFORE: ".count($this->someLargeArrayWithObjects)); // outputs 11
    $keys=array_keys($this->someLargeArrayWithObjects);
    foreach ($keys as $key)
    {
    $value=&$this->someLargeArrayWithObjects[$key];
    $a=$value['object']->someFunction();
    };
    echo ("AFTER: ".count($this->someLargeArrayWithObjects)); // outputs 11

    Has anybody else had these kind of problems? The zend.ze1_compatibility_mode
    directive didn't help...
    I solved it now and it might even be more effective (memory-wise), but still
    - I'd like to know if it's me or something else.

    Another thing: is there a nicer way to traverse an array without copying the
    values? All of the foreach(), current(), next() and similar do that - but I
    want to traverse an array just by referencing the values.

    Regards,

    Anze
    Anze Guest

  2. #2

    Default Re: PHP5, foreach and memory corruption

    > Another thing: is there a nicer way to traverse an array without copying 

    Of course, I'm looking for solution that would work in PHP4 too - in PHP5
    you can use foreach with '&' before $value.

    Regards,

    Anze
    Anze Guest

  3. #3

    Default Re: PHP5, foreach and memory corruption

    Anze wrote: 
    >
    >
    > Of course, I'm looking for solution that would work in PHP4 too - in PHP5
    > you can use foreach with '&' before $value.
    >[/ref]

    And you can't use & in PHP4?

    Stefan
     
    Stefan Guest

  4. #4

    Default Re: PHP5, foreach and memory corruption

    >> Of course, I'm looking for solution that would work in PHP4 too - in PHP5 
    > And you can't use & in PHP4?[/ref]

    No - doentation for foreach construct says:

    "As of PHP 5, you can easily modify array's elements by preceding $value
    with &. This will assign reference instead of copying the value. "

    I guess that means I can't use that in PHP4... A lot of my code runs on PHP4
    so I must be sure I don't make incompatible changes.

    Regards,

    Anze
    Anze Guest

  5. #5

    Default Re: PHP5, foreach and memory corruption

    Anze wrote: 
    >>
    >>And you can't use & in PHP4?[/ref]
    >
    >
    > No - doentation for foreach construct says:
    >
    > "As of PHP 5, you can easily modify array's elements by preceding $value
    > with &. This will assign reference instead of copying the value. "
    >
    > I guess that means I can't use that in PHP4... A lot of my code runs on PHP4
    > so I must be sure I don't make incompatible changes.
    >
    > Regards,[/ref]

    Ok I see.

    Stefan
     
    Stefan Guest

  6. #6

    Default Re: PHP5, foreach and memory corruption

    Anze wrote:
     

    http://php.paco.net/manual/en/language.oop5.iterations.php

    this might help
    Kleist Guest

  7. #7

    Default Re: PHP5, foreach and memory corruption

    Anze wrote:
     
    <snip> 

    It doesn't really solve the problem but did you try:

    foreach ($this->someLargeArrayWithObjects as $key=>$value)
    {
    $a=someLargeArrayWithObjects['key']['object']->someFunction();
    }

    C.
    Colin Guest

  8. #8

    Default Re: PHP5, foreach and memory corruption

    > It doesn't really solve the problem but did you try: 


    Probably you meant:

    $a=someLargeArrayWithObjects[ $key ]['object']->someFunction();


    Hilarion
    Hilarion Guest

  9. #9

    Default Re: PHP5, foreach and memory corruption

    > $a=someLargeArrayWithObjects[ $key ]['object']->someFunction();

    Yes, I did - just a typo, I have it right in the code.

    Regards,

    Anze
    Anze Guest

  10. #10

    Default Re: PHP5, foreach and memory corruption

    > Probably you meant: 

    I see, it wasnt MY typo... ;)

    Colin: I did try this but it didn't change anything.

    Regards,

    Anze
    Anze Guest

  11. #11

    Default Re: PHP5, foreach and memory corruption

    Kleist wrote: 
    >
    > http://php.paco.net/manual/en/language.oop5.iterations.php
    >
    > this might help[/ref]

    Thanks, though I was hoping for PHP4 & PHP5 compatible solution - my code is
    being hosted on different providers.
    I looked around and I think there is no such thing in PHP4 - pity.

    Regards,

    Anze
    Anze Guest

  12. #12

    Default Re: PHP5, foreach and memory corruption

    > > Probably you meant: 
    >
    > I see, it wasnt MY typo... ;)[/ref]

    True :) Sorry.


    Hilarion
    Hilarion Guest

  13. #13

    Default Re: PHP5, foreach and memory corruption

    "Anze" <net> wrote in message
    news:yDoOe.1653$siol.net... [/ref][/ref]
    copying [/ref][/ref]
    that - 
    > >
    > > http://php.paco.net/manual/en/language.oop5.iterations.php
    > >
    > > this might help[/ref]
    >
    > Thanks, though I was hoping for PHP4 & PHP5 compatible solution - my code[/ref]
    is 

    You could always do it both ways based on the 'phpversion()' function or
    PHP_VERSION constant...

    $version = PHP_VERSION;
    if ($version[0] == '4')
    {
    php4 version here
    }
    elseif ($version[0] == '5')
    {
    php5 version here
    }


    Norm
    ---
    FREE Avatar hosting at www.easyavatar.com



    Norman Guest

Similar Threads

  1. Memory corruption - SDK 7
    By Fred in forum Adobe Acrobat SDK
    Replies: 0
    Last Post: March 5th, 05:08 PM
  2. #40617 [NEW]: memory corruption
    By slacouette at edma dot com in forum PHP Bugs
    Replies: 2
    Last Post: February 24th, 03:35 AM
  3. #24834 [Csd->Bgs]: Memory corruption bug in object destructor
    By sniper@php.net in forum PHP Development
    Replies: 0
    Last Post: July 28th, 02:19 PM
  4. #24834 [Opn->Csd]: Memory corruption bug in object destructor
    By sniper@php.net in forum PHP Development
    Replies: 0
    Last Post: July 28th, 10:07 AM
  5. #24659 [Ver]: Crash due to some internal memory corruption (?)
    By sniper@php.net in forum PHP Development
    Replies: 0
    Last Post: July 18th, 05:36 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139