The project is to do a web-based game, talk about the system structure: UI management and data are separated, (created by the controller UI management classes and data classes) through the controller as an associate. There are underlying thread is responsible for sending and receiving messages, and unpacking forwarded to the controller after receiving the message. The controller first processes the data, and then call the UI management module will show the processed data in the interface. When there is UI operation, the call controller, and then sent to the server by the post-processing data controller. Here are the memory problems encountered:
A problem can not be released as a result of circular references:.
Here there is a problem with circular references, ie the controller holds the UI management class object reference, because the UI controller operations require a callback, then UI management classes they need to hold a reference to the controller. When you create a controller method requires the release of the controller, because the UI manager still holds a reference to the controller, the controller object can not be released.
So how to solve this problem too: you can try in the presence of circular references when using weak reference, which references an object does not increase the counter, of course, can not be released; method can create a stop to hold the controller in charge of the initiative to clean up the internal references and before the release call.
In fact, the official library inside the circular reference exists a lot of problems, the most typical is the Delegate, Delegate almost all are using weak references, which point to be very careful.
2.Delegate problems caused by weak references:
See the following code:
TestDelegate * delegate = [[TestDelegate alloc] init];
UIAlertView * alertV = [[UIAlertView alloc] initWithTitle: @ "title"
message: @ "message"
delegate: delegate
cancelButtonTitle: @ "cancel"
otherButtonTitles: @ "ok", nil];
NSLog (@ "retainCount:% d", [delegate retainCount]);
[AlertV show];
[AlertV release];
[Delegate release];
If you are a beginner, retainCount you will be surprised to find that the output is equal to 1, click the button when the program is being given a course without hesitation. This problem seems to be no simple solution. Programmers can only rely on themselves to hold the object, and then released at the right time.
Like general Deleage and Target will not hold object references, but NSTimer will hold. This can only be used by the time to try it. Even summed up nothing significant.
Do not know the foundation did not learn, when I read did not see there is talk of these issues. I do not know you have not encountered similar problems, we look forward to complement and guidance.
Original Address: http://blog.csdn.net/waynell/article/details/6672428 < / a>
Reply:
Yes, very good
Reply:
I think there might be a delegate object is either global, or is local, global, then of course do not need to let the current object when delegate holds, such appdelegate, but local objects need to delegate usually assigned to himself the , like you wrote the above code if delegae: self certainly no problem, no need to create a new object, and then delegated to other objects. Apple did not know how to consider when designing
Reply:
Delegate to the current object is generally the case should be no problem, but there are cases of the current object is released. For example, the server receives incoming messages out of the game module, perform the clean-up operations within the module. However, the dialog box is displayed on the screen as well, then the dialog box and then click the button will be a problem.
Reply:
It seems csdn in ios heroes really very little to do, put a message nobody back home is still a day of it!
Reply:
You are not in the above alloc your TestDelegate objects?
retain count of course be a cough. Is a no problem, ah, so you can release it under the
And run the above code is no problem, ah, click also possible.
Which way is my understanding wrong?
Reply:
You try to know.
UIAlertView's delegate pointed TestDelegate object, but did not hold him TestDelegate count still 1 , after a call to release the count is 0. But still display the dialog box, then click on the button on the dialog box will be released TestDelegate call results in an error.
Reply:
The latest sdk already do not need to retain, release the compiler automatically handled for you
Reply:
What version? How I think it is unlikely, java or do complex calculations done automatically by the virtual machine was released, only heard mac developers do when there is automatically released, but have not heard the compiler can deal with this problem. Could Apple be able to so cattle?
Reply:
sdk5 automatic memory management is really like a landlord without you I am a novice as written estimate is [delegate release];'s fault
Reply:
Looking for a long time, did not find the content of automatic memory management. Heroes to recommend the point?
Reply:
IOS 5 ARC
You can go to https://developer.apple.com/icloud/documentation/ watching What's New in iOS
Reply:
Also, I wonder why, when clicking the button was being given, why did I write error Demo, Demo you have a problem?
Reply:
To create a new commission, there is no other place for its references. After setting UIAlertView delegate its release, and then click there will be problems.
Reply:
Under normal circumstances, all of the delegate (delegate), when you set up.
Not proceed [delegate retain], because the statement is assign types.
TestDelegate * delegate = [[TestDelegate alloc] init];
UIAlertView * alertV = [[UIAlertView alloc] initWithTitle: @ "title"
message: @ "message"
delegate: delegate
cancelButtonTitle: @ "cancel"
otherButtonTitles: @ "ok", nil];
NSLog (@ "retainCount:% d", [delegate retainCount]);
[AlertV show];
[AlertV release];
[Delegate release];
So this is the wrong way
.You have not used the delegate object has been freed up.
But do not know delegate Alert was released, calling it generated, causing the program to crash.
No comments:
Post a Comment