What does the error "Exceeded maximum allowed number of Trackers" mean? - ios11

I'm following this tutorial to implement object tracking on iOS 11. I'm able to track objects perfectly, until a certain point, then this error appears in the console.
Throws: Error Domain=com.apple.vis Code=9 "Internal error: Exceeded maximum allowed number of Trackers for a tracker type: VNObjectTrackerType" UserInfo={NSLocalizedDescription=Internal error: Exceeded maximum allowed number of Trackers for a tracker type: VNObjectTrackerType}
Am I using the API incorrectly, or perhaps Vision has trouble handling too many consecutive object tracking tasks? Curious if anyone has insight into why this is happening.

It appears that you hit the limit on the number of trackers that can be active in the system. First thing to note is that a new tracker is created every time a new observation, with new -uuid property is used. You should be recycling the initial observation you use when you started the tracker, until you no longer want to use it, by feeding what you got from “results” for time T into the subsequent request you make for time T+1. When you no longer want to use that tracker (maybe the confidence score gets too low), there is a “lastFrame” property that can be set, which lets the Vision framework know that you are done with that tracker. Trackers also get released when the sequence request handler is released.

To track the rectangle you feed consequent observations to the same VNSequenceRequestHandler instance, say, handler. When the rectangle is lost, i.e. the new observation is nil in your handler function / callback, or you are getting some other tracking error, just re-instantiate the handler and continue, e.g. (sample code to show the idea):
private var handler = VNSequenceRequestHandler()
// <...>
func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
guard
let pixelBuffer: CVPixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer),
let lastObservation = self.lastObservation
else {
self.handler = VNSequenceRequestHandler()
return
}
let request = VNTrackObjectRequest(detectedObjectObservation: lastObservation, completionHandler: self.handleVisionRequestUpdate)
request.trackingLevel = .accurate
do {
try self.handler.perform([request], on: pixelBuffer)
} catch {
print("Throws: \(error)")
}
}
Note that handler is var, not a constant.
Also, you may re-instantiate the handler in actual handler function (like func handleVisionRequestUpdate(_ request: VNRequest, error: Error?)) in case new observation object is invalid.

My problem with this was that I had a function that called perform... on the same VNSequenceRequestHandler that the tracking was also calling perform on, because of that I was processing too many try self.visionSequenceHandler.perform(trackRequests, on: ciimage) concurrently. Make sure the VNSequenceRequestHandler is not getting hit at the same time by multiple performs....

Related

GetNetworkType in Android 11

Following the changes posted here, the getNetworkType method is deprecated from Android R and onwards.
When trying to use this method in a R compiled application, results in the following exception being thrown:
java.lang.SecurityException: getDataNetworkTypeForSubscriber: uid 10225 does not have android.permission.READ_PHONE_STATE.
at android.os.Parcel.createExceptionOrNull(Parcel.java:2285)
at android.os.Parcel.createException(Parcel.java:2269)
at android.os.Parcel.readException(Parcel.java:2252)
at android.os.Parcel.readException(Parcel.java:2194)
at com.android.internal.telephony.ITelephony$Stub$Proxy.getNetworkTypeForSubscriber(ITelephony.java:7565)
at android.telephony.TelephonyManager.getNetworkType(TelephonyManager.java:2964)
at android.telephony.TelephonyManager.getNetworkType(TelephonyManager.java:2928)
at com.ironsource.environment.ConnectivityService.getCellularNetworkType(ConnectivityService.java:197)
at com.ironsource.sdk.service.DeviceData.updateWithConnectionInfo(DeviceData.java:98)
at com.ironsource.sdk.service.DeviceData.fetchMutableData(DeviceData.java:54)
at com.ironsource.sdk.service.TokenService.collectDataFromDevice(TokenService.java:120)
at com.ironsource.sdk.service.TokenService.getRawToken(TokenService.java:177)
at com.ironsource.sdk.service.TokenService.getToken(TokenService.java:166)
at com.ironsource.sdk.IronSourceNetwork.getToken(IronSourceNetwork.java:183)
This is fine and is expected according to the documentation. If I compile the application to any version before Android R, the exception doesn't show.
This exception indicates that I need to request the android.permission.READ_PHONE_STATE permission.
I wanted to know if there is a way to get the network type with any other API that does NOT require this permission (as this permission's level is dangerous and I would rather not ask the user for it).
Take runtime permission for READ_PHONE_STATE to ignore crash of getDataNetworkTypeForSubscriber
#Override
protected void onStart() {
super.onStart();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
int res = checkSelfPermission(android.Manifest.permission.READ_PHONE_STATE);
if (res != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{android.Manifest.permission.READ_PHONE_STATE}, 123);
}
}
}
private final static int REQUEST_CODE_ASK_PERMISSIONS = 1002;
#Override
public void onRequestPermissionsResult(int requestCode,
#NonNull String[] permissions, #NonNull int[] grantResults) {
switch (requestCode) {
case REQUEST_CODE_ASK_PERMISSIONS:
if (grantResults[0] != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(getApplicationContext(), "READ_PHONE_STATE Denied", Toast.LENGTH_SHORT)
.show();
} else {
}
stepAfterSplash();
break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
You can still use getDataNetworkType(); This method does not necessarily need READ_PHONE_STATE, as stated in his Doc, but that it's sufficient "that the calling app has carrier privileges".
https://developer.android.com/reference/android/telephony/TelephonyManager#getDataNetworkType()
For what I know about getting those privigileges, it could be tricky/really hard, you may look into getting carrier privileges and using this method, which is also the suggested substitution for getNetworkType().
This method necessarily need READ_PHONE_STATE by this way in your activity not just manifest >>>
// Check if the READ_PHONE_STATE permission is already available.
if(ActivityCompat.checkSelfPermission(this,Manifest.permission.READ_PHONE_STATE)
!= PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.READ_PHONE_STATE)) {
//here >> use getNetworkType() method
// like this example
mStationInfo.set_networkType(mTelephonyManager.getNetworkType());
}
else {}
We can use ConnectivityManager#getNetworkCapabilities and NetworkCapablities#hasTransport like this
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkCapabilities caps = cm.getNetworkCapabilities(cm.getActivityNetwork());
boolean isMobile = caps.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR);
boolean isWifi = caps.hasTransport(NetworkCapabilities.TRANSPORT_WIFI);
Reference:
android.net.NetworkInfo
This class was deprecated in API level 29. Callers should instead use the ConnectivityManager.NetworkCallback API
to learn about connectivity changes, or switch to use
ConnectivityManager#getNetworkCapabilities or
ConnectivityManager#getLinkProperties to get information
synchronously. Keep in mind that while callbacks are guaranteed to be
called for every event in order, synchronous calls have no such
constraints, and as such it is unadvisable to use the synchronous
methods inside the callbacks as they will often not offer a view of
networking that is consistent (that is: they may return a past or a
future state with respect to the event being processed by the
callback). Instead, callers are advised to only use the arguments of
the callbacks, possibly memorizing the specific bits of information
they need to keep from one callback to another.
So my friend I have the same trouble as you but so far I came with a temporary fix I use the compileSdkVersion 29 not 30 as well as the targetSdkVersion 29 and my buildToolsVersion 28.0.3 and my app is loading fine.
Since this problem by me its coming due a third party library so till the fix the error I can not fix it alone, but I think with this temporary solution for now is quite well.

EasyNetQ - Custom error queue name based on the original queue

I'd like to give error queues names in EasyNetQ based on the name of the queue that originated the error. For example, all faulted messages from QueueA would go to QueueA.Error; QueueB messages would be redirected to QueueB.Error, and so on.
However, the ErrorQueueNamingConvention doesn't receive a MessageReceivedInfo parameter, as ErrorExchangeNamingConvention does, and so I cannot know what is the name of the original queue. Is there any way of getting it or any workarounds?
Thanks
You can do this by setting the ErrorQueueNamingConvention on the IBus. So it's done at the bus level, not the individual message level. I name my error queues to match the queue to which the consumer is bound (which I define in configuration). When errors occur while processing messages from queue A, for example, those errors will be routed to queue A_Errors. Here's a sample:
var errorExchangeName = _configuration.ExchangeName + "_Errors";
var errorQueueName = _configuration.ListenerQueueName + "_Errors";
var conventions = _bus.Advanced.Container.Resolve<IConventions>();
conventions.ErrorExchangeNamingConvention = info => errorExchangeName;
conventions.ErrorQueueNamingConvention = () => errorQueueName;
You can see in the following code fragment from https://github.com/EasyNetQ/EasyNetQ/blob/master/Source/EasyNetQ/Consumer/DefaultConsumerErrorStrategy.cs how EasyNetQ uses your ErrorQueueNamingConvention to bind to the error queue so it can forward messages to it:
private string DeclareErrorExchangeAndBindToDefaultErrorQueue(IModel model, ConsumerExecutionContext context)
{
var originalRoutingKey = context.Info.RoutingKey;
return errorExchanges.GetOrAdd(originalRoutingKey, _ =>
{
var exchangeName = conventions.ErrorExchangeNamingConvention(context.Info);
model.ExchangeDeclare(exchangeName, ExchangeType.Direct, durable: true);
model.QueueBind(conventions.ErrorQueueNamingConvention(), exchangeName, originalRoutingKey);
return exchangeName;
});
}
So, if you need even more control then you can override this DefaultConsumerErrorStrategy as well. There you can get at the context you are looking for, though I don't think you need it to get the behavior you are looking for.
Also see this answer.

Delay in receiving response or no response when using ExecuteQueryAsync

I am using the ExecuteQueryAsync on multiple lists to fill data into a List<ListItemCollection>. Once all the responses are received, I bind the data onto my Silverlight Application.
ExecuteQueryAsync could ideally go into success or failure. I basically keep track of the total number responses received on both Success and Failure and once the count received matches with the total requests. I show a wait icon till all the responses are received and the data is bound to the application. Now the problem is that, for example, I make 12 requests, I receive only 10 or 11 responses back. I don't seem to understand why I dont receive the response for the last few requests. I neither goes to success not to failure.
Is someone facing this issue? Could you help me understand what is causing this issue and why the response is neither received as a success nor a failure. If I perform the same operation, it works. This issue keeps happening every now and then and I am not sure how to fix this issue.
var requestCount = 0;
var responseCount = 0;
List<ListItemCollection>() bindingData;
public function getData(){
showWaitIcon();
//Some Code here to form the CAML query
bindingData = new List<ListItemCollection>();
for(int i=0; i<10; i++){
//Some Code here to create the Client Context to query each Document Library or List
clientContext.RequestTimeout = -1;
clientContext.Load(clientContext.Web);
ListItemCollection _lstItems;
clientContext.Load(_lstItems);
clientContext.ExecuteQueryAsync(onQuerySucceeded, onQueryFailed);
requestCount++;
}
}
private void onQuerySucceeded(object sender, ClientRequestSucceededEventArgs args)
{
responseCount++;
this.Dispatcher.BeginInvoke(BindData);
}
private void onQueryFailed(object sender, ClientRequestFailedEventArgs args)
{
responseCount++;
this.Dispatcher.BeginInvoke(BindData);
}
private function BindData(){
if(requestCount == responseCount() {
hideWaitIcon();
bindToSilverlightView(bindingData);
}
}
The most probable cause I can think of, without looking through your code, is that you have a synchronization issue, that is, two or more of your requests are receiving the result at nearly the exact same time and trying to modify the variable where you keep track of the number of answered requests. You may need to use a "lock" or similar structure to make sure that block of code is not accessed by two threads at once.

Silverlight -How to handle nested .ExecuteAsync With DevForce

I have a situation as below which gives me error and looks like timeout.
Its missing some insert of records.
the error is as below:
IdeaBlade.EntityModel.AsyncProcessor1.<>c__DisplayClass2.<.ctor>b__0(TArgs args)
at IdeaBlade.EntityModel.AsyncProcessor1.Signal()
at IdeaBlade.EntityModel.AsyncProcessor`1.b__5(Object x)
InnerException:
[HttpRequestTimedOutWithoutDetail]
Arguments:
Debugging resource strings are unavailable. Often the key and arguments provide sufficient information to diagnose the problem. See http://go.microsoft.com/fwlink/?linkid=106663&Version=5.0.10411.00&File=System.ServiceModel.dll&Key=HttpRequestTimedOutWithoutDetail
at IdeaBlade.EntityModel.EntityServerProxy.<>c_DisplayClass14.b_13()
at IdeaBlade.EntityModel.EntityServerProxy.ExecFunc[T](Func1 func, Boolean funcWillHandleException)
at IdeaBlade.EntityModel.EntityServerProxy.ExecuteOnServer[T](Func1 func, Boolean funcWillHandleException)
at IdeaBlade.EntityModel.EntityServerProxy.InvokeServerMethod(SessionBundle sessionBundle, ITypeWrapper entityManagerType, String typeName, String methodName, Object[] args)
at IdeaBlade.EntityModel.EntityMa
Any Idea how to handle it?
Thx:)
......
.ExecuteAsync(op =>
{
var cust =Customers.Where(p => p.IsSelected).ToList();
..........................
Ships.ForEach(.......
...........
EntityManager.SalesGetSalesQuery(
..............
.ExecuteAsync(opn =>
{
................
});
p.UpdateOrders(copyOrders);
Orders.Add(copyOrders);
Save();
});
A timeout can happen at several places, so you will want to increase all possible timeout values.
In this case, you should be looking at increasing the query (CommandTimeout and Transaction), communication, and IIS executionTimeout.
DevForce has a documentation page that talks about troubleshooting timeouts. It's at http://drc.ideablade.com/devforce-2012/bin/view/Documentation/understand-timeouts.
I noticed that your nested query ("SalesGetSalesQuery") is a StoredProcQuery. There is an outstanding bug where StoredProcQueries are not respecting the Transaction timeout value, if different than the default. (120 seconds) We are working on a fix, but unfortunately there's no workaround in the meantime.
If it's not the StoredProcQuery that's timing out, then the link above will help you resolve it.
Job number 1 is to increase the timeout period while you figure out what is taking so long.
This will help https://stackoverflow.com/questions/4877315/silverlight-4-ria-services-timeout-issues
I don't think that the issue is in the fact that the async calls are nested. Remember that the second (i.e. nested) async call will only be executed once the first completed.
What async call is timing out exacly? Is it the StoredProcQuery? (any of them since you're calling them in a loop) If yes, then it's an outstanding bug that we are working on fixing. Like I mentioned in the previous post, there is no workaround. However, since this particular storedProc takes a date range as arguments, one possibility would be 'breaking' this date range in smaller date ranges and issuing multiple async calls. (perhaps in a parallel coroutine) Not that this 'workaround' is not fail proof since all orders could be in a small range period and the async call for that particular range would still timeout.
sbelini.

WPF FormattedText "The system cannot find the file specified" exception in a service

We are using the WPF FormattedText object to determine text size in a service that grabs the latest news headlines from an RSS feed. The text retrieved needs to be in a specified canvas size. The service runs the code every 10 seconds and uses up to 2 threads if one takes longer than that. I'm using TaskFactory (which I've overridden the LimitedConcurrencyLevelTaskScheduler to limit to the amount of threads I specified).
This works great, except after several days (the length is variable), we start to get the following exceptions. The same code was working fine before we started using TPL to make it mult-threaded.
I need help figuring out what this is caused by. A few thoughts I'm looking into are: thread collisions holding on to a TTF file, memory issue, the dispatcher (see the stack trace) isn't playing nicely with the TaskFactory, other??
We don't have good profiling setup, but we've looked at the TaskManager when the exception is occurring and memory usage looks normal.
My next attempt is to use the TextBlock object and see if the exception is avoided.
Error Message: The system cannot find the file specified
Error Source: WindowsBase
Error Target Site: UInt16 RegisterClassEx(WNDCLASSEX_D)
Exception Stack Trace:
at MS.Win32.UnsafeNativeMethods.RegisterClassEx(WNDCLASSEX_D wc_d)
at MS.Win32.HwndWrapper..ctor(Int32 classStyle, Int32 style, Int32 exStyle, Int32 x, Int32 y, Int32 width, Int32 height, String name, IntPtr parent, HwndWrapperHook[] hooks)
at System.Windows.Threading.Dispatcher..ctor()
at System.Windows.Threading.Dispatcher.get_CurrentDispatcher()
at System.Windows.Media.TextFormatting.TextFormatter.FromCurrentDispatcher(TextFormattingMode textFormattingMode)
at System.Windows.Media.FormattedText.LineEnumerator..ctor(FormattedText text)
at System.Windows.Media.FormattedText.DrawAndCalculateMetrics(DrawingContext dc, Point drawingOffset, Boolean getBlackBoxMetrics)
at System.Windows.Media.FormattedText.get_Metrics()
at
(my method using the FormattedText, which is in a loop)
private static Size GetTextSize(string txt, Typeface tf, int size)
{
FormattedText ft = new FormattedText(txt, new CultureInfo("en-us"), System.Windows.FlowDirection.LeftToRight, tf, (double)size, System.Windows.Media.Brushes.Black, null, TextFormattingMode.Display);
return new Size { Width = ft.WidthIncludingTrailingWhitespace, Height = ft.Height };
}
EDIT: so far I've tried placing a lock around the code that calls this function, and calling it inside the CurrentDispatcher.Invoke method like so:
return (Size)Dispatcher.CurrentDispatcher.Invoke(new Func<Size>(() =>
{
FormattedText ft = new FormattedText(txt, new CultureInfo("en-us"), System.Windows.FlowDirection.LeftToRight, tf, (double)size, System.Windows.Media.Brushes.Black, null, TextFormattingMode.Display);
return new Size { Width = ft.WidthIncludingTrailingWhitespace, Height = ft.Height };
}));
EDIT: I've found links to others having similar, but not the exact problem.
http://www.eggheadcafe.com/software/aspnet/31783898/problem-creating-an-bitmapsource-from-an-hbitmap-in-threaded-code.aspx ~having a similar problem, but no answers
System.Windows.Media.DrawingVisual.RenderOpen() erroring after a time ~having a similar problem, but no answers
http://connect.microsoft.com/VisualStudio/feedback/details/361469/net-3-5-sp1-breaks-use-of-wpf-under-iis# ~ similar exception, but we're not using 3.5SP1 or IIS 7.
I've also submitted this through the Microsoft Connect site (please vote for it if you are having a similar problem).
https://connect.microsoft.com/WPF/feedback/details/654208/wpf-formattedtext-the-system-cannot-find-the-file-specified-exception-in-a-service
EDIT: Response from Microsoft:
"WPF objects need to be created on Dispatcher threads, not thread-pool threads. We usually recommend dedicating a thread to run the dispatcher loop to service requests to create objects and return
them frozen. Thanks, WPF Team" ~ How would I implement this?
EDIT: final solution thanks to NightDweller
if(Application.Current == null) new Application();
(Size)Application.Current.Dispatcher.CurrentDispatcher.Invoke(new Func<Size>(() =>
{
...});
EDIT: When I deployed the change (new Application();), I got an error logged " Cannot create more than one System.Windows.Application instance in the same AppDomain."
Error Source: PresentationFramework
Error Target Site: Void .ctor()
A shot in the dark:
The stack trace seems to show that WPF does not find a Dispatcher in the thread executing GetTextSize, so it has to create a new one, which involves creating a handle to a window.
Calling this every 10 seconds means 8'640 threads, thus windows per day. According to Mark Russinovich, there is a limit of 32 K windows per session, which may explain the error in RegisterClassEx.
An idea to overcome this is to read the current dispatcher from your main thread and set it in your tasks.
Edit:
I had another look and it looks like one cannot set the Dispatcher of a thread (it's created automatically).
I'm sorry, I am unable to understand what is going on here.
In order to compute the text size, WPF needs a FormattedText instance, which is stored as a member of the Dispatcher class. The existing Dispatchers are stored in a list of weak references. Each one is associated with a specific thread.
Here, it looks like new Dispatcher instances are created many, many times.
So, either the calling thread is new or memory is quite low and the weak references have been discarded.
The first case (new thread) is unlikely as the task scheduler uses the thread pool, which has about 25 threads per core (if I remember correctly), which is not enough to deplete the pool of ATOMs or windows.
In the second case, the depletion of resource is unlikely as the HwndWrapper is IDisposable and the Dispose method takes care of freeing the registered class.
As you already know from the info you provided, All UI elements (FormattedText is one) have to be created on the UI thread.
The code you are looking for is:
return (Size)Application.Current.Dispatcher.CurrentDispatcher.Invoke(new Func<Size>(() =>
{
FormattedText ft = new FormattedText(txt, new CultureInfo("en-us"), System.Windows.FlowDirection.LeftToRight, tf, (double)size, System.Windows.Media.Brushes.Black, null, TextFormattingMode.Display);
return new Size { Width = ft.WidthIncludingTrailingWhitespace, Height = ft.Height };
}));
Notice the Application.Current - you want the "Application" dispatcher which is the dispatcher for the UI thread in WPF applications.
Your current code actually creates a dispatcher for the current thread so you didn't really change the executing thread (see here regarding the dispatcher)
Have you renamed anything? If yes, check that link: WPF Prism: Problem with creating a Shell

Resources