-
Notifications
You must be signed in to change notification settings - Fork 167
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix TableKey / ObjKey conversion on Android x86 #2477
Conversation
32273cf
to
0856397
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Final nits, otherwise it looks good.
wrappers/src/object_cs.cpp
Outdated
// ObjKey is incompatible with C, so we return just the value. | ||
return object.obj().get_key().value; | ||
auto table_key_value = object.obj().get_table()->get_key().value; | ||
auto table_key_hash = std::hash<int>{}(table_key_value); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need these std::hash
-es here? Can we use table_key_value
directly - maybe with a cast?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess the normal way would be to only use them since they are already provided by the standard library but the other way round should be fine as well. Removed them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I'm not familiar with how C++ does things, so happy to use the idiomatic way there - it's just coming from C#, you would rarely use GetHashCode
on a primitive type when you can just use the value itself.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did some more research now that you mention it. Good point about the primitives:
https://stackoverflow.com/questions/38304877/why-stdhashint-seems-to-be-identity-function
It makes sense but means that I can just remove it since std::hash<int>(i) == i
anyway.
I'll just keep it our way then. There are certainly many more ways but if the current implementation is fine I guess we can just keep it.
I thought about just concatenating table key and object key since that would always lead to a unique 'hash':
auto table_key_value = object.obj().get_table()->get_key().value;
auto object_key_value = object.obj().get_key().value;
std::string concatenation = "" + table_key_value + object_key_value;
int hashCode = std::stoi(concatenation);
return hashCode;
I had to add c249f25 since there was a valid warning on UWP:
@nirinchev fyi + above comment about hashes. It's just a tiny change, but I don't like merging unseen commits. |
wrappers/src/object_cs.cpp
Outdated
auto table_key_value = object.obj().get_table()->get_key().value; | ||
auto object_key_value = object.obj().get_key().value; | ||
|
||
int32_t hashCode = -9999999769; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That warning is correct - -9999999769
is beyond what int32
can hold. This is what VS generated for a simple struct with table key and object key if you want to use it:
public override int GetHashCode()
{
var hashCode = -986587137;
hashCode = hashCode * -1521134295 + ObjKey.GetHashCode();
hashCode = hashCode * -1521134295 + TableKey.GetHashCode();
return hashCode;
}
Co-authored-by: Nikola Irinchev <[email protected]>
85f88c5
to
270ab19
Compare
* master: Update the AppConfiguration.Logger obsolete message (#2495) Add more serializer types to the preserved ones (#2489) Update changelog from Core (#2488) Build an xcframework for iOS (#2475) Prepare for vNext (#2487) Prepare for 10.2.1 (#2484) Fix TableKey / ObjKey conversion on Android x86 (#2477) Update the build manager locations (#2474) Clean up native resources on Unity application exit (#2467) Fix some native warnings (#2483) Don't try to get the app from a removed user (#2480) Use delete_files from core when deleting a Realm. (#2401) Fixes for the sync datatype tests (#2461) Clean up the package.json and add some docs (#2452) Run iOS tests on CI (#2405) Verify that objects do not belong to different realm when added to collection (#2465) Updated README instructions (#2459)
Description
When retrieving
TableKey
andObjKey
from Core we interpret the returned values of typeUInt32
andInt64
asTableKey
andObjKey
without any conversion.On Android x86 this leads to a misalignment in memory though since
TableKey
andObjKey
are structs which leads to a crash as described in #2456.This PR reads the return values correctly and converts them in their corresponding Handle into the structs used on the .NET side.
N.B.: No further tests added. The problem that this was not caught by our tests is not the test suite itself but the fact that we do not test on Android x86 at the moment. As soon as such a device is added and run the current tests will implicitly apply.
In addition to that the hash code of objects is now calculated using table key in addition to just the object key (see #2473).
Fixes #2456
Fixes #2473
TODO