I've been working on a PHP extension, binding RayLib. One issue I am
running into is that objects are being freed, which is triggering things I
don't want trigger until completely dereferenced.
So I have the following lines of PHP code:
$font = Font::fromCustom(DIR . "/resources/KAISG.ttf", 96, 0, 0);
$font->texture->genMipmaps();
Font is a struct from RayLib that I am trying to mirror into a PHP class
with properties. Here is the RayLib C Struct:
typedef struct Font {
int baseSize; // Base size (default chars height)
int charsCount; // Number of characters
Texture2D texture; // Characters texture atlas
Rectangle *recs; // Characters rectangles in texture
CharInfo *chars; // Characters info data
} Font;
The issue is every time I call $font->texture and the result $texture
object is not store, then its deference and the Texture object free
function is called, which unloads the texture, even though the Font struct
is still needing it. This is my fault as every time $font->texture is
called, I create a brand new texture object:
static zval * php_raylib_font_texture(php_raylib_font_object obj) / {{{ */
{
zval *texture = malloc(sizeof(zval));
object_init_ex(texture, php_raylib_texture_ce);
php_raylib_texture_object *result = Z_TEXTURE_OBJ_P(texture);
result->texture = obj->font.texture;
return texture;
}
/* }}} */
So after this line:
$font->texture->genMipmaps();
A Texture object is created, and right dereferenced thus calling my free
logic
void php_raylib_texture_free_storage(zend_object *object TSRMLS_DC)
{
php_raylib_texture_object *intern =
php_raylib_texture_fetch_object(object);
zend_object_std_dtor(&intern->std);
UnloadTexture(intern->texture);
}
So I assume I need to start storing the texture object, on my Font object.
Currently, it's defined like this:
typedef struct _php_raylib_font_object {
Font font;
HashTable *prop_handler;
zend_object std;
} php_raylib_font_object;
Am I to add another zend_object, zval or the struct def for Texture? I.E
typedef struct _php_raylib_font_object {
Font font;
HashTable *prop_handler;
zend_object texture; // Do I use zend_object?
zval texture; // Do I use zval?
php_raylib_texture_object texture; //Do I use the struct defined for
texture?
zend_object std;
} php_raylib_font_object;
For reference here is the Texture header:
https://github.com/joseph-montanez/raylib-php/blob/master/raylib-texture.h
Thanks,
Joseph Montanez