Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7438525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:34:05+00:00 2026-05-29T10:34:05+00:00

Is this the right way? // convert const void *buffer = NULL; size_t size

  • 0

Is this the right way?

// convert
const void *buffer = NULL;
size_t size = 0;
dispatch_data_t new_data_file = dispatch_data_create_map(data, &buffer, &size);
if(new_data_file){ /* to avoid warning really - since dispatch_data_create_map demands we care about the return arg */}

NSData *nsdata = [[NSData alloc] initWithBytes:buffer length:size];

// use the nsdata... code removed for general purpose

// clean up
[nsdata release];
free(buffer); // warning: passing const void * to parameter of type void *

It is working fine. My main concern is memory leaks. Leaking data buffers is not fun. So is the NSData, the buffer and the dispatch_data_t new_data_file all fine?

From what I can read on http://opensource.apple.com/source/libdispatch/libdispatch-187.7/dispatch/data.c it seems the buffer is DISPATCH_DATA_DESTRUCTOR_FREE. Does that mean it is my responsibility to free the buffer?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T10:34:06+00:00Added an answer on May 29, 2026 at 10:34 am

    For the most part, your code is correct.
    +initWithBytes:length: will copy the buffer sent in so, you don’t have to worry about freeing the buffer after the data, you can safely free the data first.

    According to the documentation, you do NOT free the data after you are done with it:

    If you specify non-NULL values for buffer_ptr or size_ptr, the values returned in
    those variables are valid only until you release the newly created dispatch data
    object. You can use these values as a quick way to access the data of the new
    data object.

    You simply release the new_data_file variable (ARC will not do this for you).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the right way to convert long to char* const in C++? EDIT:
Is this the right way to convert a string to a list? List styles
Is this the right way to do a nested navigation? <dl> <dt>Struktur</dt> <dd> <ul
Is this the right way to return an object from a function? Car getCar(string
I don't know if I'm thinking of this the right way, and perhaps somebody
I don't know if this is the right way to phrase the question or
Can anyone help me to do this in the right way?I mean.. like that
Sorry, I'm not really sure of the right way to ask this one so
I want to preface this by saying I believe the Right™ Way to handle
What is the most efficient way to do this? Right now I am doing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.