Thanks, yes I see what you mean. Actually I still need to write the 'transpose' part that is mentioned in the comment and that implies making a data copy.
terça-feira, 17 de Fevereiro de 2015 às 16:42:10 UTC, Jameson escreveu: > > You appear to be returning pointers to memory that are outliving the life > of their owning object. The array "out" appears to point to memory owned by > R (which you seem to be explicitly invalidating at the end of the loop) and > you use the result of pointer([ ]), without holding a reference to [ ] in a > local variable. > On Tue, Feb 17, 2015 at 11:28 AM J Luis <[email protected] <javascript:>> > wrote: > >> Hi, >> >> When I run the function bellow I have this totally unexpected (to me at >> least) variable corruption effect >> >> X[item].direction = 0 >> out = Float32[1.0f0] >> X[item].direction = 1 >> out = Float32[5.0f0,5.0f0,5.0f0,5.0f0,5.0f0,5.0f0] >> X[item].direction = 0 >> out = Float32[-1.9983972f18,-1.9983972f18,-1.9983972f18,-1.9983972f18,- >> 1.9983972f18,-1.9983972f18] >> >> The loop runs 3 times and the second iteration holds the correct value. >> But than, in the 3rd iteration something corrupts the the 'out' variable >> (the code doesn't touch it) and I'm thus unable to return its correct value. >> What am I doing wrong, or is there a bug? >> >> function GMTJL_post_process(API::Ptr{Void}, X, n_items::Int) >> out = [1.f0] >> for (item = 1:n_items) >> if (X[item]._type == GMT_IS_GRID) # We read or wrote >> a GMT grid, examine further >> # >> if ((R = GMT_Retrieve_Data(API, X[item].ID)) == C_NULL) >> error("GMTJL_PARSER:Error retrieving grid from GMT\n") >> end >> convert(Ptr{GMT_GRID}, R) >> elseif (X[item]._type == GMT_IS_DATASET) >> @show(X[item].direction) >> if (X[item].direction == GMT_OUT) # Here, GMT_OUT means >> "Return this info to Julia" >> if ((R = GMT_Retrieve_Data(API, X[item].ID)) == C_NULL) >> error("GMTJL_PARSER: Error retrieving matrix from >> GMT") >> end >> Rb = unsafe_load(convert(Ptr{GMT_MATRIX}, R)) >> >> if (Rb.shape == GMT_IS_COL_FORMAT) # Easy, just copy >> #memcpy (d, M->data.f8, M->n_rows * M->n_columns * >> sizeof (double)); >> else # Must transpose >> out = pointer_to_array(convert(Ptr{Cfloat},Rb.data), >> Rb.n_rows * Rb.n_columns) >> end >> else >> R = X[item].obj; >> end >> @show(out) >> # Else we were passing Julia data into GMT as data input and >> we are now done with it. >> # We always destroy R at this point, whether input or >> output. The alloc_mode >> # will prevent accidential freeing of any >> externally-allocated arrays. >> >> if (GMT_Destroy_Data(API, pointer([R])) != GMT_NOERROR) >> error("GMTJL_post_process: Failed to destroy matrix R >> used in the interface bewteen GMT and Julia") >> end >> end >> end >> >> return out >> >> end >> >> >>
