Create memory leaks challenge


#1

Hi,

I’m trying to create memory leaks as directed on page 385 (3rd ed. of the book). I commented out the CFMakeCollectable(colour); line so that the CG colour member wouldn’t be collected and I replaced the terms = NSAllocateCollectable(termCount * sizeof(CGFloat), 0); line with a straight call to malloc: terms = malloc(termCount * sizeof(CGFloat);

But when I check the memory performance in Instruments, the Colour and Polynomial counts still go down after deleting them through the UI! What gives? Have I missed something obvious? This is using XCode 3.2 set to a 10.6 | Debug | x86_64 configuration.

Thanks,
Oliver.

EDIT: oops - forgot to save the changes! So now the Colour count stays the same when deleting polynomials as expected. But the polynomial count still isn’t dropping with the above (saved!) changes.


#2

[quote=“OliverMooney”]Hi,

I’m trying to create memory leaks as directed on page 385 (3rd ed. of the book). I commented out the CFMakeCollectable(colour); line so that the CG colour member wouldn’t be collected and I replaced the terms = NSAllocateCollectable(termCount * sizeof(CGFloat), 0); line with a straight call to malloc: terms = malloc(termCount * sizeof(CGFloat);

But when I check the memory performance in Instruments, the Colour and Polynomial counts still go down after deleting them through the UI! What gives? Have I missed something obvious? This is using XCode 3.2 set to a 10.6 | Debug | x86_64 configuration.

Thanks,
Oliver.

EDIT: oops - forgot to save the changes! So now the Colour count stays the same when deleting polynomials as expected. But the polynomial count still isn’t dropping with the above (saved!) changes.[/quote]

Do you see some “finalizing” messages (remember the NSLog(@“finalizing”); we put in Polynomial’s - (void)finalize) in the Console (you can open it with Shift+Command+R) ? If not then the garbage collector just hasn’t kicked in yet; so just add/remove some more

Anyway, let me hijack your post and ask another question, in the book it reads:

So what’s the difference between nonscanned (0) and scanned memory (NSScannedOption) in this case?

EDIT: missing “)”


#3

i’ve been made the same than oliver. CGColor structs remains listed, polynomials still go away when removed from view. I don’t still understand how can I make a really useful leak for the sake of study.
Thanks all

EDIT: :smiley:
I’ve discovered something; doing the same as Oliver, if you launch with Performance tool/leaks, try to add and remove polynomials, as you remove polynomials you can see CGColor structs leaking! YEP! :sunglasses:
After removing 7 polynomials i saw 7 CGColor leaks, 48 bytes for every one.
But terms still collapse when polynomial removed… damn…

After a bit I reproduced another kind of leak:
if you return to make colors collectable and terms allocated by GC, (NSAllocateCollectable: etc), than declare CGColorRef as a __weak reference instead of a __strong reference, and running on performance tool/leaks let’s add some polynomials, after a while (not much), you will see all the polynomials become BLACK, I think this is the confirm that, with a __weak reference, garbage collection starts deallocating objects in a strange manner, and above all prematurely.

I hope my experience can be useful for someone.
Sorry for the english (i’m not english);