• 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: Jeff Galyan <email@hidden>
  • Date: Tue, 22 Jan 2002 14:42:36 -0700

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.


> "This is not a book to be tossed aside lightly. Rather, it
> should be hurled with great force." -Dorothy Parker
>
Love the sig :)

--Jeff


  • Follow-Ups:
    • Re: Initializing an Array
      • From: "John C. Randolph" <email@hidden>
References: 
 >Re: Initializing an Array (From: "John C. Randolph" <email@hidden>)

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