Memory leak via obj-c

Member
Posts: 117
Joined: 2010.09
Post: #1
Hi all,

I'm no fan of obj-c; I avoid it like the plague. LOLLOL

Instruments seems to suggest I am getting a memory leak here:

NSData *readstream = [[NSData alloc] initWithBytes:mystream->buf length:mystream->size];

I *presume* the nsdata alloc is never released. How do I release it??
< Yeah I know it's simple if your an obj-c fan> Blink

Cheers!
Ok, I think I can answer this myself :-)

I added [readstream release];
I presume that's correct; leak seems to be gone.
The whole obj-c memory alloc/dealloc is the biggest mess I think I have ever seen. Does anyone actually think it's well implemented???; seems like a right mare!

Cheers
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #2
The basic rules of ObjC memory management:
you own a reference to an object you +alloc, -copy, or -retain.
you must -release or -autorelease each object you own a reference to when you're done with it.

corollary:
if you don't want someone else to -release an object, you need to -retain or -copy it, eg. when you assign it to an object's field.

As you can see from the rules, it's not a mess at all. It's vastly simpler than any other scheme I've met other than actual garbage collection.
Quote this message in a reply
Member
Posts: 117
Joined: 2010.09
Post: #3
Yo OSC,
I did get the jist of it when I first looked into OBJ-C. It just seems to be the number 1 thing that trips people up and causes memory leaks. The basic premise you suggest is of course correct; perhaps it's more the way it's implemented in obj-c that makes it feel clunky. Always feel like you are heading for a memory leak with it.
Cheers
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #4
In my experience, memory leaks in ObjC are far rarer, and when present, far easier to find and fix, than in manual memory management environments like C and C++. The rules are almost trivial, and quickly become completely habitual. Only reference cycles require thought, and they require thought in any manual memory management environment.
Quote this message in a reply
⌘-R in Chief
Posts: 1,256
Joined: 2002.05
Post: #5
(Apr 17, 2011 07:55 AM)markhula Wrote:  The whole obj-c memory alloc/dealloc is the biggest mess I think I have ever seen. Does anyone actually think it's well implemented???; seems like a right mare!


It's really simple. Not as simple as garbage collection, but it really isn't difficult or a nightmare at all. Any hatred you have for Obj-C I think must be purely based on biased unfamiliarity Rasp.

Keith's rules cover it well. That's really all it comes down to. The corollary is worded a bit funny though…
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Why Does OpenGLES Appear To Leak Memory? muleskinner 2 3,527 Oct 22, 2009 04:54 AM
Last Post: muleskinner
  PNG memory leak question JYoung 0 1,923 Aug 15, 2009 07:50 PM
Last Post: JYoung
  Memory Leak using stringWithFormat MikeD 6 6,122 Apr 13, 2009 08:30 AM
Last Post: MikeD