Re: [Vala] Unnecessary g_strdup and compile error.



Hi,

please see a comment inline,

Hello,
(sorry, my English is not so good ;)

I often write the following codes.
----------------------------------------------------------------
void main () {
   var home = Environment.get_home_dir ();
   print ("%s\n", home);
}
----------------------------------------------------------------

Then, unnecessary g_strdup is generated. 
----------------------------------------------------------------
void _vala_main (void) {
   const gchar* _tmp0_ = NULL;
   gchar* _tmp1_;
   gchar* home;
   _tmp0_ = g_get_home_dir ();
   _tmp1_ = g_strdup (_tmp0_);
   home = _tmp1_;
   g_print ("%s\n", home);
   _g_free0 (home);
}
----------------------------------------------------------------

It is necessary to write a tedious following codes to avoid it. 
----------------------------------------------------------------
void main () {
   unowned string home = Environment.get_home_dir ();
   print ("%s\n", home);
}
----------------------------------------------------------------


If get_home_dir() returns unowned string, and you make assignment to
owned string reference, you get a copy of this string. Each owned
reference of a string will delete the string when it goes out of scope,
that's why this strdup is needed (note: most objects don't behave that
way). Please see that the value returned by g_get_home_dir(), _tmp0_,
is not freed in the C code.

best regards,

Is not unnecessary g_strdup generated without writing a tedious code?

After that, if the following codes are written, it becomes a compile error. (Vala 0.11.2)
----------------------------------------------------------------
void main () {
   const string home = Environment.get_home_dir ();
   print ("%s\n", home);
}
----------------------------------------------------------------

/home/kentaro/work/VALA/unnecessary_strdup.vala.c: In function '_vala_main':
/home/kentaro/work/VALA/unnecessary_strdup.vala.c:18: error: invalid initializer
error: cc exited with status 256
Compilation failed: 1 error(s), 0 warning(s)

Best regards,

-- 
Kentaro NAKAZAWA (from Japan)
_______________________________________________
vala-list mailing list
vala-list gnome org
http://mail.gnome.org/mailman/listinfo/vala-list


-- 
Mój klucz publiczny o identyfikatorze 1024D/E12C5A4C znajduje się na
serwerze hkp://keys.gnupg.net

My public key with signature 1024D/E12C5A4C is on the server
hkp://keys.gnupg.net

Attachment: signature.asc
Description: PGP signature



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]