Last active
November 29, 2017 01:56
-
-
Save giraphics/60bfddf6a88dd5a551350fb22ae4f6b3 to your computer and use it in GitHub Desktop.
Explicit deallocation model
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This example shows a demo as per new deallocation model suggestion. | |
// Here, the derive class explicitly deallocates | |
// a. owned resources. | |
// b. based class resources | |
#include <iostream> | |
using namespace std; | |
class Position // GPU allocator/deallocator for position buffers | |
{ | |
public: | |
Position() { cout << "Position Ctor" << endl; Allocate(); } | |
virtual ~Position() { cout << "Position Dtor" << endl; Deallocate(); } | |
void Allocate() { cout << "Position Allocated" << endl; } | |
void Deallocate() { cout << "Position Deallocated" << endl; } | |
}; | |
class TextureCoord // GPU allocator/deallocator for texture coordinates buffers | |
{ | |
public: | |
TextureCoord() { cout << "TextureCoord Ctor" << endl; Allocate(); } | |
virtual ~TextureCoord() { cout << "TextureCoord Dtor" << endl; Deallocate(); } | |
void Allocate() { cout << "TextureCoord Allocated" << endl; } | |
void Deallocate() { cout << "TextureCoord Deallocated" << endl; } | |
}; | |
class Normals // GPU allocator/deallocator for normal buffers | |
{ | |
public: | |
Normals() { cout << "Normals Ctor" << endl; Allocate(); } | |
virtual ~Normals() { cout << "Normals Dtor" << endl; Deallocate(); } | |
void Allocate() { cout << "Normals Allocated" << endl; } | |
void Deallocate() { cout << "Normals Deallocated" << endl; } | |
}; | |
class Shape | |
{ | |
public: | |
Shape() { cout << "Shape Ctor" << endl; } | |
virtual ~Shape() { cout << "Shape Dtor" << endl; } | |
}; | |
class Rectangle : public Shape, public Position | |
{ | |
public: | |
Rectangle() { cout << "Rectangle Ctor" << endl; Allocate(); } | |
virtual ~Rectangle() { cout << "Rectangle Dtor" << endl; Deallocate(); } | |
void Allocate() { cout << "Rectangle Allocated" << endl; } | |
void Deallocate() | |
{ | |
Position::Deallocate(); | |
cout << "Rectangle Deallocated" << endl; | |
} | |
}; | |
// Circle produces with 4 vertices from Rectangle and Texture coords | |
class Circle : public Rectangle, public TextureCoord | |
{ | |
public: | |
Circle() { cout << "Circle Ctor" << endl; Allocate(); } | |
virtual ~Circle() { cout << "Circle Dtor" << endl; Deallocate(); } | |
void Allocate() { cout << "Circle Allocated" << endl; } | |
void Deallocate() | |
{ | |
cout << "Circle Deallocated" << endl; | |
Rectangle::Deallocate(); | |
TextureCoord::Deallocate(); | |
} | |
}; | |
// Circle with light shading | |
class ShadedCircle : public Circle, public Normals | |
{ | |
public: | |
ShadedCircle() { cout << "ShadedCircle Ctor" << endl; Allocate(); } | |
virtual ~ShadedCircle() { cout << "ShadedCircle Dtor" << endl; Deallocate(); } | |
void Allocate() { cout << "ShadedCircle Allocated" << endl; } | |
void Deallocate() | |
{ | |
cout << "ShadedCircle Deallocated" << endl; | |
Circle::Deallocate(); | |
Normals::Deallocate(); | |
} | |
}; | |
int main() | |
{ | |
ShadedCircle disk; | |
} | |
// 1. Causes double deletion as the virtaul destructor is implicitly calling the Deallocate() | |
// This can be fixed by removing the Deallocate() from base class destructor. But what if the Base class object | |
// is created outside? This causes memory leak on CPU and GPU. How to fix it? | |
// 2. As the inheritance changes, the destructor needs to carefully altered explicitly over the time. More susceptible to | |
// memory leaks and double deletion. | |
// 3. Unlike, OpenCL wrapper class where GPU allocation are reference counted, the GPU allocation in OpenGL happens behind the curtains. | |
// Each allocated memory is driven by handles which has no relation with the Parent object or the Dialog memory underwhich | |
// they are allocated. Therefore, the CPU memory is still safer to be guaranted to release at some point of time. | |
// But the allocated GPU memory looks at risk and leaked. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment