Refactored the previous memleak fix

to avoid usage of a freed pointer. Thanks Laruence )
Merge branch 'PHP-5.4' into PHP-5.5

* PHP-5.4:
  Refactored the previous memleak fix
Merge branch 'PHP-5.5'

* PHP-5.5:
  Refactored the previous memleak fix