[PHP-SRC] The memory leak processing idea of PHP extension

Source: Internet
Author: User
Tags php compiler

The content is php5.6.14 for example.

One. The memory leaks is generated when the function is encapsulated.

[[email protected] www]$ PHP2. php [122,3333][tue JulTen  the: the: the  .] Script:'/home/www/2.php'/HOME/WEICHEN/DOWNLOADS/PDONER/PDONER.C ( the): Freeing0x7f86b52f79f8( +bytes), script=/home/www/2. Php[tue JulTen  the: the: the  .] Script:'/home/www/2.php'/home/weichen/downloads/php-5.6. -/ext/standard/string. C (1161): Freeing0x7f86b52f7b60( -bytes), script=/home/www/2. PHP= = Total2Memory leaks detected = = =

php compiler on--enable-debug, if there is a memory leak in the extension, there will be a corresponding prompt. The memory leak problem is quite disturbing.

Why is there a memory leak? It is your function that has been requesting memory to do something, and the function has not freed up memory after completion.

There are many Hello World programs on the Internet, basically do not talk about memory processing, even if a slight modification, it can not be used for real projects.

So freeing up memory is also a key point for the underlying program. Now analyze the above information to detect a total of 2 memory leaks.

Section (1):

Ten : []  Script:  '/home/www/2.php' /HOME/WEICHEN/DOWNLOADS/PDONER/PDONER.C ():  0x7f86b52f79f8 (bytes ), script=/home/www/2. php

Tip We PDONER.C (83) have a problem, back to the program is Make_std_zval (glue); To glue initialization no problem, the problem is not released, it is easy to think of the release of Glue.

zval_ptr_dtor (&glue);

Compile and install still have a problem, a paragraph error is generally incorrect pointer use:

[[email protected] www]$ PHP2. php [Tue JulTen  -: -: -  .] Script:'/home/www/2.php'---------------------------------------/home/weichen/downloads/php-5.6. -/zend/zend_execute.h ( -): Block0x7fb3f93459c8Status:/home/weichen/downloads/php-5.6. -/ZEND/ZEND_VARIABLES.C (Panax Notoginseng): Actual location is relayedInvalid pointer: ((thread_id=0x007a7c7a)! = (expected=0x06567840) ) Segmentation fault (core dumped)

Zval_ptr_dtor use what is the attention? It's a good idea to look at the usage in the kernel first.

There is such a use in zend_api.h:

#defineZval_zval (z, zv, copy, dtor) do {\Zval*__z =(z); Zval*__zv =(ZV);                     Zval_copy_value (__z, __zv); if(copy) {zval_copy_ctor (__z); }                                               if(dtor) {if(!copy)                            {zval_null (__ZV); } zval_ptr_dtor (&__zv); }                                           }  while(0)

Note that zval_null (__ZV) is called to NULL before calling Zval_ptr_dtor to destroy __zv.

So we set the glue to NULL in this way.

zval_null (glue); Zval_ptr_dtor (&glue);

Compile and run, you can see that there is only one hint left.

Section (2):

Ten : []  Script:  '/home/www/2.php' /home/weichen/downloads/php-5.6. /ext/standard/string. C (1161):  0x7f86b52f7b60  ( bytes), script=/home/www/2. php

At that time, this has not been resolved, the first to continue to look down, and then look back together to see the solution.

Two. The memory leaks is generated when the class is encapsulated.

Pdoner_errs_* are defined constants, and the following are versions without memory leaks:

/*{{{proto public errs::__construct (void)*/Php_method (errs, __construct) {Zval*msg;    Make_std_zval (msg);    Array_init (msg); Add_index_string (msg, PDONER_ERRS_SUCC,"Success",1); Add_index_string (msg, Pdoner_errs_fail,"failed",1); Add_index_string (msg, PDONER_ERRS_EXCEP,"Exception",1); Add_index_string (msg, Pdoner_errs_unknow,"Unknown",1);    Zend_update_property (Errs_ce, Getthis (), Zend_strl (pdoner_errs_property_name_msg), MSG tsrmls_cc); Zval_ptr_dtor (&msg);/*add_index_string (msg, PDONER_ERRS_SUCC, "success", 0);    Add_index_string (msg, pdoner_errs_fail, "failure", 0);    Add_index_string (msg, PDONER_ERRS_EXCEP, "abnormal", 0);    Add_index_string (msg, Pdoner_errs_unknow, "Unknown", 0); ADD_PROPERTY_ZVAL_EX (Getthis (), pdoner_errs_property_name_msg, sizeof (pdoner_errs_property_name_msg), MSG TSRMLS_ CC);*/}/* }}} */

The properties of an extension class cannot directly initialize an array and an object, so you can only manipulate it in the same way as you modify the property, either within the constructor or Minit stage.

Note that because the property assignment is in the construct phase, if there is no instantiation of the class, there is no value when extending the internal zend_read_property.

Zend_update_property The second parameter is the current object, so there is no way to use it in the Minit stage, the above usage references the Yaf-2.3.5:

 php_method (Yaf_config_ini, __construct) {zval  *filename, *section = NULL;  if  (Zend_parse_parameters (Zend_num_args () tsrmls_cc,  " z|z  " , &filename, &        section) == FAILURE) {zval  *prop;        Make_std_zval (prop);        Array_init (prop);        Zend_update_property (Yaf_config_ini_ce, Getthis (), Zend_strl (yaf_config_propert_name), prop TSRMLS_CC);        Zval_ptr_dtor ( &prop);     return  ; } ( void  ) Yaf_config_ini_instance ( Getthis (), filename, section tsrmls_cc);}  

Under non-ideal conditions:

1). In our constructor, if a non-duplicate parameter is passed: add_index_string (msg, PDONER_ERRS_SUCC, "Success", 0);

Call Errs:: $msg can output content successfully, but there is a segment error:

[Thu JulTen  -:Geneva: -  .] Script:'/home/www/2.php'---------------------------------------/home/weichen/downloads/php-5.6. -/zend/zend_execute.h ( -): Block0x7fbf5ed4bb4eStatus:/home/weichen/downloads/php-5.6. -/ZEND/ZEND_VARIABLES.C (Panax Notoginseng): Actual location is relayed Invalid pointer: ((thread_id=0x646f6c70)! = (expected=0x6bf6e840) ) Segmentation fault (core dumped)

Reference:when to duplicate string using add_index_string?

Http://grokbase.com/t/php/php-dev/01285y74sp/when-to-duplicate-string-using-add-index-string

If there are other places to use, copy one out; no other place to use, duplicate fill 0.

2). When the comment snippet code is enabled, the output can be successful, but there are 11 memory leaks:

[Thu JulTen  -: .: About  .] Script:'/home/www/2.php'/home/weichen/downloads/php-5.6. -/ZEND/ZEND_API.C (1369): Freeing0x7f1900d44820( thebytes), script=/home/www/2. PHP/home/weichen/downloads/php-5.6. -/ZEND/ZEND_HASH.C (419): Actual location (location is relayed) last leak repeated3Times[thu JulTen  -: .: About  .] Script:'/home/www/2.php'/home/weichen/downloads/php-5.6. -/ZEND/ZEND_API.C (1366): Freeing0x7f1900d448c8( +bytes), script=/home/www/2. Phplast Leak repeated3Times[thu JulTen  -: .: About  .] Script:'/home/www/2.php'/HOME/WEICHEN/DOWNLOADS/PDONER/PDONER.C ( -): Freeing0x7f1900d45c58( +bytes), script=/home/www/2. Php[thu JulTen  -: .: About  .] Script:'/home/www/2.php'/home/weichen/downloads/php-5.6. -/ZEND/ZEND_HASH.C (392): Freeing0x7f1900d45d58( -bytes), script=/home/www/2. PHP/home/weichen/downloads/php-5.6. -/ZEND/ZEND_ALLOC.C (2583): Actual location (location is relayed) [Thu JulTen  -: .: About  .] Script:'/home/www/2.php'/HOME/WEICHEN/DOWNLOADS/PDONER/PDONER.C (121): Freeing0x7f1900d467c0( thebytes), script=/home/www/2. PHP/home/weichen/downloads/php-5.6. -/ZEND/ZEND_API.C (1011): Actual location (location is relayed)= = Total OneMemory leaks detected = = =

Visible is not destroyed zval *msg reason. You add Zval_ptr_dtor (&msg); will also report a section of error.

Note that the above usage is not used: ADD_PROPERTY_ZVAL_EX (Getthis (), Zend_strl (pdoner_errs_property_name_msg), MSG tsrmls_cc);

Three. The challenge of string return value.

Back to the string question at (2), this was done at first:

    Char " [";     char *ori = z_strval_p (return_value);     Char " ]";     char *dest = (char *) Emalloc (1024x768);        strcat (dest, SRC1);    strcat (dest, ori);    strcat (dest, SRC2);     0);

Obviously Dest is a long-term memory-consuming, but how can you destroy it after you return the value, I'm afraid it can't be done.

A concept is introduced here, when your function does not return a value, the variable returned by the function by default is Zval *return_value, which means that you do not have a problem with it.

In addition, we use the string connection function provided in the kernel concat_function (zval *result, Zval *op1, Zval *op2) instead of strcat for more efficient processing.

Concat_function in./zend/zend_operators.c:1422, sometimes it is not clear that usage is best seen in its implementation.

After using concat_function, there are some issues to be aware of, see the code:

    //The first method introduces a variableZval result; Concat_function (&result, &OP1, Return_value tsrmls_cc); Concat_function (&result, &result, &op2 tsrmls_cc); //1. Zval_ptr_dtor (&return_value) was wrong. //2. Forget Zval_dtor (Return_value) would cause memory leaks.Zval_dtor (Return_value); //copy result to Return_value; //if "zval result" is not zero-terminated, use Zval_zval () instead, like the 2. (PHP warning:string is not zero-terminated.)Zval_copy_value (Return_value, &result); //the second method, more conciseConcat_function (&OP1, &OP1, Return_value tsrmls_cc); Concat_function (&OP1, &OP1, &op2 tsrmls_cc);    Zval_dtor (Return_value); Zval_zval (Return_value,&OP1,0,1); Zval_dtor (&OP1); Zval_dtor (&OP2);

Above is the implementation of the Pdoner extension function Pd_implode_json: Https://github.com/farwish/pdoner

PHP does not turn on debug mode using the Valgrind tool: Http://tina.reeze.cn/book/?p=chapt06/06-07-memory-leaks

Link:http://www.cnblogs.com/farwish/p/5663993.html

@ Black eyed poet <www.farwish.com>

[PHP-SRC] The memory leak processing idea of PHP extension

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.