Re: [Vala] Help: unowned string returns garbage characters



The word unowned doesnt probably reflects the meaning at all by itself. Maybe 'weak' is more accurate.

The thing is that an object you don't own can be corrupted at any time. So you don't own it, and anybody else 
can free it, trash it or modify it.

It has nothing to do if it comes from a library or what. Think like this in C:

char *unowned() {
    static char ret[128];
    strncpy (ret, getenv ("FOO"), sizeof (ret)-1);
    return ret;
}

char *owned() {
    return strdup (getenv ("FOO"));
}

Maybe my definition is not accurate at all but hope if helps to understand the problem :)

On 29/10/2011, at 22:53, bsquared <bwcode4u gmail com> wrote:


LoveBug356 <thijsvermeir gmail com>
writes:

Hi,

On Sat, 2011-10-29 at 09:44 -0700, bsquared wrote:
Hello, 

I am refactoring some of my test code into a class and have run
into a problem with unowned strings.

I have a class that has several methods that return strings. These
methods get their value form the response buffer of a Soup Message.

       public string send_misc_method (MiscMethod mm) {
              [...]
              debug ("send_misc_method: %s",
                    (string) message.response_body.flatten ().data); // OK
              return  (string) message.response_body.flatten ().data
       }

       public unowned string get_motd () {
              string s = send_misc_method (MiscMethod.MOTD);
              debug ("get_motd: %s", s);                       // OK
              return s.to_string (); // transfers ownership?
       }

This looks wrong. You are creating a new unowned string from a string
wich you will drop te ownership at the end of the function. So this
unowned string will always be wrong. Probably there is no reason here to
return a unowned string.

Gr,
Thijs

       main (string[] args) {
            [...]
            debug ("main - get_motd: %s", server.get_motd ()); // !OK
            // var s = server.get_motd ();
            // debug ("main - get_motd: %s", s); // also !OK             
       }

output:
** (process:28697): DEBUG: couchdb-server-api.vala:90: 
send_misc_method: {"couchdb":"Welcome","version":"1.2.0a-"}

** (process:28697): DEBUG: couchdb-server-api.vala:104: 
get_motd: {"couchdb":"Welcome","version":"1.2.0a-"}

** (process:28697): DEBUG: couchdb-server-api.vala:146: 
main - get_motd : \xc0\xa0\u0004uchdb":"Welcome","version":"1.2.0a-"}

So, what have I done wrong?

Thank you.



Thanks.  

I am confused by the ownership. I am referencing other vapi files
and guessed that the string should be unowned if it is returned from a
library.


_______________________________________________
vala-list mailing list
vala-list gnome org
http://mail.gnome.org/mailman/listinfo/vala-list




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