![]() |
|
Memory leak via obj-c - Printable Version +- iDevGames Forums (http://www.idevgames.com/forums) +-- Forum: Development Zone (/forum-3.html) +--- Forum: iPhone, iPad & iPod Game Development (/forum-11.html) +--- Thread: Memory leak via obj-c (/thread-8848.html) |
Memory leak via obj-c - markhula - Apr 17, 2011 07:55 AM Hi all, I'm no fan of obj-c; I avoid it like the plague. ![]() ![]() 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> ![]() 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 RE: Memory leak via obj-c - OneSadCookie - Apr 17, 2011 08:04 AM 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. RE: Memory leak via obj-c - markhula - Apr 17, 2011 08:17 AM 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 RE: Memory leak via obj-c - OneSadCookie - Apr 17, 2011 09:32 AM 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. RE: Memory leak via obj-c - SethWillits - Apr 17, 2011 01:35 PM (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 .Keith's rules cover it well. That's really all it comes down to. The corollary is worded a bit funny though… |