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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:29:53+00:00 2026-05-25T20:29:53+00:00

I want to use a struct pointer as a @property but I’m not sure

  • 0

I want to use a struct pointer as a @property but I’m not sure how should I free it.

This is what I have now:

.h:

@property (nonatomic, assign) InfoStruct * info;

.m:

@synthesize info; 
- (id)init {
    self = [super init];
    if (self) {
        self.info = (struct InfoStruct *) malloc(sizeof(struct InfoStruct));
    }
    return self;
}

-(void)dealloc {
    free(info);
    [super dealloc];
}

Could the code above cause any trouble? Is it correct? Seems to work fine, but I think I need a reassurance.

  • 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-25T20:29:53+00:00Added an answer on May 25, 2026 at 8:29 pm

    It depends on how you use instances of that class. For example:

    SomeClass *obj = [SomeClass new];
    // never assign an address to obj.info
    [obj release];
    

    will be okay.

    However, since you’ve declared that property to be read-write, you need to take into account the ownership of the object pointed by that structure.

    For example, in:

    SomeClass *obj = [SomeClass new];
    obj.info = someInfo;
    [obj release];
    

    the object you’ve allocated in -init doesn’t get freed (you’re leaking it), and the object pointed to by someInfo gets freed. You could change the property setter to something like:

    - (void)setInfo:(InfoStruct *)newInfo {
        if (newInfo != info) {
            free(info); // free previous object
            info = newInfo;
        }
    }
    

    in which case an assignment will always free the previous object unless the value being assigned is the same as the previous value. This means that your class effectively owns the object whose address is being assigned to the property.

    If you decide that the class shouldn’t be responsible for freeing objects other than the one created in -init, things get a tad more complicated. If info is assigned some address other than the address of the original info created in -init, the client code needs to free obj.info before assigning it a new value. For example:

    SomeClass *obj = [SomeClass new];
    free(obj.info);
    obj.info = someInfo;
    [obj release];
    

    That’s problematic because you have code outside of the class deallocating memory used by an object of that class, which is quite intrusive. And, if the class isn’t responsible for freeing objects other than the one created in -init, you need to keep additional state indicating that so that -dealloc only frees info if info wasn’t changed after the object was initialised.

    Also, as Jim noted, the correct method name is dealloc.

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

Sidebar

Related Questions

So I have this C .dll source code which I want to use in
I'm not sure how to explain this but this piece of code bellow can
I want to use some basic struct in C like the following: struct p
i want use some data from a website with web service. i have a
I have a transaction log file in CSV format that I want use to
I am trying to use a double void pointer but I am a little
I have a C structure that looks like this typedef struct event_queue{ Event* event;
If I want to use a pointer to a class and I dont do
I want to convert function object to function. I wrote this code, but it
I have a template looking like this: struct add_node_value_visitor : boost::static_visitor<> { add_node_value_visitor(){} template

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.