• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Initializing an Array
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Initializing an Array


  • Subject: Re: Initializing an Array
  • From: "John C. Randolph" <email@hidden>
  • Date: Tue, 22 Jan 2002 13:57:32 -0800

On Tuesday, January 22, 2002, at 01:42 PM, Jeff Galyan wrote:

On 1/22/02 2:35 PM, "John C. Randolph" <email@hidden> wrote:


On Tuesday, January 22, 2002, at 01:19 PM, Jeff Galyan wrote:
Well, here's the rub: when I malloc or calloc something and
then free it
after use (in Cocoa, and after ensuring it's non-NULL), I get a
segfault
when the ObjC runtime tries to free it again.

Well, of course. If you want NSObject's -dealloc method to free
some memory, you shouldn't be doing it, too.

It sounds like your problem is a result of mixing malloc() with
+alloc. If you're allocating storage for an Obj-C instance,
don't use malloc(), use +alloc. If you want an
arbitrarily-sized block of bytes, try using NSData and
NSMutableData.

I wish this were the case - that would make my life so much easier. What I'm
doing is inside each method, I calloc a char* the length of the NSString (by
calling -length to get the length), pass it to -getCString: and then pass
the char* to a function in a C++ library. When I'm done with the char*, and
before the method exits, if I free it (even after verifying it's not a
pointer to NULL), the app crashes. If I don't, then the runtime whines at me
about attempts to free a pointer not malloced, but it doesn't crash.

Okay, in the interest of making your life easier:

1) since you're going to be done with the c string before you return from the method where you're creating it, go ahead and use -cString instead of -getCString:.

- (const char *)cString

Returns a representation of the receiver as a C string in the default C string encoding. The returned C string will be automatically freed just as a returned object would be released; your code should copy the C string or use getCString: if it needs to store the C string outside of the autorelease context in which the C string is created.

and

2) if you need to find the length of an NSString in order to manually allocate space for it's c-string representation, use -cStringLength, not -length.

"Returns the length in char-sized units of the receiver's C string representation in the default C string encoding."


-jcr

John C. Randolph <email@hidden> (408) 974-8819
Sr. Software Engineer, Cocoa Evangelism
Apple Worldwide Developer Relations


  • Follow-Ups:
    • Re: Initializing an Array
      • From: Jeff Galyan <email@hidden>
References: 
 >Re: Initializing an Array (From: Jeff Galyan <email@hidden>)

  • Prev by Date: Re: Initializing an Array
  • Next by Date: Re: Question about movePath
  • Previous by thread: Re: Initializing an Array
  • Next by thread: Re: Initializing an Array
  • Index(es):
    • Date
    • Thread