iDevGames Forums

Full Version: Memory leak via obj-c
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
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.
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
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.
(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…
Reference URL's