You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2022/10/14 21:23:57 UTC

[GitHub] [arrow-rs] tustvold commented on issue #2874: Pointer freed error when deallocating ArrayData with shared memory buffer

tustvold commented on issue #2874:
URL: https://github.com/apache/arrow-rs/issues/2874#issuecomment-1279482662

   @jhorstmann is more familiar with this part of the codebase than myself, but I suspect you need to use [Buffer::from_custom_allocation](https://docs.rs/arrow-buffer/latest/arrow_buffer/buffer/struct.Buffer.html#method.from_custom_allocation). You can then attach an `Arc` to the shared memory segment to ensure it has the appropriate lifetime
   
   Perhaps something like (not tested)
   
   ```
   use std::ptr::NonNull;
   use shared_memory::{ShmemConf, ShmemError};
   use arrow::{array::{ArrayData}, datatypes::DataType, buffer::Buffer};
   use tracing::error;
   use std::sync::Arc;
   
   fn main(){
       let os_id = "test_200";
       let shmem = match ShmemConf::new().size(1024).os_id(os_id).create() {
           Ok(m) => m,
           Err(ShmemError::MappingIdExists) => ShmemConf::new().os_id(os_id).open().unwrap(),
           Err(e) => {error!("Unable to create or open shmem os_id {} : {}",os_id, e);
               return
           }
       };
       let ptr = shmem.as_ptr();
       let allocation = Arc::new(shmem);
       let data = unsafe{        
           let buf = Buffer::from_custom_allocation(NonNull::new(ptr).unwrap(), 1024, allocation); 
           
           let data = ArrayData::builder(DataType::UInt8)
               .len(4)
               .add_buffer(buf)
               .build()
               .unwrap();
   
           data
       };
       println!("data: {:?}", data);   
   }
   ```
   
   This will drop the shmem segment after the Buffer has been dropped
   


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org