Vala binding: how to pass args to free_function?

Hi guys,

Met a big stone on the way: how to pass args to free_function

[CCode (cname = "SQLFreeHandle")]
public Return free_handle(HandleType handle_type, void* handle);

[CCode (cname = "void", free_function = "SQLFreeHandle", has_type_id = false)]
[Compact]
public class Statement {
}

Anybody could give me a hand? Thanks in advanced.

As far as I know this is not possible. I would create your own free function without parameters in the vapi, that calls free_handle with the right handle.

[CCode (cname = "SQLFreeHandle")]
public Return free_handle(HandleType handle_type, void* handle);

public void free_stmt(void* handle)
{
    free_handle(HandleType.STMT, handle);
}

[CCode (cname = "void", free_function = "free_stmt", has_type_id = false)]
[Compact]
public class Statement {
}

I’ve tried this solution, then ld command failed: undefined reference to `free_stmt’

I would add a [CCode (cname = "mylib_free_stmt to the free_stmt function. Then you can use “mylib_free_stmt” in free_function = ...

Maybe mark with attribute CCode with the final function is the rightt way to solve this problem.

I’v tried your solution, compile failed.

What was the compilation error and can you show the source code you used now?

The ODBC vapi source: odbc.vapi ( simplified model)

[CCode (cheader_filename = "windows.h,sqlext.h")]
namespace Odbc {
    [CCode (cname = "ODBCVER")]
    public const int VERSION;

    [CCode (cname = "SQLHANDLE", has_type_id = false)]
    public struct Handle : long {}

    [CCode (cname = "SQLHWND", has_type_id = false)]
    public struct Hwnd : long {}

    [CCode (cname = "SQLPOINTER", has_type_id = false)]
    public struct Pointer : long {}

    // Global function -------------------------------------------------------
    [CCode (cname = "SQLAllocHandle")]
    public Return alloc_env(HandleType handle_type, void* input_handle, out Environment output_handle);

    [CCode (cname = "SQLAllocHandle")]
    public Return alloc_dbc(HandleType handle_type, void* input_handle, out Connection output_handle);

    [CCode (cname = "SQLAllocHandle")]
    public Return alloc_stmt(HandleType handle_type, void* input_handle, out Statement output_handle);

    [Version (since = "3.8")]
    [CCode (cname = "SQLCancelHandle")]
    public Return cancel_handle(HandleType handle_type, void* handle);

    [CCode (cname = "SQLFreeHandle")]
    public Return free_handle(HandleType handle_type, void* handle);
    
    [CCode (cname = "void", free_function = "SQLFreeEnv", has_type_id = false)]
    [Compact]
    public class Environment {
        public static Environment @new()
        {
            Environment env;
            var retval = alloc_env(HandleType.ENV, null, out env);
            GLib.assert(succeeded(retval));

            return env;
        }
    }
    
    [CCode (cname = "void", free_function = "SQLFreeConnect", has_type_id = false)]
    [Compact]
    public class Connection {
        public static Connection @new(Environment env)
        {
            Connection connection;
            var retval = alloc_dbc(HandleType.DBC, env, out connection);
            GLib.assert(succeeded(retval));

            return connection;
        }
    }
    
    public void free_stmt(void* handle)
    {
        free_handle(HandleType.STMT, handle);
    }
    
    [CCode (cname = "void", free_function = "odbc_free_stmt", has_type_id = false)]
    [Compact]
    public class Statement {
        public static Statement @new(Connection connection)
        {
            Statement statement;
            var retval = alloc_stmt(HandleType.STMT, connection, out statement);
            GLib.assert(succeeded(retval));

            return statement;
        }
    }
}

Test function source code: odbc_vapi.vala( simplified model)

public void odbc_connection_test_alloc_statement()
{
    Odbc.Environment env = Odbc.Environment.new();

    Odbc.Return retval = env.set_attribute(Odbc.Attribute.ODBC_VERSION, (void *)Odbc.OdbcVersion.ODBC3, Odbc.ValueType.INTEGER);
    GLib.assert(Odbc.succeeded(retval));

    Odbc.Connection connection = Odbc.Connection.new(env);
    retval = connection.connect("Northwind".data, null, null);
    GLib.assert(Odbc.succeeded(retval));

    Odbc.Statement stmt;
    retval = connection.alloc_stmt(out stmt);
    GLib.assert(Odbc.succeeded(retval));

    retval = connection.disconnect();
    GLib.assert(Odbc.succeeded(retval));
}

the output message:

"cc" @Illuminate/libilluminate.dll.rsp
C:/msys/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: Illuminate/libilluminate.dll.p/meson-generated_Supports_Providers_Odbc_provider_odbc.c.obj: in function `odbc_statement_new':
D:\Codelabs\GitHub\kangaroo\build/../vapi/odbc.vapi:738: undefined reference to `odbc_free_stmt'
C:/msys/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: Illuminate/libilluminate.dll.p/meson-generated_Supports_Providers_Odbc_provider_odbc.c.obj: in function `kangaroo_illuminate_supports_providers_odbc_provider_test':
D:\Codelabs\GitHub\kangaroo\build/../Illuminate/Supports/Providers/Odbc/provider_odbc.vala:11: undefined reference to `odbc_free_stmt'
collect2.exe: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.

Use free_stmt to generate the code body: free_stmt((void*)0);

[CCode (cname = "void", free_function = "odbc_free_stmt", has_type_id = false)]
[Compact]
public class Statement {
    public static Statement @new(Connection connection)
    {
        Statement statement;
        var retval = alloc_stmt(HandleType.STMT, connection, out statement);
        GLib.assert(succeeded(retval));
        free_stmt((void*)0);

        return statement;
    }
}

The output message:

odbc_vapi.c:1101:1: error: static declaration of 'odbc_free_stmt' follows non-static declaration
tests/kangaroo.exe.p/odbc_vapi.c:41:63: note: previous implicit declaration of 'odbc_free_stmt' with type 'void(void *)'
   41 | VALA_EXTERN void odbc_environment_test_datasources (void);
      |                                                               ^         
tests/kangaroo.exe.p/odbc_vapi.c:41:63: note: in definition of macro '_odbc_free_stmt0'
   41 | VALA_EXTERN void odbc_environment_test_datasources (void);
      |                                                               ^         
[5/12] Linking target Illuminate/libilluminate.dll
ninja: build stopped: subcommand failed.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.