Ask a Question related to Mac Programming, Design and Development.
-
Eric VERGNAUD #1
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
-
garbage problem
hi, i've a problem with garbage collection, my server: 2 CPU intel xeon 3 Ghz (dual core) 3 Gb of ram my jvm.config java.args=-server... -
Garbage collector
Hey all, I have a table, 'Objects', and a table 'Refs'. The table 'Refs' could have multiple rows which reference one object in 'Object'. What is... -
GARBAGE Thanks to whoever posted this! IT REALLY IS GARBAGE!!!! $$$$$
Dip Head... Post your stupid garbage Elsewhere!!! Freakin Garbage! Thank you for your time and have a great day, Carroll P. MacDonald -- -
Fastest way to get a the string between <title> </title>
Hello, I have in my strHtml a html page. so what is the fastest way (with XML Read?) to get the "myValue" between <title>myValue</title> ... -
URL Garbage
Thanks so very much. I know I've asked it, I just couldn't remember what the answer was. Tibby "Marina" <zlatkinam@nospam.hotmail.com> wrote in... -
Frederick Cheung #2
Re: Garbage in button title
On Sat, 13 Sep 2003, Eric VERGNAUD wrote:
Yes. @"Hello" is already a NSString. A plain old C string constant would> 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 ?
be written without the @ (and in carbon
SetControlTitleWithCFString(theControl,CFSTR("My title")); will work)
Fred
Frederick Cheung Guest
-
Eric VERGNAUD #3
Re: Garbage in button title (solved)
dans l'article
[email]Pine.LNX.4.44.0309122307180.8789-100000@kern.srcf.societies.cam.ac.uk[/email],
Frederick Cheung à [email]fglc2@srcf.DUH.ucam.org[/email] a écrit le 13/09/03 0:09*:
Thank you Fred,> 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
>
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
-
matt neuburg #4
Re: Garbage in button title (solved)
In <BB8819D8.11FA9%eric.vergnaud@wanadoo.fr> Eric VERGNAUD wrote:
Certainly it does. If it didn't, this next code would not work:> 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.
The output of stringWithString is autoreleased, and you are not> So finally, I changed my code to:
> [inRef setTitle: [NSString stringWithString:inString]];
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]matt@tidbits.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
-
Manfred Lippert #5
Re: Garbage in button title (solved)
> However, what I was looking for in the first place, is to set the button
No, I don't think so. I don't know what is going wrong in your code, but> 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.
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.
No, this could not be the solution, if really setTitle does not retain the> So finally, I changed my code to:
>
> [inRef setTitle: [NSString stringWithString:inString]];
>
> And that does it, because now the button owns the title.
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
-
Eric VERGNAUD #6
Re: Garbage in button title (solved)
dans l'article BB891758.19A58%mani@mani.de, Manfred Lippert à [email]mani@mani.de[/email] a
écrit le 13/09/03 18:50*:
Well maybe it COULD not be, but it IS.>>> 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.
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
-
Michael J Ash #7
Re: Garbage in button title (solved)
On Sat, 13 Sep 2003, Eric VERGNAUD wrote:
You're creating some weird fake CFString instance that does not follow the> 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:
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.
You can't do this and expect it to work. If a CFString doesn't own its> 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.
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:mail@mikeash.com>
Michael J Ash Guest
-
Miro Jurisic #8
Re: Garbage in button title (solved)
In article <Pine.OSF.3.96.1030913131008.14915A-100000@alpha3.csd.uwm.edu>,
Michael J Ash <mikeash@csd.uwm.edu> wrote:
That's not true. Look at CFStringCreateWith*StringNoCopy.> 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.
meeroh
--
If this message helped you, consider buying an item
from my wish list: <http://web.meeroh.org/wishlist>
Miro Jurisic Guest
-
Frederick Cheung #9
Re: Garbage in button title (solved)
On Sat, 13 Sep 2003, Miro Jurisic wrote:
Although strings created thusly don't work with DrawThemeTextBox and some> In article <Pine.OSF.3.96.1030913131008.14915A-100000@alpha3.csd.uwm.edu>,
> Michael J Ash <mikeash@csd.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.
of its friends.
Fred
Frederick Cheung Guest
-
Miro Jurisic #10
Re: Garbage in button title (solved)
In article
<Pine.LNX.4.44.0309132109020.20310-100000@kern.srcf.societies.cam.ac.uk>,
Frederick Cheung <fglc2@srcf.DUH.ucam.org> wrote:
Sure they do. You just can't let the buffer go away before the text box is done> On Sat, 13 Sep 2003, Miro Jurisic wrote:
>> Although strings created thusly don't work with DrawThemeTextBox and some> > In article <Pine.OSF.3.96.1030913131008.14915A-100000@alpha3.csd.uwm.edu>,
> > Michael J Ash <mikeash@csd.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.
> of its friends.
with it.
meeroh
--
If this message helped you, consider buying an item
from my wish list: <http://web.meeroh.org/wishlist>
Miro Jurisic Guest
-
Frederick Cheung #11
Re: Garbage in button title (solved)
On Sat, 13 Sep 2003, Miro Jurisic wrote:
Hmm. It explicitly says not to do this in the headers.> In article
> <Pine.LNX.4.44.0309132109020.20310-100000@kern.srcf.societies.cam.ac.uk>,
> Frederick Cheung <fglc2@srcf.DUH.ucam.org> wrote:
>>> > On Sat, 13 Sep 2003, Miro Jurisic wrote:
> >> > Although strings created thusly don't work with DrawThemeTextBox and some> > > In article <Pine.OSF.3.96.1030913131008.14915A-100000@alpha3.csd.uwm.edu>,
> > > Michael J Ash <mikeash@csd.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.
> > of its friends.
> Sure they do. You just can't let the buffer go away before the text box is done
> with it.
Fred
Frederick Cheung Guest
-
Eric VERGNAUD #12
Re: Garbage in button title (solved)
dans l'article [email]macdev-1ACB91.14354713092003@senator-bedfellow.mit.edu[/email], Miro
Jurisic à [email]macdev@meeroh.org[/email] a écrit le 13/09/03 20:35*:
Which, by the way, is what I was using in my example. I was expecting the OS> In article <Pine.OSF.3.96.1030913131008.14915A-100000@alpha3.csd.uwm.edu>,
> Michael J Ash <mikeash@csd.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
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



Reply With Quote

