preface

We often use alloc and init methods in our daily development. So what exactly do these two methods do? What’s the difference? Next, let’s explore further!

Let’s look at an example

It can be seen from the printed log:

Obj and obj1 have the same contents and point to the same memory address, but the memory address of the pointer is different.

Obj2 differs from obj, the contents of obj1, the memory address to which it points, and the memory address of the pointer.

It is possible to conclude that the alloc method opens up memory. The init method doesn’t seem to be doing anything. So is this really the case? Next, explore the underlying implementation of Alloc.

How do I find out which system class library the method to be analyzed is under

OC is not open source in Xcode as you all know, but Apple does open source some libraries for you. You can use this link to download the source code. Opensource.apple.com/tarballs/ how specific analysis, positioning alloc method is in which libraries implemented? There are three ways to do this.

Method 1

Break to the line of code you want to locate. Then hit a symbolic breakpoint to capture the alloc method. Execute down after the symbolic breakpoint. The invoked system library is found.

Method 2

The second breaks to the line of code you want to locate. Then hold down the Control key and step into assembly. Then the called method is found and a symbolic breakpoint is made to the found method. Execute down after the symbolic breakpoint. The invoked system library is found.

Methods 3

The third type first breaks to the line of code to be located. Then go to Debug->Debug Workflow->Always Show Disassembly in the menu bar. Hold down the Control key and step into the method to be called. Then use the symbol breakpoint method to find the system library.

How to use source code analysis

Found libraries to explore and analyze. After downloading, you cannot compile and debug directly. You can refer to some great articles to configure, you can also go to GitHub to download the source code can be compiled and debugged. Github.com/LGCooci/obj…

Alloc source code exploration

Open source project, wow, so complicated. Don’t panic. Search alloc {globally for location.

The _objc_rootAlloc method is called.

Notice that callAlloc is called, and continue.

It’s a little bit more complicated here, but that’s okay. Since we are running the source code, just follow the interrupt point and see which method we enter. The _objc_rootAllocWithZone method is entered.

There’s nothing to say keep going. This is where you get to the core of the code.

Break your own breakpoints, step by step in the console output variables, you will probably see each line of code in action. This method does three main things: CLS ->instanceSize(extraBytes); Calculate memory space (id)calloc(1, size); Open up memory and return pointer obj->initInstanceIsa(CLS, hasCxxDtor); Initialize the pointer and associate it with the class

conclusion

The alloc method is basically used to open up memory and associate Pointers with classes. The init method does nothing to open up memory. It acts as a constructor, an interface. Allows developers to initialize objects they want.

`