Skip to content

ffi enum type (when enum has no name) make memory leak #14286

Closed

Description

@avriltank

Description

The following code:

<?php
$ffi = FFI::cdef("
    enum{
  //enum  TestEnum{  //if enum has a name there is no memory leak
        TEST_ONE=0,
        TEST_TWO=1,
    };
");
echo $ffi->TEST_TWO; //echo 1 
//the result is right but memory leak

i found the c ffi code when enum has no name make memory leak

void zend_ffi_make_enum_type(zend_ffi_dcl *dcl) /* {{{ */
{
	zend_ffi_type *type = pemalloc(sizeof(zend_ffi_type), FFI_G(persistent));
	type->kind = ZEND_FFI_TYPE_ENUM;
	type->attr = FFI_G(default_type_attr) | (dcl->attr & ZEND_FFI_ENUM_ATTRS);
	type->enumeration.tag_name = NULL;
	if (type->attr & ZEND_FFI_ATTR_PACKED) {
		type->size = zend_ffi_type_uint8.size;
		type->align = zend_ffi_type_uint8.align;
		type->enumeration.kind = ZEND_FFI_TYPE_UINT8;
	} else {
		type->size = zend_ffi_type_uint32.size;
		type->align = zend_ffi_type_uint32.align;
		type->enumeration.kind = ZEND_FFI_TYPE_UINT32;
	}
	dcl->type = ZEND_FFI_TYPE_MAKE_OWNED(type);
	dcl->attr &= ~ZEND_FFI_ENUM_ATTRS;
}

ffi_bad

PHP Version

php 8.3

Operating System

windows,linux

Activity

  1. changed the title [-]ffi enum type make memory leak[/-] [+]ffi enum type (when enm has no name) make memory leak[/+] on May 21, 2024
  2. changed the title [-]ffi enum type (when enm has no name) make memory leak[/-] [+]ffi enum type (when enum has no name) make memory leak[/+] on May 21, 2024
  3. ndossche commented on May 21, 2024

    @ndossche
    Member

    Problem seems to be that for top-level anonymous type definition we never store the declaration anywhere else nor the type anywhere else. The declaration keeps owning the type and it goes out of scope. For anonymous fields this gets handled by the add_anonymous_field code that removes the type from the declaration. I think we should do something similar.
    I tried this here, which seems to work, but it feels hacky: https://gist.github.com/nielsdos/62016be38fff00f97bfbee4a430cc009

  4. avriltank commented on May 22, 2024

    @avriltank
    Author

    Problem seems to be that for top-level anonymous type definition we never store the declaration anywhere else nor the type anywhere else. The declaration keeps owning the type and it goes out of scope. For anonymous fields this gets handled by the add_anonymous_field code that removes the type from the declaration. I think we should do something similar. I tried this here, which seems to work, but it feels hacky: https://gist.github.com/nielsdos/62016be38fff00f97bfbee4a430cc009

    thanks,patch this patch(https://gist.github.com/nielsdos/62016be38fff00f97bfbee4a430cc009) correct this Problem

  5. added a commit that references this issue on Jul 5, 2024
  6. added 2 commits that reference this issue on Jul 22, 2024
    c0de721
    b2963e9
  7. added a commit that references this issue on Jul 22, 2024
    77f870e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions