I have a viewController showing some content. In header file, I defined an instance variable named _clientRequest, which is a ClassA object. ClassA deals with downloading Json from server.
For different users, there are 1 or 2 places using ClassA in the controller.
Currently my codes are like this,
// 1st request. every user will do this.
_clientRequest = [ClassA alloc] initWithTarget......];
[_clientRequest download];
...
// 2nd request. some user will do this.
_clientRequest = [ClassA alloc] initWithTarget......];
[_clientRequest upload];
you may notice that _clientRequest “alloc” and “initWithTarget” twice. In the future, server request may be much more in this controller. So I don’t want to declare 1 variable for 1 request. Is anything wrong in above codes? if a variable is alloc and initialized, how about re-alloc and re-initialize it? I run the app and no crash happens.
I am a newbie in obj-c. And English is not my native language. Hope you can understand.
Thanks in advance!
This will leak memory because you are allocing ClassA to _clientRequest, then allocing another instance of it without calling release on the first one. When you allocate data in this way, you must release it yourself.
You should call
[_clientRequest release];before your second call to[ClassA alloc]...