Skip to content

[MNT]: macOS backend should always pair +alloc/-init calls #31798

@iccir

Description

@iccir

Summary

I mentioned the following in #21788:

In Objective-C, an -init… method should always be called immediately after +alloc. Calling any other instance method on an object you just allocated before calling -init… is undefined behavior.123

I believe that each tp_new callback should only call tp_alloc and not instantiate an uninitialized Objective-C object. Each tp_init callback should then create a fully initialized Objective-C object via an +alloc/-init… pair.

I'm starting work on #31797 and the use of unpaired +alloc/-init… calls is generating a few warnings. I'd like to clean this up at the same time I do the ARC work.

Proposed fix

No response

Footnotes

  1. "But the resulting object is not yet usable." from Object Allocation

  2. "You must use an init... method to complete the initialization process." from +alloc

  3. "immediately" and "An init message is coupled with an alloc … message in the same line of code" from -init

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions