Objective-C Memory management Memory management is semi-automatic: The programmer must allocate memory for objects either a) explicitly (alloc) or b) indirectly.

Slides:



Advertisements
Similar presentations
Objective-C Lecture 2 Memory management Memory management in OC is semi- automatic: The programmer must allocate memory for objects either a) explicitly.
Advertisements

1 Objective-C and Object-Oriented Programming. 2 Introduction Objective-C is implemented as set of extensions to the C language. It's designed to give.
Lecture 3 - Writing Initializers A correct initializer must comply with the following: The initializer name should begin with init The initializer returns.
Operating Systems Lecture 7.
Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Introduction to Memory Management. 2 General Structure of Run-Time Memory.
(Chapter 5) Deleting Objects
Chapter 6 Data Types
CSE 332: C++ memory management idioms C++ Memory Management Idioms Idioms are reusable design techniques in a language –We’ll look at 4 important ones.
CS 3800 Su 11 Beg. IOS Dev L4 Obj C part 1 CS 3800 Introduction to IOS programming Summer 2011 G. Zimmerman.
1 Objects and ClassesStefan Kluth 1.6Objects and Classes 1.6What is an Object? 1.7Objects and Classes 1.8Object Interface, Class Inheritance, Polymorphism.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 18.
CPSC 388 – Compiler Design and Construction
IPhone Development Crash Course By Dylan Harris
Lifetime “The lifetime of a variable is the time during which the variable is bound to a specific memory location.” [p. 219] “…the lifetime of a variable.
IPhone 101. Outline Objective-C Random bits of the API Using the simulator Debugging with Xcode.
Classes Separating interface from implementation
Intro to Objective-C Syntax: What’s most confusing about Objective-C? Most class names start with NS: NSString, NSObject Parameter lists are not comma.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Objective-C Quinton Palet. Introduction  Objective-C was developed to bring large scale software development the power of the object-oriented approach.
Review of C++ Programming Part II Sheng-Fang Huang.
Ch 4. Memory Management Timothy Budd Oregon State University.
Memory Management and Automatic Reference Counting/ Copying Objects Archiving Copyright © 2012 by Yong-Gu Lee
Advanced Inheritance Concepts. In this chapter, we will cover: Creating and using abstract classes Using dynamic method binding Creating arrays of subclass.
CSSE501 Object-Oriented Development. Chapter 5: Messages, Instances and Initialization  This chapter examines OOP run-time features: Object Creation.
Object Oriented Programming
EE4E. C++ Programming Lecture 1 From C to C++. Contents Introduction Introduction Variables Variables Pointers and references Pointers and references.
CS212: Object Oriented Analysis and Design Lecture 12: Operator Overloading-II.
CSSE501 Object-Oriented Development. Chapter 12: Implications of Substitution  In this chapter we will investigate some of the implications of the principle.
Objective-C First Part adapted from a presentation by Kevin Layer.
Objective-C OOP Spring OOP Conceptually the same as C++, Java, and all other object oriented languages The syntax, however… …is, well, different.
CS212: Object Oriented Analysis and Design Lecture 7: Arrays, Pointers and Dynamic Memory Allocation.
Storage Management. The stack and the heap Dynamic storage allocation refers to allocating space for variables at run time Most modern languages support.
1 Introduction to Objective-C Programming (Level: Beginner) Lecture 1.
Objective C. Основан на C Объектно-ориентированный Использует сообщения Динамический Протоколы Интроспекция.
Memory Management Issues, Solutions, and Examples.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
Dynamic Memory Allocation. Domain A subset of the total domain name space. A domain represents a level of the hierarchy in the Domain Name Space, and.
User Defined Functions Chapter 7 2 Chapter Topics Void Functions Without Parameters Void Functions With Parameters Reference Parameters Value and Reference.
Object Oriented Programming Elhanan Borenstein Lecture #3 copyrights © Elhanan Borenstein.
CSCI-383 Object-Oriented Programming & Design Lecture 18.
UFS003C3 Lecture 15 Data type in C & C++ Using the STL.
Object-Based Programming Mostly Review. Objects Review what is object? class? member variables? member functions? public members? private members? friend.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
More Distributed Garbage Collection DC4 Reference Listing Distributed Mark and Sweep Tracing in Groups.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
The iOS Platform and SDK. iOS iPhoneiPad Mini iPad.
Lec 5 Obj-C part 2 CS 3800 Introduction to IOS programming Lecture 5 Summer 2011.
More about Java Classes Writing your own Java Classes More about constructors and creating objects.
2/20/2016 EEC492/693/793 - iPhone Application Development 12/20/2016 EEC492/693/793 - iPhone Application Development 1 EEC-492/693/793 iPhone Application.
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
Objective-C: Intro Michelle Alexander COMS E6998 2/4/2013.
GARBAGE COLLECTION Student: Jack Chang. Introduction Manual memory management Memory bugs Automatic memory management We know... A program can only use.
Jim Fawcett CSE 691 – Software Modeling and Analysis Fall 2000
Constructors and Destructors
Module 9: Memory and Resource Management
Akinori Ito, *Kengo Watanabe Genki Kuroda, Ken’ichiro Ito
Storage Management.
Dynamically Allocated Memory
Storage.
EEC-492/693/793 iPhone Application Development
Constructors and Destructors
Dynamic Memory Management
Resource Allocation and Ownership
Advanced Inheritance Concepts
Class: Special Topics 2 For classes using memory allocation
The Constructors Lecture 7 Fri, Feb 2, 2007.
Dynamic Memory Management
SPL – PS3 C++ Classes.
Presentation transcript:

Objective-C Memory management Memory management is semi-automatic: The programmer must allocate memory for objects either a) explicitly (alloc) or b) indirectly using a convenience constructor No need to de-allocate

Allocation Allocation happens through the class method alloc. The message ‘alloc’ is sent to the class of the requested object. Alloc is inherited from NSObject. Every alloc creates a new instance (=object) [HelloWorld alloc]; The class creates the object with all zeros in it and returns a pointer to it. HelloWorld *p = [HelloWorld alloc]; The pointer p now points to the new instance. Now we send messages to the instance through p.

The reference counter Every instance has a reference counter. It counts how many references are retaining the object. The counter is 1 after allocation. It does not count how many references exist to the object Sending the retain message to the object increases the reference counter by 1. Sending the release message decreases the reference counter by 1. No need to do either if using ARC.

reference counter = retain counter When the reference counter reaches zero, the object is automatically de-allocated. The programmer does not de-allocate. The programmer only does: alloc retain release

Rules for memory management With no ARC: The method that does an alloc or a retain must also do a release, it must maintain the balance between: (alloc or retain) and (release) If a method does alloc and returns a pointer to the created object then the method must do an autorelease instead of release.

Autorelease pool With no ARC: For outorelease to work the programmer must create an autorelease pool, using: NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; When Cocoa is used then the autorelease pool is created automatically – the programmer does not need to do it. To release the pool and all objects in it, do: [pool release];

Convenience Constructors This is a class method that allocates and initializes an object. The programmer is neither doing alloc nor init. Example: +(id)studentWithName :(NSString*)name AndGpa:(float)gpa { id newInst = [[self alloc]initStudent:name :gpa]; return [newInst autorelease]; }

Convenience Constructors Essential: the method sends alloc to self which is the Student class object Essential: the method autoreleases the instance, because it returns a pointer to the created instance Not essential: This example uses an existing initializer, it could use something else or initialize the Student data directly

Convenience Constructors Calling the convenience constructor: id stud = [Student AndGpa: 3.8]; The message is sent to the Student class object and returns a pointer to it, the pointer is assigned to stud The calling code does neither alloc nor init An autorelease pool must be in place

End of Memory With newer versions of Xcode, the memory management is becoming more and more automatic. Use Automatic Reference Counting ARC to take advantage of the latest changes and improvements with memory management!