Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:117319 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 19427 invoked from network); 13 Mar 2022 06:11:37 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 13 Mar 2022 06:11:37 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 7D0191804E3 for ; Sat, 12 Mar 2022 23:35:47 -0800 (PST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=0.6 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: Error (Cannot connect to unix socket '/var/run/clamav/clamd.ctl': connect: Connection refused) X-Envelope-From: Received: from mail-il1-f182.google.com (mail-il1-f182.google.com [209.85.166.182]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Sat, 12 Mar 2022 23:35:47 -0800 (PST) Received: by mail-il1-f182.google.com with SMTP id n16so7702177ile.11 for ; Sat, 12 Mar 2022 23:35:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:from:date:message-id:subject:to; bh=EJ8QwdkugTDkW8bTTPSz9I1Xm8Oi+Lfiy5WiKsyRvv8=; b=pDduekqk771FWZOdbXrAL3cSzyJwGj9n7Ah6EeiHRXrNPL6RYesoEl/YXwIwu3BYBi 8VWUgN022gI9Su0Fdyn55wh6TOYZtnjMUggk9V5r96LG8aTASVN92T2KW034qYywDShI +8OouGm6wL5bkqfylcTjJG57g0KfbC9Hhe06chEHdEGNfO8N5RequwZoxIDd+Es2ofgW qQbEbGFzdS79n7IvunejSklyaULCk6y09XNCBj86KMQaHe+GP5lNBbPFePHTFM3qP9/k rQ+FzlWo5lGuEnj/cHZB8PkO733gs/jRdFUEk1VWlcTDnbNOOgMYOe/TVKVDT/KZPf/7 Xx8w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=EJ8QwdkugTDkW8bTTPSz9I1Xm8Oi+Lfiy5WiKsyRvv8=; b=xqCdzH359y+6blYAQoRd4Ayrb+sKyZLVvanNARn3lRC4fsPIaoa0bLm8cTsJy5TWTS yRecJuy1u4sUzvrguiCqLOqR4XhB5t0zdcuLGBoKwttPRvgxpTJjnBqrSbi5DbXQS/4U r9Jq596yQc4WrBurS3L9pS3/SrYaJ3dmTEFFW6sw/fC2X5ruP3TDjSfLRGwMypU/NFRY vtTWoegAbQAwz3ncPLqfj+nOI6gJUb2EPNjEGXyyTYfMjygdzb8idS0zD9nM32Vd6RVe 1qfhJAKxKaXlTSNQb0PvBEZoqCFqf877Qo9CtZ1kSk4RXrLCTDRGFAPSwt7htWn2nW4B za/Q== X-Gm-Message-State: AOAM53136YpWJu5mXBZeCg+axeTLtREPLYqumZaLatJwBRJRHQIGe0kR 3uIYRf10fKfTKQ1HBgWDmJyr4bRyBvKPckAcn9mD6a+PAuY= X-Google-Smtp-Source: ABdhPJxkzjmIHDEGfEK2KfK600dqhNTGE12kSB/EEnUNBbtQGace+U9Md1DG2AxPIchG6sf2I+NXz0+5Swn/mNA/ugU= X-Received: by 2002:a92:cdaa:0:b0:2c6:6751:dd18 with SMTP id g10-20020a92cdaa000000b002c66751dd18mr14479390ild.314.1647156946099; Sat, 12 Mar 2022 23:35:46 -0800 (PST) MIME-Version: 1.0 Date: Sat, 12 Mar 2022 23:35:36 -0800 Message-ID: To: internals@lists.php.net Content-Type: text/plain; charset="UTF-8" Subject: Tracking PHP Objects In PHP C-API From: sutabi@gmail.com (Joseph Montanez) Hello, What I am trying to do is hold onto another PHP object in an internal struct. I.E: position = new Vector3(); $camera3d->position->x++; The reason for this is that the Camera3D struct has additional data I need to track, so it cannot be an object of raw values. // Vector3, 3 components typedef struct Vector3 { float x; // Vector x component float y; // Vector y component float z; // Vector z component } Vector3; // Camera, defines position/orientation in 3d space typedef struct Camera3D { Vector3 position; // Camera position Vector3 target; // Camera target it looks-at Vector3 up; // Camera up vector (rotation over its axis) float fovy; // Camera field-of-view apperture in Y (degrees) in perspective, used as near plane width in orthographic int projection; // Camera projection: CAMERA_PERSPECTIVE or CAMERA_ORTHOGRAPHIC } Camera3D; The idea is that when the $camera3d->position is modified, and when I want to use the internal camera struct, I'll pull back the other PHP object's internal values. Right now it looks like this between Vector3 and Camera3D. typedef struct _php_raylib_vector3_object { Vector3 vector3; HashTable *prop_handler; zend_object std; } php_raylib_vector3_object; typedef struct _php_raylib_camera3d_object { Camera3D camera3d; HashTable *prop_handler; php_raylib_vector3_object *position; // $camera3d->position php_raylib_vector3_object *target; // $camera3d->target php_raylib_vector3_object *up; // $camera3d->up zend_object std; } php_raylib_camera3d_object; I've looked at the PHP LibXML extension, its using its own reference counting, while SQLITE only uses efree() when deconstructing. The general issue I am running into is that other values are being assigned, or worst memory being corrupted, this crashing. Its terms of the getting for the camera3d position object, I make sure to add a gc ref when providing the object. // If someone calls $camera3d->position static zend_object * php_raylib_camera3d_get_position(php_raylib_camera3d_object *obj) /* {{{ */ { GC_ADDREF(&obj->position->std); return &obj->position->std; } /* }}} */ Then for the setter I delref and addref. // If someone calls $camera3d->position = new Vector3d(1,2,3); static int php_raylib_camera3d_set_position(php_raylib_camera3d_object *obj, zval *newval) /* {{{ */ { int ret = SUCCESS; if (Z_TYPE_P(newval) == IS_NULL) { // Cannot set this to null... return ret; } php_raylib_vector3_object *phpPosition = Z_VECTOR3_OBJ_P(newval); GC_ADDREF(&phpPosition->std); GC_DELREF(&obj->position->std); obj->position = phpPosition; obj->camera3d.position = phpPosition->vector3; return ret; } /* }}} */ All is okay until when I need to update the internal Camera3D struct via an update function called before it used: // This takes the PHP objects of positon, target and up and assigns those values back to the raw camera struct. void php_raylib_camera3d_update_intern(php_raylib_camera3d_object *intern) { intern->camera3d.position = intern->position->vector3; // intern->position is php_raylib_vector3_object * intern->camera3d.target = intern->target->vector3; // intern->position is php_raylib_vector3_object * intern->camera3d.up = intern->up->vector3; // intern->position is php_raylib_vector3_object * } This works for the first time, but after creates an access error. Am I missing something when tracking? When I need to provide a PHP object I assumed I need to GC_ADDREF and the same was when replaced GC_DELREF. But when it's initialized or accessed internally do I need to continue to use those macros? I looked like LibXML as it has a similar concept but it's manually tracked references. SQLite3 also does something similar but only calls efree() when the root object is freed. I have the code available for how it's constructed. https://github.com/joseph-montanez/raylib-php/blob/develop/camera3d.c Thanks, Joseph Montanez Thanks, Joseph Montanez