NSCFString autoreleased with no pool in place - ju

2019-04-10 18:13发布

问题:

I suddenly get hundreds of these lines in my console log running a process which uses a progress controller I implemented:

** __NSAutoreleaseNoPool(): Object 0x7afafd0 of class NSCFString autoreleased with no pool in place - just leaking
** __NSAutoreleaseNoPool(): Object 0xd8ca4a0 of class __NSCFData autoreleased with no pool in place - just leaking

I'm in a multithreaded environment on iPhone Simulator, downloading a file from the web using WebClient(). I am puzzled how to deal with this as I have no idea what might cause the problem. The thread that is running the download is embedded in

using ( var oAutoRelease = new NSAutoreleasePool (  ) )

I'm attaching to the WebClient's DownloadProgressChanged method and in there I call a delegate which updates the progress view. If I remove this line, the warnings are gone:

ProgressInfo(ACTION.ReceivingResponse, e.ProgressPercentage);

Calling the delegate in turns will go back to my progress controller and udpate a label:

// iIndicator = the value of e.ProgressPercentage.
oProgressController.CurrentActivity = "Percentage done: " + iInidicator.ToString (  ) + "%";
// ProgressController.CurrentActivity:
this.InvokeOnMainThread(delegate { this.oLblCurrentActivity.Text = value; });

What am I missing here!?

EDIT: I figured out that I had to put another NSAutoReleasePool() around this.InvokeOnMainThread(delegate { this.oLblCurrentActivity.Text = value; }); But why? The whole thing is already in a separate pool.

回答1:

This link should help you http://blog.datispars.com/tasks-in-background-thread-cocoa-performselectorinbackground/ Each thread should have it's own autorelease pool



标签: xamarin.ios