Le dimanche 21 août 2016 à 01:36 -0700, Andreas Lobinger a écrit :
> Hello colleagues,
>
> i'm trying to use unsafe_wrap from a pointer from an external call
> (cfunction) to an array access.
> Looks like i have type problems:
>
>
>
> function read_from_stream_callback(s::IO, buf::Ptr{UInt8},
> len::UInt32)
>
> #b1 = zeros(UInt8,len)
> #nb = readbytes!(s,b1,len)
>
> #for i=1:len
> # unsafe_store!(buf,b1[i],i)
> #end
>
> b1 = zeros(UInt8,len)
> unsafe_wrap(b1,buf,len)
> nb = readbytes!(s,b1,len)
>
> @compat(Int32(0))
> end
>
> _
> _ _ _(_)_ | A fresh approach to technical computing
> (_) | (_) (_) | Documentation: http://docs.julialang.org
> _ _ _| |_ __ _ | Type "?help" for help.
> | | | | | | |/ _` | |
> | | |_| | | | (_| | | Version 0.5.0-rc2+0 (2016-08-12 11:25 UTC)
> _/ |\__'_|_|_|\__'_| |
> |__/ | x86_64-linux-gnu
>
> julia> include("png_stream.jl")
> ERROR: LoadError: MethodError: no method matching
> unsafe_wrap(::Array{UInt8,1}, ::Ptr{UInt8}, ::UInt32)
> Closest candidates are:
> unsafe_wrap(::Type{String}, ::Union{Ptr{Int8},Ptr{UInt8}},
> ::Integer) at pointer.jl:86
> unsafe_wrap(::Type{String}, ::Union{Ptr{Int8},Ptr{UInt8}},
> ::Integer, ::Bool) at pointer.jl:86
>
> unsafe_wrap{T}(::Union{Type{Array{T,1}},Type{Array{T,N}},Type{Array}}
> , ::Ptr{T}, ::Integer) at pointer.jl:57
> ...
> in
> read_from_stream_callback(::Base.AbstractIOBuffer{Array{UInt8,1}},
> ::Ptr{UInt8}, ::UInt32) at /home/lobi/juliarepo/png_stream.jl:16
> in read_png_from_stream(::Base.AbstractIOBuffer{Array{UInt8,1}}) at
> /home/lobi/juliarepo/png_stream.jl:26
> in include_from_node1(::String) at ./loading.jl:426
> while loading /home/lobi/juliarepo/png_stream.jl, in expression
> starting on line 55
>
> to which i somehow disagree (as long as UInt32 is an Integer).
>
> the commented code runs correctly (copy to but UInt8 seq.)
>
> A better explaination of the error?
The problem is with the first argument: it should be a type, not an
array. You should simply pass 'Array' instead of 'b1': no need to
allocate a buffer, as this function uses the original memory referred
to by the pointer (without making a copy).
Regards
> Wishing a happy day,
> Andreas
>
>