How to check if two JNI Arrays point to the same memory location? - android-ndk

I have two arrays:
auto inputArray = reinterpret_cast<jbyteArray>(mainEnv->NewGlobalRef(imageDataArray));
auto output = reinterpret_cast<jfloatArray>(mainEnv->NewGlobalRef(data));
When I try:
auto input = env->GetByteArrayElements(inputArray, nullptr);
I'm getting this error:
"JNI DETECTED ERROR IN APPLICATION: attempt to get byte primitive array elements with an object of type float[]"
My guess is "inputArray" (byte array) point to the same memory location of "output" (float array).
How can I check that?

You can tell if two object references point to the same object with the JNI IsSameObject function.
The error message is telling you that you're calling GetByteArrayElements on a float[]. Getting the array object's class (with GetObjectClass) would let you query the class of the object at the point it's passed to native code, so you can confirm that the arrays have the types you expect. From there you can narrow your focus and figure out where things are going wrong.

Related

How to use EncodedObjectAsID?

I'm trying to understand get_instance_id()
and I came across this line in the documentation:
This ID can be saved in EncodedObjectAsID, and can be used to retrieve
the object instance with #GDScript.instance_from_id.
I can't seem to understand what this statement means exaclty and how to use EncodedObjectAsID, could someone please provide a working example?
The EncodedObjectAsID follows a pattern called Boxing. Boxing is where you put a primitive value, like an int, into an object. This boxed primitive can now be used in an object oriented way. For example, you can pass the boxed int to a function that only takes objects (i.e. it applies Polymorphism):
func only_takes_object(obj: Object)
only_takes_object(123) # Error
var box = EncodedObjectAsID.new()
box.object_id = 123
only_takes_object(box) # Valid
This is how parts of the editor use the EncodedObjectAsId object.
In marshalls.cpp we can see that an encoded Object may be an integer ID or the whole object. When it is flagged as only an integer ID a EncodedObjectAsID object is created. This object is then converted to a Variant.
When adding a stack variable in editor_debugger_inspector.cpp a variant with a type of object is assumed to be and converted to an EncodedObjectAsID to fetch the referenced object's id.
Here's two more links that follow a similar pattern:
array_property_edit.cpp
scene_debugger.cpp
Note that Variant can be implicitly converted to an Object and Object::cast_to() only takes Objects.
This ID can be saved in EncodedObjectAsID, and can be used to retrieve the object instance with #GDScript.instance_from_id.
This sentence should be split into two independent clauses. It should read as
"The instance ID can be saved in an EncodedObjectAsID."
"The instance ID can be used to retrieve the object instance with #GDScript.instance_from_id()."
Note: You should not store an object's id in storage memory. There is no guarantee that an object's id will remain the same after restart.

Hazelcast Predicate in clause

Can anyone tell me how to 'in' clause with Hazelcast predicate. I want to use the following,
Predicates.in('column',value);
I want to pass the value as an ArrayList of values. But it is throwing error as it expects object that implements comparable. Is there any workaround for this?
Predicates.in takes arguments of types (String, Comparable...).
So for the column name you pass a String as you have done.
Comparable... means you can pass the individual values in a comma-separated list, but you can also pass them in an array. An ArrayList won't automatically be converted, but you can do it as follows:
ArrayList<String> values = new ArrayList<>(Arrays.asList("one", "two", "three");
Predicates.in("column", values.toArray(new String[]));
The (new String[]) argument is just to ensure you get back an array of Strings, otherwise you'll get an array of Objects.

Declaring an instance of a composite type without initializing all the fields

So I am trying to create an instance of a structure:
struct keypoint
x
y
scale
angle
Vector{Any}(VecLength)
end
Now I know the values of all the fields except the last one. I need to initialize the instance of the structure with the known values but for the last field I have to call another function where the data to be generated and then stored in the last field of the instance. Is there a way to get this done in Julia?
I am referring to the tutorials here and here but I guess in both places all the fields of the instance have been initialized at one go.
Thanks!
mutable struct keypoint
x
y
scale
angle
keypoint(x,y,scale) = new(x,y,scale)
end
a = keypoint(1,1.0,2.0) # keypoint(1, 1.0, 2.0, #undef)
Notice that if you then try to access a.angle you get
ERROR: UndefRefError: access to undefined reference
Stacktrace:
[1] getproperty(::Any, ::Symbol) at .\sysimg.jl:18
so by leaving it off you get an undef in there that errors upon access. But you can then set it later.

TypeError: Cannot read property 'substring' of undefined

Written in node.js file
The function is returning the error:
Returns the ACE occupation exhibit identifier
Example of originalID: 46R-002-30BroadJour12_01-12_11
Expected output: 46R-002
/*This function is returning the error:
Cannot read property 'substring' of undefined*/
function splitID(originalID){
var aceid = originalID.substring(0,7);
return aceid;
}
//1. Get the ace exhibit occupation id for each of them and put it in a parallel array.
for (var row in values) {
//split the 5th column using our function
var output = splitID(row[4]);
var result = getOccupation(output);
//now we add the split output to our occupation array.
occupationsToInsert.append(result);
}
If you may refer to the documentation here at MDN, it advises against using for...in for looping over the arrays because it does not give consistent values on return. It rather iterates on the enumerable properties of the concerned object passed to it.
In other words, for (var row in values) would not iterate over each individual rows as expected, but rather the enumerable properties of the values list.
So, for your const array, you can find the enumerable properties by simply doing
Object.getOwnPropertyNames(values)
which would return you the following list :
["0", "length"]
You're essentially trying to access the fourth element of this array which doesn't exist, and thereby it is undefined, causing the error you observe.
The error is telling you the exact problem: originalID is undefined. In your for loop, row[4] is resulting in an undefined value. Verify your values array contains what you are expecting.

Casting to types that are know only at Runtime by their string names. C#

I've got a problem here. (C#)
There's a collection in another assembly (I cannot change it) that takes a string as parameter and returns an object.
Like:
object Value = ThatCollection.GetValue("ParameterName");
The problem is, for each parameter string, it returns a DIFFERENT type as object.
What I want is to cast those objects to their respective types, knowing the types only at runtime by their string names.
I need to do some operations with those returned values.
And for that I need to cast them properly in order to access their members and so.
Limitations:
I cannot use "dynamic" since my code needs to be done in an older framework: 3.5 (because of interop issues).
I need to do operations with MANY returned values of different types (no common interfaces nor base classes, except "object", of course)
All I have is a table (containing string values) correlating the parameter names with their returned types.
Yes, I could transform that table into a biiig "switch" statement, not very nice, don't want that.
Any hints??
You want to look into reflection, something like the following should work to cast an object to type T. Set up a simple cast method:
public static T CastToType<T>(object o)
{
return (T)o;
}
Invoke this using reflection:
Type t = Type.GetType(stringName)
MethodInfo castTypeMethod = this.GetType().GetMethod("CastToType").MakeGenericMethod(t);
object castedObject = castTypeMethod .Invoke(null, new object[] { obj });

Resources