vyasr commented on code in PR #406: URL: https://github.com/apache/arrow-nanoarrow/pull/406#discussion_r1534426874
########## examples/cmake-scenarios/src/library.cpp: ########## @@ -0,0 +1,39 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#include "nanoarrow/nanoarrow.h" +#include "nanoarrow/nanoarrow.hpp" + +#include "library.hpp" + +std::optional<std::pair<std::unique_ptr<ArrowArray>, std::unique_ptr<ArrowSchema>>> +make_simple_array() { + nanoarrow::UniqueArray tmp_array; + nanoarrow::UniqueSchema tmp_schema; + auto result = make_simple_array(tmp_array.get(), tmp_schema.get()); + if (result != 0) { + if (tmp_array.get()->release) tmp_array.get()->release(tmp_array.get()); + if (tmp_schema.get()->release) tmp_schema.get()->release(tmp_schema.get()); + return std::nullopt; + } + auto ret_array = std::make_unique<ArrowArray>(); Review Comment: OK yeah so in that example the library interface is a C++ interface, so it's fine to return a `UniqueSchema`. That makes sense, I was just saying that I had written my example with the assumption that you'd typically want to code this kind of thing with nanoarrow assuming that the other library could be a C library consuming it. I guess the core issue here is that I wrote an example that's a very odd hybrid of C and C++. I made some assumptions that we'd end up interfacing with a C library, but at the moment the functions use C++ interfaces. That's also clear from my second question about lifetimes. I think what you're getting at is that because I'm using a `unique_ptr` to the array, you'd need to set a custom deleter on that object so that when the pointer goes out of scope the right deleter is called, which is completely true. I was just assuming that in practice this object would need to be released and the underlying raw pointer would need to be passed back, at which point the real consumer would be in charge of calling the release callback as per the C data interface contract, but you're totally right that as the code is written now there's a potential memory leak because I'm handing back a `unique_ptr` that could go out of scope and delete the array without actually freeing its underlying buffers. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
