Xamarin.iOS/monotouch memory management - xamarin.ios

I’m trying to understand the memory management needed in Xamarin.iOS.
In the following scenario, does this.NavigationController.PopToRootViewController release the created UIViewControllers or
should I release them? If I need to do it, where should this be done?
Each letter represent a UIViewController)
A ( rootViewcontroller)
from A:
B b = new B()
this.NavigationController.PushViewController (b, true);
From B:
C c = new C()
this.NavigationController.PushViewController (c, true);
From C:
D d = new D()
this.NavigationController.PushViewController (d, true);
From D:
this.NavigationController.PopToRootViewController(true);

Can you enable "Use Reference Counting Extension"? This will take care of this for you.

Related

`ara::core::Result< T, E >::Result (T const &t);` vs `static Result ara::core::Result< T, E >::FromValue (T const &t);`

Since ara::core::Result< T, E >::Result (T const &t); allready could construct a new Result from the specified value (given as lvalue), it seems meaningless to to provide such a method(static Result ara::core::Result< T, E >::FromValue (T const &t);) to achieve the similar goal.
Am I missing something? If I am wrong, please let me know.
As per the document of AutoSar, which says that
Build a new Result from the specified value (given as lvalue).
Here is the example code snippet:
int foo =1;
[]()->ara::core::Result<int>{
return ara::core::Result<int>::Result(foo);
};
[]()->ara::core::Result<int>{
return ara::core::Result<int>::FromValue(foo);
};

Implement phone directory using two tries

I have encountered an interview question
“Implement a phone directory using Data Structures”
I want to solve it using tries.By solving it with tries,I tried using two tries,one for name and another for phone number,
but I faced a difficulty .
Suppose ,I have to add three entries( AB “112” BC ”124” CD ”225”)
Then if I query the name for number “225”,how do I return CD.
that is,how these two tries will be linked .
One approach I was thinking was taking two pointers in both the tries.
These pointers will point to the first and last word in the other trie.
For example,if the structures are as follows:
Struct nametrie
{
Struct nametrie *child[26];
struct phonetrie*head,*tail;
struct phonetrie*root;
-----------
}
Struct phonetrie
{
struct phonetrie*child[9];
struct nametrie*head,*tail;
struct nametrie*root;
-----------
}
Then for AB “112”,
Name trie willstore head(1) and tail (2).
But I think this approach will not work for duplicate entries(one name and multiple numbers.)
Can someone please explain a good approach.I am not looking for code but good understanding of approach,may be via diagram or algorithm.
I dont know C so I cant comment in your code.
The idea of using tries is valid.
you seems to be missing what data the nodes can hold in tries
the node in trees has 2 main components
the data it has which can be anytype
list of childen (or left , right childeren) or any combination of children
what we will do here is that we will add another field to each node and call it the value "theValue"
So the trie node will look like this
Class TrieNode{
public char theChar;
public String theValue;
public List<TrieNode> children;
}
So for forward lookup (name to phone) you construct one Trie and on the node that match entry in the directory you will set theValue to that entrie.
you will need to create 2nd trie to do the same for reverse lookup (phone to name)
So to give you example how it will look like for this data it will be
( AB “112” AC ”124” ACD ”225”)
//create nodes
TrieNode root = new TrieNode();
TrieNode A = new TrieNode();
A.theChar = 'A';
TrieNode B = new TrieNode();
A.theChar = 'B';
TrieNode C = new TrieNode();
A.theChar = 'C';
TrieNode C2 = new TrieNode();
A.theChar = 'C';
TrieNode D = new TrieNode();
A.theChar = 'D';
//link nodes together
root.children = new ArrayList<>();
root.children.add(A);
A.children = new ArrayList<>();
A.children.add(B);
A.children.add(C);
B.children = new ArrayList<>();
B.children.add(C2);
//fill the data
B.theValue = "112";
C.theValue = "124";
C2.theValue = "225";
now you can easy traverse this Trie and when you reach a node and whant to check the value just read theValue
i hope it is clear

Create embedded instances dynamically during navigation

I am really digging groovy's ability to navigate hierarchies of objects elegantly using the dot notation.
One question I have, is there a way to create embedded objects elegantly as well during navigation. For instance, given the following classes:
class Bar {
int a
}
class Foo {
Bar b
}
I want to be able to do this:
Foo f = new Foo()
f.b.a = 4
Currently, I have to do:
Foo f = new Foo()
f.b = new Bar()
f.b.a = 4
Note that I need f.b to be null unless it has been set via navigation or otherwise. So blindly instantiating b = new Bar() within the class Foo does not meet the needs of the requirement.
You may use null safe operator ?. Or override a getter for b in Foo and if it's null set new instance of b in Foo and return it.
It would be:
class Foo {
Bar b
Bar getB() {
if(b == null)
this.#b = new Bar()
b
}
}
You could initialize the entire hierarchy in a single line, like so:
Foo f = new Foo(b: new Bar(a: 4))

NDepend rule to warn if objects of a given type are compared using ==

as the title says: I need a NDepend rule (CQLinq) for C#/.net code, that fires whenever instances of a given type are compared using == (reference comparison). In other words, I want to force the programmer to use .Equals.
Note that the type in question has no overloaded equality operator.
Is this possible? If so, how? :)
Thanks, cheers,
Tim
With the following code with see that for value type, == translate to the IL instruction: ceq. This kind of usage cannot be detected with NDepend.
int i = 2;
int j = 3;
Debug.Assert(i == j);
var s1 = "2";
var s2 = "3";
Debug.Assert(s1 == s2);
However for reference types we can see that a operator method named op_Equality is called.
L_001d: call bool [mscorlib]System.String::op_Equality(string, string)
Hence we just need a CQLinq query that first match all method named op_Equality, and then list all callers of these methods. This can look like:
let equalityOps = Methods.WithSimpleName("op_Equality")
from m in Application.Methods.UsingAny(equalityOps)
select new { m,
typesWhereEqualityOpCalled = m.MethodsCalled.Intersect(equalityOps).Select(m1 => m1.ParentType) }
This seems to work pretty well :)

Parameters of a object get lost (XCode for IPhone)

I have here a strange behaviour. XCode 4 for iOS 6 (Iphone 4s)
In the init-section I want to read data from a file.
NSString *rawData = [[NSString alloc] initWithContentsOfFile:filePathLib encoding:NSUTF8StringEncoding error:NULL];
NSArray *zeilen = [rawData componentsSeparatedByString:#";"];
NSLog(#"Check1 \n%#\n%#",rawData, zeilen);
OK, now the LogItem shows me, that the correct data were read.
Now I have an if-condition:
if ([zeilen count]==4){
A = [zeilen objectAtIndex:0];
B = [zeilen objectAtIndex:1];
C = [zeilen objectAtIndex:2];
D =[[zeilen objectAtIndex:3] intValue];
NSLog(#"Check2 \n%#\n%# - %# - %# - %# - %d",rawData, zeilen, A, B, C, D);
} else {
A = #"A0";
B = #"B0";
C = #"C0";
D = 3;
}
Now I run this programm.
If the first branch was used, the program crashes, if the second was used, it runs perfectly.
If I use break points I can observe the following:
A NSString * 0x20063bf0 #"A123" (the correct value)
B, C, D also correct
later, a line before the crash:
file-read branch:
A, B and C are NOT available, D is correct.
default, branch:
A, B, C and D are correct.
The variables are nowhere else manipulated or even deleted.
Has anyone an idea, where the problem is?
My assumption: The array "zeilen" does not contain NSString-values and therefore they are somehow deleted. But, I do not really believe in this theory, because at the beginning the elements are correct and available and only later they are deleted.
Any idea?
Solved it:
replace:
A = [zeilen objectAtIndex:0];
by
A = [[NSString alloc] stringWithFormat:#"%#",[zeilen objectAtIndex:0]];
Memory was not allocated, therefore it got lost.

Resources