Question

I believe I was following the rules but still a problem exists

My class init includes a block like this:

HTTPChunkReceiveBlock chunkBlock =  ^(id connection, NSData *data) {
    NSLog(@"Hi there!!");
};

and I am passing this block into an HttpConn obj which my class holds:

operation_ = [[HttpClient sharedClient] performChunkedRequest:url 
                                                 chunkHandler:chunkBlock];

Now for the problem: my object is never deallocated!!

The problem seems to be caused because the HttpConn is keeping a pointer to the block, but I want to mention two points:

  1. The block is not referring to self!
  2. The HttpConn class is keeping a copy of the block, like this:

    chunkBlock_ = [chunkBlock copy];

Any explanation would be greatly appreciated!

EDIT

Extra info: I have verified that if I'm freeing operation_ then my object is deallocated fine:

reader.operation_ = nil;
reader = nil; //previous line allows 'dealloc' to be called

Now repeating the question: operation did not get a pointer of reader's self, it only holds a copy of the block which do not refer to self!

Was it helpful?

Solution

Ok, I will answer my own question so that others do not fall into the same problem. @DarkDust was actually correct. there was a tiny line which I was completely ignoring:

**retriesNumber++;**

It looks like an innocent sentence, but because retriesNumber is a member of the class, it is actually meaning

(INVISIBLE strong pointer to self)->retriesNumber

so the solution was to declare it as a property (versus a member ivar) so that we can use self to access it, and then write:

pSelf->retriesNumber++;

Thank you guys for your quick support, and I hope others will learn from it too!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top