| [](https://github.com/dart-lang/native/actions/workflows/objective_c.yaml) | |
| [](https://app.codecov.io/gh/dart-lang/native) | |
| [](https://pub.dev/packages/objective_c) | |
| [](https://pub.dev/packages/objective_c/publisher) | |
| # objective_c | |
| A library to access Objective C from Flutter that acts as a support library for | |
| package:ffigen. | |
| This library isn't very useful on its own. Typically it is used while | |
| interoperating with an Objective C library created by package:ffigen. See | |
| https://pub.dev/packages/ffigen | |
| For general information about interop with Objective C, see | |
| https://dart.dev/guides/libraries/objective-c-interop | |
| You can find more documentation about Objective-C interop using FFIgen in | |
| the TOPICS section in [FFIgen's API documentation](https://pub.dev/documentation/ffigen/latest/). | |
| ## Memory management | |
| Objective C and Dart use different styles of memory management. Dart is garbage | |
| collected, while Objective C uses reference counting. This difference is a | |
| common cause of bugs when interoperating between the two languages. | |
| Memory management issues typically manifest as seg faults, which can be hard to | |
| debug as they often have no useful stack trace. The most common issue is use | |
| after free errors, where a Dart object is constructed to wrap an Objective C | |
| object that has already been destroyed. To debug these issues, try running your | |
| program with [asserts enabled](https://dart.dev/language/error-handling#assert). | |
| When the Dart wrapper object is created or destroyed, an assert checks that the | |
| Objective C object still exists, which should provide a much more useful stack | |
| trace. | |
| References are automatically retained when the Dart wrapper object is | |
| created, and released when the Dart object is garbage collected. But to fix a | |
| reference counting issue, it may occasionally be necessary to manually control | |
| the ref count. The ref count of the Objective C object can be controlled from | |
| Dart by the `retain` and `release` options in the Dart wrapper object | |
| constructors, and by the `retainAndReturnPointer()` and `release()` methods. | |
| For more details on avoiding reference cycles and leaks, see the | |
| [Objective-C Memory Management topic](https://pub.dev/documentation/ffigen/latest/topics/Objective-C%20Memory%20Management-topic.html). |