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 7826541
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T09:27:36+00:00 2026-06-02T09:27:36+00:00

I am using dataWithBytesNoCopy:length: method for creating NSData object from bytes . -(NSData *)dataFromBytes

  • 0

I am using dataWithBytesNoCopy:length: method for creating NSData object from bytes.

-(NSData *)dataFromBytes
{ 
...
   void *data = malloc(bytes);
... 
    return [NSData dataWithBytesNoCopy:data length:bytes];;
}

From apple documentation:

The returned object takes ownership of the bytes pointer and frees it
on deallocation.

should i release return data object in caller function?

-(void)testFunction
{
NSAutoreleasePool *nsp = [[NSAutoreleasePool alloc] init];

NSData *ldata = [mObject dataFromBytes];
NSlog(@"%@", ldata)//printing data
[nsp release];
NSlog(@"%@", ldata)//Not printing data //sometimes getting exc_bad_access

}
  • 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-06-02T09:27:37+00:00Added an answer on June 2, 2026 at 9:27 am

    Short answer: No, the caller should only release the returned data if it has retained it. In your testFunction you haven’t retained it so you shouldn’t release it.

    Longer answer: Firstly, the dataFromBytes method in your above code doesn’t have a return value, presumably this should actually return NSData *.

    Anyway, the dataWithBytesNoCopy:length: method returns an autoreleased NSData instance which is immediately being returned from the method. We know that it is autoreleased because the method name doesn’t contain init, new or copy. This means the caller is NOT automatically taking ownership of the returned data so it shouldn’t release it (unless it retains it).

    Ownership of the data that has been malloc’ed is taken over by the NSData object and shouldn’t be freed. When the NSData instance is dealloc’ed it will free the memory that was malloc’ed.

    In your, testFunction you are receiving the NSData object and printing it. Next you release the autorelease pool which will cause the autoreleased object to be released and dealloc’ed. You print it again but it has already been dealloc’ed which is dangerous (I’m surprised it doesn’t always EXC_BAD_ACCESS.

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

Sidebar

Related Questions

Using PHP, I can convert MySQL data or static table data to csv, Excel,
Using the javascript function function squareIt(number) { return number * number; } When given
Using the navigator.geolocation object in JavaScript. Trying to establish accurate ranges, but wondering exactly
Using mercurial, I've run into an odd problem where a line from one committer
Using Android TelephonyManager an application can obtain the state of data activity over the
Using php/html, I want to retrieve email addresses (plus other information) from MySQL and
Using Core Data, I have a fetch request to fetch the minimum of a
(Using MVC 2) From inside my controller action, I need to display the url:
I have a file that was encrypted using AES. I use the following NSData
I'm using the following code to encrypt files in cocoa: - (NSData *)AES256EncryptWithKey:(NSString *)key

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.