• Stars
    star
    361
  • Rank 117,957 (Top 3 %)
  • Language
    Objective-C
  • License
    Other
  • Created over 14 years ago
  • Updated about 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Zeroing weak references for retain/release Objective-C

MAZeroingWeakRef - by Mike Ash - [email protected]

Introduction

MAZeroingWeakRef is a library for using zeroing weak references in retain/release Cocoa and Cocoa Touch code. These are references which do not keep an object alive, and which automatically become nil once the object is destroyed.

MAZeroingWeakRef does not work under Cocoa garbage collection. The built-in __weak specifier exists for that, although it is used somewhat differently.

The API is simple and mostly self-explanatory from the header file. Note that cleanup blocks are only needed for advanced uses when you need to take more action than simply zeroing the reference when the target object is destroyed. Be sure to heed the warning in the header about not doing too much work in the cleanup block.

In order to allow weak references to CoreFoundation bridged objects, a fair amount of crazy hacking of private APIs had to be done. For people who don't like that sort of thing, or who are worried about rejection when building for iOS, this crazy hacking can be reduced or disabled altogether. Look at the COREFOUNDATION_HACK_LEVEL macro in MAZeroingWeakRef.m, and the comment above it which explains what the different levels do.

A similar KVO_HACK_LEVEL macro is also available.

App Store

For iOS work, or Mac App Store work, you will probably want to set both COREFOUNDATION_HACK_LEVEL and KVO_HACK_LEVEL to 0. In this mode, MAZeroingWeakRef uses no private APIs.

Also, if you need your app to run on iOS 3.x you need to disable blocks based code setting USE_BLOCKS_BASED_LOCKING to 0.

Source Code

The MAZeroingWeakRef code is available from GitHub:

http://github.com/mikeash/MAZeroingWeakRef

MAZeroingWeakRef is made available under a BSD license.

More Information

For more information about it, this blog post gives a basic introduction to the API:

http://mikeash.com/pyblog/introducing-mazeroingweakref.html

This describes how it works in most cases:

http://mikeash.com/pyblog/friday-qa-2010-07-16-zeroing-weak-references-in-objective-c.html

And this describes some of the more hairy parts:

http://mikeash.com/pyblog/friday-qa-2010-07-30-zeroing-weak-references-to-corefoundation-objects.html

Enjoy!

More Repositories

1

MAObjCRuntime

ObjC wrapper for ObjC runtime API
Objective-C
1,534
star
2

MAKVONotificationCenter

Better key-value observing for Cocoa
Objective-C
528
star
3

MABlockClosure

ObjC block -> C function pointer using libffi
Objective-C
359
star
4

Circle

Objective-C
313
star
5

MACollectionUtilities

Smalltalk-inspired collection utility methods and macros for Cocoa
Objective-C
250
star
6

BinaryCoder

An example implementation of Swift.Codable using a simple binary format
Swift
168
star
7

memorydumper2

Swift
161
star
8

SwiftObserverSet

NSNotificationCenter re-conceptualization for Swift
Swift
132
star
9

MADispatchQueue

A spiritual reimplementation of the basics of dispatch_queue, for educational purposes
Objective-C
128
star
10

memorydumper

Swift
106
star
11

MAMirroredQueue

Objective-C
98
star
12

MAFuture

Proxying futures library for Objective-C
Objective-C
89
star
13

MAAsyncIO

GCD-based async IO
Objective-C
83
star
14

MAObject

Reimplementation of most NSObject functionality, for educational purposes
Objective-C
83
star
15

TSUD

Type-Safe User Defaults - Swifty NSUserDefaults wrapper
Swift
77
star
16

MABGTimer

Background timers, not associated with any thread, for Objective-C
Objective-C
71
star
17

iphone-user-performance-tests

A utility for doing simple tests of an iOS device's user-facing performance, and some collected results.
Swift
51
star
18

MAGenerator

Blocks/macros-based library for building Python-like generators in Objective-C
Objective-C
48
star
19

MABlockForwarding

Generic block forwarding, like forwardInvocation:
Objective-C
47
star
20

mikeash.com-svn

Legacy repository converted over from mikeash.com/svn/
Objective-C
43
star
21

PerformanceTest

Simple performance testing for various common operations in Mac/iOS programs
Objective-C++
40
star
22

DemoXcodePlugin

Objective-C
40
star
23

NSRectangle

Objective-C
39
star
24

XCommentWrap

Xcode extension for hard wrapping comments.
Swift
38
star
25

MANotificationCenter

NSNotificationCenter workalike, for education purposes
Objective-C
36
star
26

ChemicalBurn

Objective-C
35
star
27

MAInvocation

Reimplementation of NSInvocation, minus floating-point support, for educational purposes
Objective-C
35
star
28

MACollections

Reimplementation of some Cocoa collections, for educational purposes
Objective-C
35
star
29

GPULife

GPU-accelerated Game of Life screensaver
Objective-C
30
star
30

MAPlistTypeChecking

Conveniences for type-checking and reporting errors in plists, JSON, and other similar structures
Objective-C
26
star
31

MAAutoreleasePool

NSAutoreleasePool workalike for educational purposes
Objective-C
23
star
32

MASON

Friendly JSON decoding API for Swift
Swift
20
star
33

StringWithFormat

Reimplementation of stringWithFormat: for educational purposes
Objective-C
18
star
34

MADispatchGroup

Reimplementation of dispatch_group, for educational purposes.
Objective-C
12
star
35

MAParanoidAllocator

Objective-C
12
star
36

refcounting

Implementation of ObjC refcounting, for educational purposes
Objective-C
10
star
37

Smurf

A Swift library which renames Any to Smurf
Swift
9
star
38

sitescan

Python
5
star
39

Turmites

My Turmites screensaver, from the distant past
Objective-C
5
star
40

compressor

Simple Arduino audio compressor code
Arduino
4
star
41

creatures

My extremely, extremely old artificial life program
Objective-C
4
star
42

Centrifugal

Objective-C
2
star
43

BlockContextExecution

BlockContextExecution
Objective-C
2
star
44

pyunitcalc

Python
1
star