Professional Web Applications Themes

newbie simple strings question - Mac Programming

I got tired of typing CFSTR so I thought I would just pass a char string and convert it to a CFString. The Mac OS X Programming book is very light on explanation of strings (need book recommendations). Why doesn't this work? int tryText(char *psTxt) { CFMutableStringRef sTxt; CFStringAppendCString( sTxt, psTxt, CFStringGetSystemEncoding() ); return 0; } Program received signal: "EXC_BAD_ACCESS". TIA!...

  1. #1

    Default newbie simple strings question

    I got tired of typing CFSTR so I thought I would just pass a char string
    and convert it to a CFString. The Mac OS X Programming book is very light
    on explanation of strings (need book recommendations).

    Why doesn't this work?

    int tryText(char *psTxt)
    {
    CFMutableStringRef sTxt;
    CFStringAppendCString( sTxt, psTxt, CFStringGetSystemEncoding() );
    return 0;
    }

    Program received signal: "EXC_BAD_ACCESS".

    TIA!
    Frank Guest

  2. #2

    Default Re: newbie simple strings question

    In article <no-3009031947230001eli-216-190-254-129.ev1.net>,
    [email]nospam.com[/email] (Frank) wrote:
    > int tryText(char *psTxt)
    > {
    > CFMutableStringRef sTxt;
    sTxt = CFStringCreateMutable(kCFAllocatorDefault, 0);
    > CFStringAppendCString( sTxt, psTxt, CFStringGetSystemEncoding() );
    > return 0;
    > }
    >
    > Program received signal: "EXC_BAD_ACCESS".
    You haven't actually created a mutable string yet, you only defined a
    space in memory for one. Add what I added above, and it should work fine
    (I think! I haven't used CF much). You can replace 0 with a max length
    for the string if you want.

    Doug

    --
    Doug Brown - La Grande, OR - [url]http://www.ircandy.com/doug/[/url]
    Idiot's Guide to Mac Cases - [url]http://www.ircandy.com/maccases/[/url]
    If you want to reply by email, remove "pleasenospam." and ".invalid"
    Doug Brown Guest

  3. #3

    Default Re: newbie simple strings question

    In article <ev1.net>,
    com (Frank) wrote:
     

    It's nothing to do with CFSTR vs. C strings. It's just that you never
    allocate any memory for sTxt. A CFMutableStringRef is just a reference
    to the string, not the string itself, so you need to give it something
    to refer to. That is, you need to call CFStringCreateMutable() before
    you attempt to use the string object.

    --
    Tom "Tom" Harrington
    Macaroni, Automated System Maintenance for Mac OS X.
    Version 1.4: Best cleanup yet, gets files other tools miss.
    See http://www.atomicbird.com/
    Tom Guest

  4. #4

    Default Re: newbie simple strings question

    In article <ev1.net>,
    com (Frank) wrote:
     

    You should note "Append" in "CFStringAppendCString"...you would need
    sTxt to be an object to which something could be appended. Even though
    for your usage it would be simply a null string (""), it wouldn't be a
    null pointer..

    Furthermore, EXC_BAD_ACCESS generally means you messed up a pointer.
    Either the pointer hasn't been allocated, so it points to an incorrect
    (gibberish) memory location, or the pointer is pointing at an address
    where the object has been deallocated.


    ==Example==

    NSString *myString = [NSString stringWithString:"hello, world"];

    // blah blah, then somewhere after the event loop returns:

    NSString *myOtherString = [[NSString stringWithString:myString] retain];


    ==Example==


    This will bomb out, and you'll get an error of EXC_BAD_ACCESS, because
    you failed to retain myString (which was autoreleased) and so now that
    pointer points to garbage when you try to pass it as an argument later
    on.
    Mark Guest

Similar Threads

  1. [XML::Simple-2.12] problems parsing non ASCII strings
    By Michel Rodriguez in forum PERL Modules
    Replies: 4
    Last Post: July 17th, 12:55 AM
  2. Simple Newbie question
    By jeremy in forum Coldfusion - Getting Started
    Replies: 17
    Last Post: April 9th, 06:46 PM
  3. Simple Question for a newbie
    By RES in forum Macromedia Flash
    Replies: 1
    Last Post: July 28th, 10:40 AM
  4. Simple Question: Converting lists to strings
    By lingo smith in forum Macromedia Director Basics
    Replies: 6
    Last Post: April 17th, 04:04 PM
  5. Newbie with a simple question.
    By Curtsong in forum Macromedia Flash Sitedesign
    Replies: 2
    Last Post: February 17th, 01:48 AM

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