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

  • SEARCH
  • Home
  • 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 6588951
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:06:44+00:00 2026-05-25T17:06:44+00:00

We have an NSMutableData object that frequently has data appended to it. We also

  • 0

We have an NSMutableData object that frequently has data appended to it. We also frequently pull out data via the bytes method for reading.

We’ve synchronized access to this NSMutableData object via a pthread mutex:

pthread_mutex_t _mutex;

pthread_mutexattr_t attributes;
pthread_mutexattr_settype( &attributes, PTHREAD_MUTEX_DEFAULT );
pthread_mutex_init( &_mutex, &attributes );

and then every time we access this object we:

pthread_mutex_lock(&_mutex);
const UInt8* rawData = [_coverage bytes];
//code that works with the raw bytes
pthread_mutex_unlock(&_mutex);

Also, every addData method we have locks the mutex before adding data to the NSMutableData object.

The problem is we still get the occasional EXC_BAD_ACCESS while working with rawData. I understand that NSMutableBytes will grow its byte array as data gets added to it. I also understand that I shouldn’t expect rawData to magically grow also.

I’m just wondering how we can ever get into this situation where rawData has been free’d from underneath us when we have explicitly locked access for both read and write?

Are we doing something wrong with the mutex or the way we are accessing the bytes?

EDIT

I discovered the real reason why I was getting an EXC_BAD_ACCESS. I was not initializing the mutex attributes, so locking the mutex did nothing. Here is the corrected code:

pthread_mutex_t _mutex;

pthread_mutexattr_t attributes;
pthread_mutexattr_init(&attributes);
pthread_mutex_init(&_mutex, &attributes);
pthread_mutexattr_destroy(&attributes);
  • 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-25T17:06:44+00:00Added an answer on May 25, 2026 at 5:06 pm

    Yes it is possible that is being freed from underneath you.

    According to the documentation:

    bytes
    Returns a pointer to the receiver’s contents.

    You should make copies of the data to ensure that it will not be changed or freed from underneath you. When your done with your copy make sure to free() it.

    pthread_mutex_lock(&_mutex);
    const UInt8 *origData = [_coverage bytes];
    UInt8 *rawData;
    memmove(rawData, origData, [_coverage length]);
    
    //code that works with the raw bytes
    free(rawData);
    pthread_mutex_unlock(&_mutex);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have managed to NSInputStream and read some data to NSMutableData object. I am
I have an object that has an NSMutableArray of custom UIViews, a UIImage, and
i have the following method -(void)request { responseData = [[NSMutableData data] retain]; NSString *post
I have a data in NSMutableData type. Can I convert the data to a
I have an object that I alloc/init like normal just to get a instance.
I have a method that asynchronously downloads images. If the images are related to
I create an NSURLRequest and an NSURLConnection object and collect data in an NSMutableData
I have an object that I've written in objective c. It is very simple
I have a lllooonnnggg NSMutableData instance. I want to feed non-overlapping sub-ranges this data
I have a iPhone app that has a queue of pictures to upload to

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.