You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
With optimisation enabled, obtaining a pointer using withUnsafePointer and storing the pointer in a union inside a struct which is passed as a pointer to a C function causes the original struct instance to be deallocated before it is used in the C function.
When run without optimisation (debug scheme) the program runs correctly (prints 1, 2, 3). When run with optimisation (release scheme), `str` is released early and the memory read inside the C function is invalid (prints 0, 0, 0). Uncommenting the print statement causes the struct instance to be retained even when optimised.
The text was updated successfully, but these errors were encountered:
Thanks for the reproducer project, it's just fantastic. It seems like the optimizer is ignoring that taking the address of the temporary counts as a non-removable use and is just erasing everything. In fact, the IR in release just shows everything got inlined into `main` and we're just passing a freshly alloca'd value to your function in C.
Good to know it is a bug and I'm not just misusing swift! For anyone suffering from this until a fix is released, it can be worked around using withExtendedLifetime on the s variable
Attachment: Download
Environment
Xcode Version 11.4 (11E146), macOS 10.15.4
Additional Detail from JIRA
md5: 8c20184c1e5d46cc15863bed3d2ef704
Issue Description:
The attached project demonstrates the issue.
With optimisation enabled, obtaining a pointer using withUnsafePointer and storing the pointer in a union inside a struct which is passed as a pointer to a C function causes the original struct instance to be deallocated before it is used in the C function.
When run without optimisation (debug scheme) the program runs correctly (prints 1, 2, 3). When run with optimisation (release scheme), `str` is released early and the memory read inside the C function is invalid (prints 0, 0, 0). Uncommenting the print statement causes the struct instance to be retained even when optimised.
The text was updated successfully, but these errors were encountered: