Browse Source

FIX(memory): Process end, but not output file, memory leak.

viest 7 năm trước cách đây
mục cha
commit
032122a957
5 tập tin đã thay đổi với 36 bổ sung66 xóa
  1. 4 5
      excel_writer.c
  2. 24 58
      kernel/excel.c
  3. 3 3
      kernel/format.c
  4. 3 0
      kernel/include.h
  5. 2 0
      php_excel_writer.h

+ 4 - 5
excel_writer.c

@@ -15,13 +15,10 @@
 #endif
 
 #include "php.h"
-#include "php_ini.h"
-#include "php_excel_writer.h"
 #include "ext/standard/info.h"
+#include "kernel/include.h"
 
-#include "kernel/excel.h"
-#include "kernel/format.h"
-#include "kernel/exception.h"
+excel_resource_t *excel_res;
 
 int le_excel_writer;
 
@@ -33,6 +30,8 @@ PHP_MINIT_FUNCTION(excel_writer)
 	VTIFUL_STARTUP_MODULE(excel);
 	VTIFUL_STARTUP_MODULE(format);
 
+	excel_res = emalloc(sizeof(excel_resource_t));
+
     le_excel_writer = zend_register_list_destructors_ex(_php_vtiful_excel_close, NULL, VTIFUL_RESOURCE_NAME, module_number);
 
 	return SUCCESS;

+ 24 - 58
kernel/excel.c

@@ -95,7 +95,6 @@ PHP_METHOD(vtiful_excel, fileName)
 {
     zval rv, file_path, handle, *config, *tmp_path;
     zend_string *file_name, *key, *full_path;
-    excel_resource_t *res;
 
     ZEND_PARSE_PARAMETERS_START(1, 1)
             Z_PARAM_STR(file_name)
@@ -122,11 +121,10 @@ PHP_METHOD(vtiful_excel, fileName)
     full_path = zend_string_init(tmp_dir, strlen(tmp_dir), 0);
     ZVAL_STR(&file_path, full_path);
 
-    res = emalloc(sizeof(excel_resource_t));
-    res->workbook  = workbook_new(tmp_dir);
-    res->worksheet = workbook_add_worksheet(res->workbook, NULL);
+    excel_res->workbook  = workbook_new(tmp_dir);
+    excel_res->worksheet = workbook_add_worksheet(excel_res->workbook, NULL);
 
-    ZVAL_RES(&handle, zend_register_resource(res, le_excel_writer));
+    ZVAL_RES(&handle, zend_register_resource(excel_res, le_excel_writer));
 
     zend_update_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_FIL), &file_path);
     zend_update_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), &handle);
@@ -140,9 +138,8 @@ PHP_METHOD(vtiful_excel, fileName)
  */
 PHP_METHOD(vtiful_excel, header)
 {
-    zval rv, res_handle, *attr_handle, *header, *header_value;
+    zval rv, res_handle, *header, *header_value;
     zend_long header_l_key;
-    excel_resource_t *res;
 
     ZEND_PARSE_PARAMETERS_START(1, 1)
             Z_PARAM_ARRAY(header)
@@ -150,15 +147,12 @@ PHP_METHOD(vtiful_excel, header)
 
     ZVAL_COPY(return_value, getThis());
 
-    attr_handle = zend_read_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), 0, &rv TSRMLS_DC);
-    res = zval_get_resource(attr_handle);
-
     ZEND_HASH_FOREACH_NUM_KEY_VAL(Z_ARRVAL_P(header), header_l_key, header_value) {
-         type_writer(header_value, 0, header_l_key, res, NULL);
+         type_writer(header_value, 0, header_l_key, excel_res, NULL);
          zval_ptr_dtor(header_value);
     } ZEND_HASH_FOREACH_END();
 
-    ZVAL_RES(&res_handle, zend_register_resource(res, le_excel_writer));
+    ZVAL_RES(&res_handle, zend_register_resource(excel_res, le_excel_writer));
     zend_update_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), &res_handle);
 }
 /* }}} */
@@ -167,9 +161,8 @@ PHP_METHOD(vtiful_excel, header)
  */
 PHP_METHOD(vtiful_excel, data)
 {
-    zval rv, *data, *attr_handle, res_handle, *data_r_value, *data_l_value;
+    zval rv, *data, res_handle, *data_r_value, *data_l_value;
     zend_long data_r_key, data_l_key;
-    excel_resource_t *res;
 
     ZEND_PARSE_PARAMETERS_START(1, 1)
             Z_PARAM_ARRAY(data)
@@ -177,19 +170,16 @@ PHP_METHOD(vtiful_excel, data)
 
     ZVAL_COPY(return_value, getThis());
 
-    attr_handle = zend_read_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), 0, &rv TSRMLS_DC);
-    res = zval_get_resource(attr_handle);
-
     ZEND_HASH_FOREACH_NUM_KEY_VAL(Z_ARRVAL_P(data), data_r_key, data_r_value) {
         if(Z_TYPE_P(data_r_value) == IS_ARRAY) {
             ZEND_HASH_FOREACH_NUM_KEY_VAL(Z_ARRVAL_P(data_r_value), data_l_key, data_l_value) {
-                type_writer(data_l_value, data_r_key+1, data_l_key, res, NULL);
+                type_writer(data_l_value, data_r_key+1, data_l_key, excel_res, NULL);
                 zval_ptr_dtor(data_l_value);
             } ZEND_HASH_FOREACH_END();
         }
     } ZEND_HASH_FOREACH_END();
 
-    ZVAL_RES(&res_handle, zend_register_resource(res, le_excel_writer));
+    ZVAL_RES(&res_handle, zend_register_resource(excel_res, le_excel_writer));
     zend_update_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), &res_handle);
 }
 /* }}} */
@@ -199,15 +189,13 @@ PHP_METHOD(vtiful_excel, data)
 PHP_METHOD(vtiful_excel, output)
 {
     zval rv, null_handle, *handle, *file_path;
-    excel_resource_t *res;
 
     handle    = zend_read_property(vtiful_excel_ce, getThis(), ZEND_STRL(V_EXCEL_HANDLE), 0, &rv TSRMLS_DC);
     file_path = zend_read_property(vtiful_excel_ce, getThis(), ZEND_STRL(V_EXCEL_FIL), 0, &rv TSRMLS_DC);
-    res = zval_get_resource(handle);
 
-    workbook_file(res, handle);
+    workbook_file(excel_res, handle);
 
-    efree(res);
+    efree(excel_res);
 
     ZVAL_NULL(&null_handle);
     zend_update_property(vtiful_excel_ce, getThis(), ZEND_STRL(V_EXCEL_HANDLE), &null_handle);
@@ -234,10 +222,9 @@ PHP_METHOD(vtiful_excel, getHandle)
 PHP_METHOD(vtiful_excel, insertText)
 {
     zval rv, res_handle;
-    zval *attr_handle, *data;
+    zval *data;
     zend_long row, column;
     zend_string *format = NULL;
-    excel_resource_t *res;
 
     ZEND_PARSE_PARAMETERS_START(3, 4)
             Z_PARAM_LONG(row)
@@ -249,12 +236,9 @@ PHP_METHOD(vtiful_excel, insertText)
 
     ZVAL_COPY(return_value, getThis());
 
-    attr_handle = zend_read_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), 0, &rv TSRMLS_DC);
-    res = zval_get_resource(attr_handle);
+    type_writer(data, row, column, excel_res, format);
 
-    type_writer(data, row, column, res, format);
-
-    ZVAL_RES(&res_handle, zend_register_resource(res, le_excel_writer));
+    ZVAL_RES(&res_handle, zend_register_resource(excel_res, le_excel_writer));
     zend_update_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), &res_handle);
 }
 /* }}} */
@@ -264,9 +248,8 @@ PHP_METHOD(vtiful_excel, insertText)
 PHP_METHOD(vtiful_excel, insertImage)
 {
     zval rv, res_handle;
-    zval *attr_handle, *image;
+    zval *image;
     zend_long row, column;
-    excel_resource_t *res;
 
     ZEND_PARSE_PARAMETERS_START(3, 3)
             Z_PARAM_LONG(row)
@@ -276,12 +259,9 @@ PHP_METHOD(vtiful_excel, insertImage)
 
     ZVAL_COPY(return_value, getThis());
 
-    attr_handle = zend_read_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), 0, &rv TSRMLS_DC);
-    res = zval_get_resource(attr_handle);
-
-    image_writer(image, row, column, res);
+    image_writer(image, row, column, excel_res);
 
-    ZVAL_RES(&res_handle, zend_register_resource(res, le_excel_writer));
+    ZVAL_RES(&res_handle, zend_register_resource(excel_res, le_excel_writer));
     zend_update_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), &res_handle);
 }
 /* }}} */
@@ -291,9 +271,8 @@ PHP_METHOD(vtiful_excel, insertImage)
 PHP_METHOD(vtiful_excel, insertFormula)
 {
     zval rv, res_handle;
-    zval *attr_handle, *formula;
+    zval *formula;
     zend_long row, column;
-    excel_resource_t *res;
 
     ZEND_PARSE_PARAMETERS_START(3, 3)
             Z_PARAM_LONG(row)
@@ -303,12 +282,9 @@ PHP_METHOD(vtiful_excel, insertFormula)
 
     ZVAL_COPY(return_value, getThis());
 
-    attr_handle = zend_read_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), 0, &rv TSRMLS_DC);
-    res = zval_get_resource(attr_handle);
+    formula_writer(formula, row, column, excel_res);
 
-    formula_writer(formula, row, column, res);
-
-    ZVAL_RES(&res_handle, zend_register_resource(res, le_excel_writer));
+    ZVAL_RES(&res_handle, zend_register_resource(excel_res, le_excel_writer));
     zend_update_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), &res_handle);
 }
 /* }}} */
@@ -318,9 +294,7 @@ PHP_METHOD(vtiful_excel, insertFormula)
 PHP_METHOD(vtiful_excel, autoFilter)
 {
     zval rv, res_handle;
-    zval *attr_handle;
     zend_string *range;
-    excel_resource_t *res;
 
     ZEND_PARSE_PARAMETERS_START(1, 1)
             Z_PARAM_STR(range)
@@ -328,12 +302,9 @@ PHP_METHOD(vtiful_excel, autoFilter)
 
     ZVAL_COPY(return_value, getThis());
 
-    attr_handle = zend_read_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), 0, &rv TSRMLS_DC);
-    res = zval_get_resource(attr_handle);
-
-    auto_filter(range, res);
+    auto_filter(range, excel_res);
 
-    ZVAL_RES(&res_handle, zend_register_resource(res, le_excel_writer));
+    ZVAL_RES(&res_handle, zend_register_resource(excel_res, le_excel_writer));
     zend_update_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), &res_handle);
 }
 /* }}} */
@@ -343,9 +314,7 @@ PHP_METHOD(vtiful_excel, autoFilter)
 PHP_METHOD(vtiful_excel, mergeCells)
 {
     zval rv, res_handle;
-    zval *attr_handle;
     zend_string *range, *data;
-    excel_resource_t *res;
 
     ZEND_PARSE_PARAMETERS_START(2, 2)
             Z_PARAM_STR(range)
@@ -354,12 +323,9 @@ PHP_METHOD(vtiful_excel, mergeCells)
 
     ZVAL_COPY(return_value, getThis());
 
-    attr_handle = zend_read_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), 0, &rv TSRMLS_DC);
-    res = zval_get_resource(attr_handle);
-
-    merge_cells(range, data, res);
+    merge_cells(range, data, excel_res);
 
-    ZVAL_RES(&res_handle, zend_register_resource(res, le_excel_writer));
+    ZVAL_RES(&res_handle, zend_register_resource(excel_res, le_excel_writer));
     zend_update_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), &res_handle);
 }
 /* }}} */

+ 3 - 3
kernel/format.c

@@ -98,7 +98,7 @@ PHP_METHOD(vtiful_format, underline)
 
 /** {{{ excel_methods
 */
-zend_function_entry formac_methods[] = {
+zend_function_entry format_methods[] = {
         PHP_ME(vtiful_format, bold,      format_style_arginfo, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
         PHP_ME(vtiful_format, italic,    format_style_arginfo, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
         PHP_ME(vtiful_format, underline, format_style_arginfo, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
@@ -111,9 +111,9 @@ zend_function_entry formac_methods[] = {
 VTIFUL_STARTUP_FUNCTION(format) {
     zend_class_entry ce;
 
-    INIT_NS_CLASS_ENTRY(ce, "Vtiful\\Kernel", "Format", formac_methods);
+    INIT_NS_CLASS_ENTRY(ce, "Vtiful\\Kernel", "Format", format_methods);
 
-    zend_declare_class_constant_long(&ce, "UNDERLINE_SINGLE", sizeof("UNDERLINE_SINGLE")-1, (zend_long)LXW_UNDERLINE_SINGLE);
+//    zend_declare_class_constant_long(&ce, "UNDERLINE_SINGLE", sizeof("UNDERLINE_SINGLE")-1, (zend_long)LXW_UNDERLINE_SINGLE);
 
     vtiful_format_ce = zend_register_internal_class(&ce);
 

+ 3 - 0
kernel/include.h

@@ -22,6 +22,9 @@ typedef struct {
     lxw_worksheet *worksheet;
 } excel_resource_t;
 
+extern excel_resource_t *excel_res;
+
+
 excel_resource_t * zval_get_resource(zval *handle);
 
 STATIC lxw_error _store_defined_name(lxw_workbook *self, const char *name, const char *app_name, const char *formula, int16_t index, uint8_t hidden);

+ 2 - 0
php_excel_writer.h

@@ -13,6 +13,8 @@
 #ifndef PHP_VTIFUL_H
 #define PHP_VTIFUL_H
 
+#include "kernel/include.h"
+
 extern zend_module_entry excel_writer_module_entry;
 #define phpext_excel_writer_ptr &excel_writer_module_entry