• 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: Passing strings to threads and memory leaks
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Passing strings to threads and memory leaks


  • Subject: Re: Passing strings to threads and memory leaks
  • From: Pontus Ilbring <email@hidden>
  • Date: Fri, 7 May 2004 12:31:59 +0200

On 2004-05-07, at 01.56, Chuck Rice wrote:

I have a chunk of code that sets a field to be sent to a Liquid Crystal Display and to a window. On thread sets the value using the following method. Another thread updates the displays. I am leaking memory because of the retain for theData, so I tried to use a temp field to swap the previous string and release it. But I am still leaking memory. Where am I going wrong. I just barely have my head around the memory allocation thing, so a remedial explanation would not be unwelcome. If I change the retain to autorelease, I crash when the method ends. Help! -Chuck-


This is an oversimplified explanation of the memory management rules that still can get you pretty far:

 If you create an object using alloc or copy then you must either release or autorelease it at the end of the method. You use autorelease if you want to return the object from the method and release otherwise.

 If you got the object from another method, and only intend to use it within the scope of the current method, you don't have to worry about retaining or releasing it.

 If you want to hold on to an object for longer than the duration of the current method you must retain it now and then release it when you're done with it.


Following those rules, the code would look like this:

/ /-----------------------------------------------------------------------
// setDisplayLine:(int)number to:(char *)data atx:(int)x aty:(int)y
/ /-----------------------------------------------------------------------

- (void)setDisplayLine:(int)ident to:(char *)data
{
// we create an object using alloc
NSString *theData = [[[NSString alloc] initWithCString:data]];
NSString *temp;

if (ident > MAXFIELDS) {ident = MAXFIELDS;}
if (ident < 1) {ident = 1;}

temp =displayField[ident].data;

displayField[ident].font = 3;
// we want to hold on to the string, so we retain it
displayField[ident].data = [theData retain];
displayField[ident].displayNeedsUpdate = TRUE;
iScreenNeedsUpdate = TRUE;

// we release the old string because we're done with it
[temp release];

// we release the created object at the end of the method
[theData release];
}

---

This code is a bit inefficient, but not enough to in any way impact performance. There is no real point in both retaining and releasing the same object in a method, but if you always code this way then it reduces the risk of you making mistakes.

If you want to understand more about memory management then you should also read the following articles:

http://www.stepwise.com/Articles/Technical/2001-03-11.01.html
http://www.stepwise.com/Articles/Technical/HoldMe.html


// Pontus Ilbring
_______________________________________________
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.


  • Follow-Ups:
    • Re: Passing strings to threads and memory leaks
      • From: Chuck Rice <email@hidden>
References: 
 >Passing strings to threads and memory leaks (From: Chuck Rice <email@hidden>)

  • Prev by Date: Re: [OFF] connect.apple.com Asia Pacific mirror broken?
  • Next by Date: Re: Tab to NSButtons as nextKeyView
  • Previous by thread: Passing strings to threads and memory leaks
  • Next by thread: Re: Passing strings to threads and memory leaks
  • Index(es):
    • Date
    • Thread