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




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.





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