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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:54:12+00:00 2026-06-13T00:54:12+00:00

I have an Objective-C class whose instances can detect when they are no longer

  • 0

I have an Objective-C class whose instances can detect when they are no longer needed and destroy themselves, but I am looking for a safe way to trigger an object’s self-destruction from within the object itself, without somehow “destroying the method that is calling the destruction”… My code looks roughly like this (some removed for brevity):

+ (oneway void)destroyInstance:(id)obj {

    printf("Destroying.\n");
    if (obj != nil) {
        free(obj);
        obj = nil;
    }

}

- (oneway void)release {

    _noLongerRequired = [self determineIfNeeded]; // BOOL retVal

    if (_noLongerRequired) {
        [self deallocateMemory]; // Free ivars etc (NOT oneway)
        [MyClass destroyInstance:self]; // Oneway
    }
}

If I call -release, it should return instantly to the main thread (due to the oneway).

Meanwhile, if the instance finds it is no longer needed, it should then call the oneway class method destroyInstance: and remove itself from the runtime. My question is, is this safe?? And have I used oneway correctly? It seems to me there is the possibility of destroying the instance’s -release function before it returns, which could be… rather bad..?

(PS: Obviously not looking for anything to do with NSObject, etc :))

  • 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-13T00:54:13+00:00Added an answer on June 13, 2026 at 12:54 am

    This is without a doubt a terrible idea if you want working and maintainable software and unsafe in just about any context. But sometimes terrible, unsafe ideas can be fun on the weekend, so I’ll answer the components of the question I can discern.

    A method will not get “destroyed” because an instance is deallocated. What can happen is that self can end up pointing to deallocated memory during the execution of a method, which means that accessing self or any instance variables during this time can crash.

    As to the rest of your code, there is no reason at all to set obj equal to nil in +destroyInstance, so if you were trying accomplish something in particular (nil‘ing out pointers to the object perhaps) that way is not the right way to go about it.

    Thinking about the use of oneway, what the language says is that sending this message won’t block the calling thread. In the context of releasing objects I think it makes some sense, as presumably the target of the message won’t ever be referenced by that thread again. By that logic I’d think your declaration of +destroyInstance is maybe OK. I do wonder if you’d need to provide some sort of synchronization so that there’s no retain/release race conditions but thinking about it, taking ownership of an object should probably never be asynchronous.

    My personal opinion is that anyone who puts this code into production should probably be fired or sued =P. But if it’s only for educational purposes, have fun, and hope this helps.

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

Sidebar

Related Questions

I have the following objective C class. It is to store information on a
I have a peculiar problem. I created a normal Objective C class called SampleTable.
I'm new in objective-C and i wonder if its correct: I have a class
If subclass in objective-c wants to override a super class's method, does it have
I have an objective-C++ class which contain some honest C++ object pointers. When the
I have a brand new Objective-C class called test . The test class has
I want to have a compiler (pre-compiler) directive in my objective-C class to distinguish
I have an Objective-C class that implements NSCoding. I need to return a representation
I have an objective C class like, @interface message : NSObject { NSString *from;
I have an objective-c class with member variables. I am creating getters and setters

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.