• 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: Adding data to a TableView
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Adding data to a TableView


  • Subject: Re: Adding data to a TableView
  • From: Andy Lee <email@hidden>
  • Date: Fri, 21 Jun 2002 12:46:48 -0400

At 5:13 PM +0100 6/21/02, Jeremy Dronfield wrote:
I've got a feeling I'm missing something eye-poppingly obvious. Can anyone explain what's going wrong with the following code? I can't get it to work (either signal 6, signal 10 or signal 11, or just no response at all). I've included some of the 'commented out' alternative bits of code I've tried.

You need to bone up on how to use retain/release. A starting point would be <file:///Developer/Documentation/Cocoa/TasksAndConcepts/ProgrammingTopics/MemoryMgmt/index.html>.

I'll annotate the retain/release problems I've spotted below.

#import "MyController.h"

@implementation MyController

- (id)init
{
[super init];
bookmarks = [[NSMutableArray alloc] init];
/*[(NSMutableArray *)bookmarks init];*/
[bookmarks retain];

You don't need this retain. The +alloc method returns an object that's already retained. This wouldn't cause a crash, just a memory leak.

totalSegments = 4;
return self;
}

- (void)dealloc
{

You should add [bookmarks release] here, and probably releases for other instance variables. Again, this is a memory leak, not a crash causer.

[...]
// Getting text
- (void)textViewDidChangeSelection:(NSNotification *)aNotification
{
if ([someTextTextView selectedRange].length > 0) {
textToBookmark=[[someTextTextView string] substringWithRange:[someTextTextView
selectedRange]];

The NSString object created above is *autoreleased*. You can tell because you are using one of NSString's convenience methods (+substringWithRange:) for instantiation rather than alloc/init, and by convention such instantiators return autoreleased objects. This *will* cause a crash because textToBookmark points to an object whose memory is very shortly going to be released (i.e., when the autorelease pool next does its thing).

I assume you deliberately don't have setter methods. If you did have one for textToBookmark, it should look something like this to properly manage the memory used by textToBookmark (i.e., both prevent a memory leak and prevent a crash):

- (void)setTextToBookmark:(NSString *)aString
{
[aString retain];
[textToBookmark release];
textToBookmark = aString;
}

So either add the setter method and call it, or replace the "textToBookmark=..." line with the equivalent logic.

--Andy
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

References: 
 >Adding data to a TableView (From: Jeremy Dronfield <email@hidden>)

  • Prev by Date: Re: NSTree ??
  • Next by Date: Re: My newbie question..
  • Previous by thread: Adding data to a TableView
  • Next by thread: Re: Adding data to a TableView
  • Index(es):
    • Date
    • Thread