-
Notifications
You must be signed in to change notification settings - Fork 147
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
broken Base.unsafe_convert method returns garbage pointers #53
Comments
Thanks Jameson for the input. I must admit I find it slightly difficult to navigate what is possible and what is not (my approach has been to play at the REPL and see what works and what doesn't). The purpose of this function was to make it easy to pass a pointer to a stack allocated (remember, allocating and garbage collecting the To give a better idea of what I'm attempting, take the command
Here the |
Yes (assumption not needed)
No.
They both allocate. The only different between using |
@andyferris I suspect the main problem with a lot of these subtle issues is that you're still thinking about immutables as stack allocated C types, with similar lifetime rules. But this is quite far from the real language semantics. Julia semantics says nothing about this (as far as I know), simply that an immutable is an object whose fields may not be rebound; pretty much everything else is implementation details. For example:
I suspect the compiler should feel quite free to transform this function foo()
m = eye(SMatrix{2,2,Int})
dirty_tricks!(m) # Attempt to modify m in place
m + m
end into this function foo()
m = eye(SMatrix{2,2,Int})
m2 = m # Copies allowed, it's immutable after all.
dirty_tricks!(m) # Woo, the implementation didn't stop me
m2 + m2 # Uh oh
end I could be wrong about some of the details above, but if you break the semantics of the language, anything can happen, and frequently does when you don't expect it :-) Eg, you wouldn't try to use a reference to a C++ class after it goes out of scope. It might "work" simply because the associated memory hasn't be reused for anything else yet (eg, the stack slots recycled for another variable newly in scope in the same function, or the heap memory reallocated). But it'll fail randomly after some refactoring. |
Right, thanks @vtjnash for clearing that up - I think I understand better now! Does the heap allocation occur because of the @c42f I do agree with all of that... and the attempt here was never to mutate them - just to pass immutable inputs to Fortran functions at high speed. But things like you mention here
probably make it impossible to use this approach. I think what I should do is just use the |
Oh wait, nevermind, I guess they are boxed because of the |
Hmm, this is deprecated in favor of |
Yes, I learnt |
Right, there seems to be a fair bit of specialized code in ccall.cpp around |
Thank you very much Jameson for the contribution (I pushed this one to master). |
data_pointer_from_objref
returns an invalid pointer when called onSMatrix
. This function should thrown an error in this case (the case where the object can be stack allocated), but that error checking hasn't been implemented yet. That means this function is invalid:The correct way to implement this is to use
Ref
:The text was updated successfully, but these errors were encountered: