Re: [Vala] C Integration, it's cool
- From: Serge Hulne <serge hulne gmail com>
- To: vala-list <vala-list gnome org>
- Subject: Re: [Vala] C Integration, it's cool
- Date: Sun, 18 Dec 2011 06:06:21 +0100
The following example seems to indicate that it is safe to call an external
function written in C, from a Vala source code, as long as the calling Vala
code knows how to free the memory associated with type of the return value
yielded by the C function.
I am not sure this is true irrespectively of the type of the return value
of the C function.
I guess, this is only true if the type of the return value has a
corresponding type in Vala or if the memory allocated for said value is
freed manually in Vala (which is not vala-like and which is error prone).
Serge.
========================================
#Makefile
all : hello
hello : _hello.c hello.vala
valac -o hello --save-temps _hello.c hello.vala
========================================
=========================================================
// hello.vala
/////////////////
extern char * hello();
void main(string[] arg) {
//char* s = hello(); // wrong solution : memory leak (s
is not freed)
string s = (string)hello(); // correct solution : no memory leak
(s is freed)
print("s = %s\n", s);
}
==========================================================
=============================================================
// hello.c
////////////
#include <stdlib.h>
#include <string.h>
char * hello(){
char * a = malloc(sizeof(char)*strlen("hello")+1);
strcpy(a, "hello");
return a;
}
==============================================================
serge2 serge2-Macmini:~/development/workspace/vala_test_1/vapi_test$
valgrind ./hello
==30004== Memcheck, a memory error detector
==30004== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.
==30004== Using Valgrind-3.6.1-Debian and LibVEX; rerun with -h for
copyright info
==30004== Command: ./hello
==30004==
s = hello
==30004==
==30004== HEAP SUMMARY:
==30004== in use at exit: 45,684 bytes in 361 blocks
==30004== total heap usage: 426 allocs, 65 frees, 88,577 bytes allocated
==30004==
==30004== LEAK SUMMARY:
==30004== definitely lost: 0 bytes in 0 blocks
==30004== indirectly lost: 0 bytes in 0 blocks
==30004== possibly lost: 3,620 bytes in 66 blocks
==30004== still reachable: 37,552 bytes in 248 blocks
==30004== suppressed: 4,512 bytes in 47 blocks
==30004== Rerun with --leak-check=full to see details of leaked memory
==30004==
==30004== For counts of detected and suppressed errors, rerun with: -v
==30004== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 4 from 4)
============================================
for completeness: The temporary C code generated by Vala :
============================================
/* hello.c generated by valac 0.12.0, the Vala compiler
* generated from hello.vala, do not modify */
#include <glib.h>
#include <glib-object.h>
#define _g_free0(var) (var = (g_free (var), NULL))
gchar* hello (void);
void _vala_main (gchar** arg, int arg_length1);
void _vala_main (gchar** arg, int arg_length1) {
gchar* _tmp0_ = NULL;
gchar* s;
_tmp0_ = hello ();
s = (gchar*) _tmp0_;
g_print ("s = %s\n", s);
_g_free0 (s);
}
int main (int argc, char ** argv) {
g_type_init ();
_vala_main (argv, argc);
return 0;
}
==============================================
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]