AlexLecture_ReferenceCounting

AlexLecture_ReferenceCounting - 1 Reference Counting Mixin...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 Reference Counting Mixin classes In object-oriented programming languages, a mixin is a class that provides a certain functionality to be inherited by a subclass, while not meant for instantiation (the generating of objects of that class). Inheriting from a mixin is not a form of specialization but is rather a means of collecting functionality. A class may inherit most or all of its functionality from one or more mixins through multiple inheritance. 2 So-called because designed to encapsulate orthogonal functionality that may be mixed into classes in an existing hierarchy Mixin classes are always abstract Motivation Suppose we define a function: Expr* mkExpr(void) { Expr* l1 = new Literal(2); Expr* v = new Variable(x); Expr* t1 = new Add(l1,v); Expr* l2 = new Literal(2); 3 Expr* t2 = new Multiply(l2,t1); } t2 l2 t1 l1 v Problem Suppose a client program executes the code: ... Expr* e = mkExpr(); e->print(cout); delete e; ... Wh t h ? 4 What happens? How could we ensure that when a composite object is deleted, its parts are also deleted? 2 Solution class Expr { ... virtual ~Expr() {} }; class BinaryExpr : public Expr { 5 class BinaryExpr : public Expr { ... ~BinaryExpr() { delete(leftOperand); delete(rightOperand); } ... }; Or is it? Often desirable for two or more composite objects to share the same component Example: Expr* l = new Literal(2); Expr* v = new Variable(x); Expr* t1 = new Add(l,v); Expr* t2 = new Multiply(l,t1); t2 t1 6 Here, object l is the child of two composite objects, t1 and t2 . Will our memory-reclamation scheme work now? l v class BinaryExpr : public Expr { ... ~BinaryExpr() { delete(leftOperand); delete(rightOperand); } ... }; Deleting composites that share parts Not safe for destructor of a composite class to delete its children if these objects might be children of multiple composite objects Solution: design a protocol that tracks references to objects and that deletes an object only when its reference count reaches 0. 7 Clients never call delete Objects call delete on themselves! Clients must explicitly notify objects when references to these objects are assigned or overwritten Abstract ref-counting class class RCObject { public: void addRef() { ++refCount; } void removeRef() { if(--refCount <= 0) delete this; } protected: 8 RCObject() : refCount(0) {} virtual ~RCObject() {} private: int refCount; }; 3 Example Modify class Expr as follows: class Expr : public RCObject { ... }; 9 Subsequently, every class that derives from Expr will inherit reference counting infra- structure Example Modify BinaryExpr as follows: class BinaryExpr : public Expr { ... BinaryExpr( Expr* l, Expr* r ) : leftOperand(l), rightOperand(r) { l->addRef(); r->addRef(); } 10 ......
View Full Document

Page1 / 14

AlexLecture_ReferenceCounting - 1 Reference Counting Mixin...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online