Skip to content

Commit ebe7dcf

Browse files
committed
fix a memleak
we were always copying images! argh speed is much better now, and memory use is stable
1 parent f3b86dd commit ebe7dcf

File tree

3 files changed

+58
-7
lines changed

3 files changed

+58
-7
lines changed

Diff for: ChangeLog

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
vips extension changelog
22

3+
Version 0.1.3 (2016-11-04)
4+
--------------------------
5+
* Fix memleak
6+
37
Version 0.1.2 (2016-10-24)
48
--------------------------
59
* Always dereference REFERENCE zvalues

Diff for: examples/leaktest.php

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/usr/bin/env php
2+
<?php
3+
4+
for($i = 0; $i < 2000; $i++) {
5+
echo "Loop $i ...\r";
6+
7+
$x = vips_image_new_from_file($argv[1], ["access" => "sequential"])["out"];
8+
9+
$width = vips_image_get($x, "width")["out"];
10+
$height = vips_image_get($x, "height")["out"];
11+
12+
$x = vips_call("crop", $x, 100, 100, $width - 200, $height - 200)["out"];
13+
14+
vips_image_write_to_file($x, $argv[2]);
15+
16+
$x = NULL;
17+
}
18+
19+
/*
20+
* Local variables:
21+
* tab-width: 4
22+
* c-basic-offset: 4
23+
* End:
24+
* vim600: expandtab sw=4 ts=4 fdm=marker
25+
* vim<600: expandtab sw=4 ts=4
26+
*/
27+

Diff for: vips.c

+27-7
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,8 @@ vips_php_zval_to_gval(VipsImage *match_image, zval *zvalue, GValue *gvalue)
566566
}
567567

568568
static int
569-
vips_php_set_value(VipsPhpCall *call, GParamSpec *pspec, zval *zvalue)
569+
vips_php_set_value(VipsPhpCall *call,
570+
GParamSpec *pspec, VipsArgumentFlags flags, zval *zvalue)
570571
{
571572
const char *name = g_param_spec_get_name(pspec);
572573
GType pspec_type = G_PARAM_SPEC_VALUE_TYPE(pspec);
@@ -581,7 +582,8 @@ vips_php_set_value(VipsPhpCall *call, GParamSpec *pspec, zval *zvalue)
581582
/* If we are setting a MODIFY VipsArgument with an image, we need to take a
582583
* copy.
583584
*/
584-
if (g_type_is_a(pspec_type, VIPS_TYPE_IMAGE)) {
585+
if (g_type_is_a(pspec_type, VIPS_TYPE_IMAGE) &&
586+
(flags & VIPS_ARGUMENT_MODIFY)) {
585587
VipsImage *image;
586588
VipsImage *memory;
587589

@@ -647,7 +649,7 @@ vips_php_set_required_input(VipsObject *object,
647649
}
648650

649651
if (arg &&
650-
vips_php_set_value(call, pspec, arg)) {
652+
vips_php_set_value(call, pspec, argument_class->flags, arg)) {
651653
return call;
652654
}
653655
}
@@ -686,7 +688,7 @@ vips_php_set_optional_input(VipsPhpCall *call, zval *options)
686688
if (!(argument_class->flags & VIPS_ARGUMENT_REQUIRED) &&
687689
(argument_class->flags & VIPS_ARGUMENT_INPUT) &&
688690
!(argument_class->flags & VIPS_ARGUMENT_DEPRECATED) &&
689-
vips_php_set_value(call, pspec, value)) {
691+
vips_php_set_value(call, pspec, argument_class->flags, value)) {
690692
return -1;
691693
}
692694
} ZEND_HASH_FOREACH_END();
@@ -891,6 +893,12 @@ vips_php_get_optional_output(VipsPhpCall *call, zval *options,
891893
continue;
892894
}
893895

896+
/* value should always be TRUE.
897+
*/
898+
if (Z_TYPE_P(value) != IS_TRUE) {
899+
continue;
900+
}
901+
894902
name = ZSTR_VAL(key);
895903
if (vips_object_get_argument(VIPS_OBJECT(call->operation), name,
896904
&pspec, &argument_class, &argument_instance)) {
@@ -1046,7 +1054,6 @@ PHP_FUNCTION(vips_call)
10461054
char *operation_name;
10471055
size_t operation_name_len;
10481056
zval *instance;
1049-
int i;
10501057

10511058
VIPS_DEBUG_MSG("vips_call:\n");
10521059

@@ -1182,7 +1189,7 @@ PHP_FUNCTION(vips_image_new_from_array)
11821189
int width;
11831190
int height;
11841191
VipsImage *mat;
1185-
int x, y;
1192+
int x;
11861193
zval *row;
11871194

11881195
VIPS_DEBUG_MSG("vips_image_new_from_array:\n");
@@ -1237,6 +1244,8 @@ PHP_FUNCTION(vips_image_write_to_file)
12371244
zval *options = NULL;
12381245
VipsImage *image;
12391246

1247+
VIPS_DEBUG_MSG("vips_image_write_to_file:\n");
1248+
12401249
if (zend_parse_parameters(ZEND_NUM_ARGS(), "rp|a",
12411250
&IM, &filename, &filename_len, &options) == FAILURE) {
12421251
RETURN_LONG(-1);
@@ -1247,6 +1256,8 @@ PHP_FUNCTION(vips_image_write_to_file)
12471256
RETURN_LONG(-1);
12481257
}
12491258

1259+
VIPS_DEBUG_MSG("\t%p -> %s\n", image, filename);
1260+
12501261
if (vips_image_write_to_file(image, filename, NULL)) {
12511262
RETURN_LONG(-1);
12521263
}
@@ -1477,7 +1488,6 @@ PHP_FUNCTION(vips_image_remove)
14771488
char *field_name;
14781489
size_t field_name_len;
14791490
VipsImage *image;
1480-
GType type;
14811491

14821492
if (zend_parse_parameters(ZEND_NUM_ARGS(), "rs",
14831493
&im, &field_name, &field_name_len) == FAILURE) {
@@ -1606,6 +1616,12 @@ PHP_MINIT_FUNCTION(vips)
16061616
le_gobject = zend_register_list_destructors_ex(php_free_gobject,
16071617
NULL, "GObject", module_number);
16081618

1619+
#ifdef VIPS_DEBUG
1620+
printf( "php-vips-ext init\n" );
1621+
printf( "enabling vips leak testing ...\n" );
1622+
vips_leak_set( TRUE );
1623+
#endif /*VIPS_DEBUG*/
1624+
16091625
return SUCCESS;
16101626
}
16111627
/* }}} */
@@ -1618,6 +1634,10 @@ PHP_MSHUTDOWN_FUNCTION(vips)
16181634
UNREGISTER_INI_ENTRIES();
16191635
*/
16201636

1637+
#ifdef VIPS_DEBUG
1638+
printf( "php-vips-ext shutdown\n" );
1639+
#endif /*VIPS_DEBUG*/
1640+
16211641
vips_shutdown();
16221642

16231643
return SUCCESS;

0 commit comments

Comments
 (0)