Professional Web Applications Themes

Garbage in button title - Mac Programming

Hi, A little more experienced than a few hours ago :-). I'm trying to programmatically set the title of a button, using the following: [inRef setTitle:[NSString stringWithCString:"Hello"]]; However, this end up with the button displaying garbage. I noticed the same problem when using SetControlTitleWithCFString in Carbon, so I switched back to SetControlTitle, which is ok, but I cannot use this trick in Cocoa. Is this a known problem, or is there omething wrong in the code above ? Thanks, Eric...

  1. #1

    Default Garbage in button title

    Hi,

    A little more experienced than a few hours ago :-).

    I'm trying to programmatically set the title of a button, using the
    following:

    [inRef setTitle:[NSString stringWithCString:"Hello"]];

    However, this end up with the button displaying garbage.

    I noticed the same problem when using SetControlTitleWithCFString in Carbon,
    so I switched back to SetControlTitle, which is ok, but I cannot use this
    trick in Cocoa.

    Is this a known problem, or is there omething wrong in the code above ?

    Thanks,

    Eric

    Eric VERGNAUD Guest

  2. #2

    Default Re: Garbage in button title

    On Sat, 13 Sep 2003, Eric VERGNAUD wrote:
    > Hi,
    >
    > A little more experienced than a few hours ago :-).
    >
    > I'm trying to programmatically set the title of a button, using the
    > following:
    >
    > [inRef setTitle:[NSString stringWithCString:"Hello"]];
    >
    > However, this end up with the button displaying garbage.
    >
    > I noticed the same problem when using SetControlTitleWithCFString in Carbon,
    > so I switched back to SetControlTitle, which is ok, but I cannot use this
    > trick in Cocoa.
    >
    > Is this a known problem, or is there omething wrong in the code above ?
    Yes. "Hello" is already a NSString. A plain old C string constant would
    be written without the (and in carbon
    SetControlTitleWithCFString(theControl,CFSTR("My title")); will work)

    Fred

    Frederick Cheung Guest

  3. #3

    Default Re: Garbage in button title (solved)

    dans l'article
    [email]Pine.LNX.4.44.0309122307180.8789-100000kern.srcf.societies.cam.ac.uk[/email],
    Frederick Cheung à [email]fglc2srcf.DUH.ucam.org[/email] a écrit le 13/09/03 0:09:
    > On Sat, 13 Sep 2003, Eric VERGNAUD wrote:
    >
    >> Hi,
    >>
    >> A little more experienced than a few hours ago :-).
    >>
    >> I'm trying to programmatically set the title of a button, using the
    >> following:
    >>
    >> [inRef setTitle:[NSString stringWithCString:"Hello"]];
    >>
    >> However, this end up with the button displaying garbage.
    >>
    >> I noticed the same problem when using SetControlTitleWithCFString in Carbon,
    >> so I switched back to SetControlTitle, which is ok, but I cannot use this
    >> trick in Cocoa.
    >>
    >> Is this a known problem, or is there omething wrong in the code above ?
    >
    > Yes. "Hello" is already a NSString. A plain old C string constant would
    > be written without the (and in carbon
    > SetControlTitleWithCFString(theControl,CFSTR("My title")); will work)
    >
    > Fred
    >
    Thank you Fred,

    In fact, I've been going a little further and found that:

    [inRef setTitle:"Hello"];

    Works perfectly.

    However, what I was looking for in the first place, is to set the button
    title with a string passed as a parameter, like in:

    void CocoaSetControlCaption(NSButton* inRef,NSString* inString)
    {
    [inRef setTitle: inString];
    }

    That didn't work.

    The reason is that setTitle does NOT retain the string. So when the caller
    does, the NSString held by the NSButton is no longer valid.

    So finally, I changed my code to:

    [inRef setTitle: [NSString stringWithString:inString]];

    And that does it, because now the button owns the title.

    Thanks,

    Eric

    Eric VERGNAUD Guest

  4. #4

    Default Re: Garbage in button title (solved)

    In <BB8819D8.11FA9%eric.vergnaudwanadoo.fr> Eric VERGNAUD wrote:
    > However, what I was looking for in the first place, is to set the
    > button title with a string passed as a parameter, like in:
    >
    > void CocoaSetControlCaption(NSButton* inRef,NSString* inString)
    > {
    > [inRef setTitle: inString];
    >}
    >
    > That didn't work.
    >
    > The reason is that setTitle does NOT retain the string.
    Certainly it does. If it didn't, this next code would not work:
    > So finally, I changed my code to:
    > [inRef setTitle: [NSString stringWithString:inString]];
    The output of stringWithString is autoreleased, and you are not
    retaining it, so if setTitle didn't retain it, the title would vanish in
    a puff of smoke. This proves that setTitle *does* retain the string.

    What was wrong with your original code was exactly what you were told by
    Frederick Cheung - you were supplying an NSString where a C string was
    expected. m.

    PS stringWithCString is deprecated so don't use it. The reason is that
    it gives you no control over the encoding.

    --
    matt neuburg, phd = [email]matttidbits.com[/email], [url]http://www.tidbits.com/matt[/url]
    REALbasic: The Definitive Guide! 2nd edition!
    [url]http://www.amazon.com/exec/obidos/ASIN/0596001770/somethingsbymatt[/url]
    Subscribe to TidBITS. It's free and smart.
    matt neuburg Guest

  5. #5

    Default Re: Garbage in button title (solved)

    > However, what I was looking for in the first place, is to set the button
    > title with a string passed as a parameter, like in:
    >
    > void CocoaSetControlCaption(NSButton* inRef,NSString* inString)
    > {
    > [inRef setTitle: inString];
    > }
    >
    > That didn't work.
    >
    > The reason is that setTitle does NOT retain the string. So when the caller
    > does, the NSString held by the NSButton is no longer valid.
    No, I don't think so. I don't know what is going wrong in your code, but
    your last sentence can not be true. It would make not any sense if Cocoa
    would not retain the NSString in setTitle, I can not believe this.
    It must be some other reason why your code failed.
    > So finally, I changed my code to:
    >
    > [inRef setTitle: [NSString stringWithString:inString]];
    >
    > And that does it, because now the button owns the title.
    No, this could not be the solution, if really setTitle does not retain the
    string. You have the same situation as before. Because: with [NSString
    stringWithString:inString] the actual autorelease pool is owning the string
    (not the button) and so you should have the same problem as before. The
    string passed to setTitle will be released soon, and if setTitle would
    really not retain it (what I can not believe) then your button would have
    the same problem as before.

    Regards,
    Mani
    ---
    iVolume, LittleSecrets, SharingMenu:
    [url]http://www.mani.de/[/url]

    Manfred Lippert Guest

  6. #6

    Default Re: Garbage in button title (solved)

    dans l'article BB891758.19A58%manimani.de, Manfred Lippert à [email]manimani.de[/email] a
    écrit le 13/09/03 18:50:
    >> However, what I was looking for in the first place, is to set the button
    >> title with a string passed as a parameter, like in:
    >>
    >> void CocoaSetControlCaption(NSButton* inRef,NSString* inString)
    >> {
    >> [inRef setTitle: inString];
    >> }
    >>
    >> That didn't work.
    >>
    >> The reason is that setTitle does NOT retain the string. So when the caller
    >> does, the NSString held by the NSButton is no longer valid.
    >
    > No, I don't think so. I don't know what is going wrong in your code, but
    > your last sentence can not be true. It would make not any sense if Cocoa
    > would not retain the NSString in setTitle, I can not believe this.
    > It must be some other reason why your code failed.
    >
    >> So finally, I changed my code to:
    >>
    >> [inRef setTitle: [NSString stringWithString:inString]];
    >>
    >> And that does it, because now the button owns the title.
    >
    > No, this could not be the solution, if really setTitle does not retain the
    > string. You have the same situation as before. Because: with [NSString
    > stringWithString:inString] the actual autorelease pool is owning the string
    > (not the button) and so you should have the same problem as before. The
    > string passed to setTitle will be released soon, and if setTitle would
    > really not retain it (what I can not believe) then your button would have
    > the same problem as before.
    Well maybe it COULD not be, but it IS.

    This is probably related to some unknown inners of NSString. The NSString I
    was passing to the above routine was referring to some UniCode data held in
    a Mac handle being disposed immediatly after return:

    Void MySetCaption(NSButtonRef inRef,char* inCString)
    {
    MyOwnUnicodeString str(inCString);
    MyMacCFStringFactory cfstr(str); // locks the Unicode data in str and
    provides a temporary CFString

    CocoaSetControlCaption(inRef,cfstr.GetCFString());

    // the destructor of cfstr
    // releases the CFString
    // unlocks the data

    // the destructor of str
    // dispose the unicode data
    }

    I agree that setTitle retains the string. However, it accepts a CFString
    which does not own its data without making a copy. That was the reason of my
    trouble.

    Thanks anyway.


    Eric

    Eric VERGNAUD Guest

  7. #7

    Default Re: Garbage in button title (solved)

    On Sat, 13 Sep 2003, Eric VERGNAUD wrote:
    > Well maybe it COULD not be, but it IS.
    >
    > This is probably related to some unknown inners of NSString. The NSString I
    > was passing to the above routine was referring to some UniCode data held in
    > a Mac handle being disposed immediatly after return:
    You're creating some weird fake CFString instance that does not follow the
    rules of reference counting. Rules such as, an object should remain valid
    until its reference count reaches zero. One shouldn't be required to make
    a copy, a simple retain should be sufficient. Your fake instance broke
    those rules and so of course it produced incorrect results.
    > I agree that setTitle retains the string. However, it accepts a CFString
    > which does not own its data without making a copy. That was the reason of my
    > trouble.
    You can't do this and expect it to work. If a CFString doesn't own its
    data then it can't follow the semantics that the frameworks and everyone
    else expect them to follow, so you get broken code.

    --
    "From now on, we live in a world where man has walked on the moon.
    And it's not a miracle, we just decided to go." -- Jim Lovell

    Mike Ash - <http://www.mikeash.com/>, <mailto:mailmikeash.com>

    Michael J Ash Guest

  8. #8

    Default Re: Garbage in button title (solved)

    In article <Pine.OSF.3.96.1030913131008.14915A-100000alpha3.csd.uwm.edu>,
    Michael J Ash <mikeashcsd.uwm.edu> wrote:
    > You can't do this and expect it to work. If a CFString doesn't own its
    > data then it can't follow the semantics that the frameworks and everyone
    > else expect them to follow, so you get broken code.
    That's not true. Look at CFStringCreateWith*StringNoCopy.

    meeroh

    --
    If this message helped you, consider buying an item
    from my wish list: <http://web.meeroh.org/wishlist>

    Miro Jurisic Guest

  9. #9

    Default Re: Garbage in button title (solved)

    On Sat, 13 Sep 2003, Miro Jurisic wrote:
    > In article <Pine.OSF.3.96.1030913131008.14915A-100000alpha3.csd.uwm.edu>,
    > Michael J Ash <mikeashcsd.uwm.edu> wrote:
    >
    > > You can't do this and expect it to work. If a CFString doesn't own its
    > > data then it can't follow the semantics that the frameworks and everyone
    > > else expect them to follow, so you get broken code.
    >
    > That's not true. Look at CFStringCreateWith*StringNoCopy.
    Although strings created thusly don't work with DrawThemeTextBox and some
    of its friends.

    Fred

    Frederick Cheung Guest

  10. #10

    Default Re: Garbage in button title (solved)

    In article
    <Pine.LNX.4.44.0309132109020.20310-100000kern.srcf.societies.cam.ac.uk>,
    Frederick Cheung <fglc2srcf.DUH.ucam.org> wrote:
    > On Sat, 13 Sep 2003, Miro Jurisic wrote:
    >
    > > In article <Pine.OSF.3.96.1030913131008.14915A-100000alpha3.csd.uwm.edu>,
    > > Michael J Ash <mikeashcsd.uwm.edu> wrote:
    > >
    > > > You can't do this and expect it to work. If a CFString doesn't own its
    > > > data then it can't follow the semantics that the frameworks and everyone
    > > > else expect them to follow, so you get broken code.
    > >
    > > That's not true. Look at CFStringCreateWith*StringNoCopy.
    > Although strings created thusly don't work with DrawThemeTextBox and some
    > of its friends.
    Sure they do. You just can't let the buffer go away before the text box is done
    with it.

    meeroh

    --
    If this message helped you, consider buying an item
    from my wish list: <http://web.meeroh.org/wishlist>

    Miro Jurisic Guest

  11. #11

    Default Re: Garbage in button title (solved)

    On Sat, 13 Sep 2003, Miro Jurisic wrote:
    > In article
    > <Pine.LNX.4.44.0309132109020.20310-100000kern.srcf.societies.cam.ac.uk>,
    > Frederick Cheung <fglc2srcf.DUH.ucam.org> wrote:
    >
    > > On Sat, 13 Sep 2003, Miro Jurisic wrote:
    > >
    > > > In article <Pine.OSF.3.96.1030913131008.14915A-100000alpha3.csd.uwm.edu>,
    > > > Michael J Ash <mikeashcsd.uwm.edu> wrote:
    > > >
    > > > > You can't do this and expect it to work. If a CFString doesn't own its
    > > > > data then it can't follow the semantics that the frameworks and everyone
    > > > > else expect them to follow, so you get broken code.
    > > >
    > > > That's not true. Look at CFStringCreateWith*StringNoCopy.
    > > Although strings created thusly don't work with DrawThemeTextBox and some
    > > of its friends.
    >
    > Sure they do. You just can't let the buffer go away before the text box is done
    > with it.
    Hmm. It explicitly says not to do this in the headers.

    Fred

    Frederick Cheung Guest

  12. #12

    Default Re: Garbage in button title (solved)

    dans l'article [email]macdev-1ACB91.14354713092003senator-bedfellow.mit.edu[/email], Miro
    Jurisic à [email]macdevmeeroh.org[/email] a écrit le 13/09/03 20:35:
    > In article <Pine.OSF.3.96.1030913131008.14915A-100000alpha3.csd.uwm.edu>,
    > Michael J Ash <mikeashcsd.uwm.edu> wrote:
    >
    >> You can't do this and expect it to work. If a CFString doesn't own its
    >> data then it can't follow the semantics that the frameworks and everyone
    >> else expect them to follow, so you get broken code.
    >
    > That's not true. Look at CFStringCreateWith*StringNoCopy.
    >
    > meeroh
    Which, by the way, is what I was using in my example. I was expecting the OS
    to be AWARE of the fact that the NSString did not own its data, and would
    cleverly make a copy of it.

    Or more frankly, I had no clue, but now that the problem is solved, I think
    it's what it should do.

    Eric

    Eric VERGNAUD Guest

Similar Threads

  1. garbage problem
    By Add_89 in forum Coldfusion Server Administration
    Replies: 4
    Last Post: February 9th, 03:36 PM
  2. Garbage collector
    By Nicholas Sherlock in forum MySQL
    Replies: 1
    Last Post: February 11th, 09:12 PM
  3. GARBAGE Thanks to whoever posted this! IT REALLY IS GARBAGE!!!! $$$$$
    By Carroll P. MacDonald in forum ASP Database
    Replies: 1
    Last Post: January 28th, 04:15 PM
  4. Fastest way to get a the string between <title> </title>
    By Andreas Klemt in forum ASP.NET General
    Replies: 1
    Last Post: August 10th, 01:58 AM
  5. URL Garbage
    By Tibby in forum ASP.NET General
    Replies: 0
    Last Post: August 5th, 12:50 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139