Project

General

Profile

« Previous | Next » 

Revision e3a72781

Added by Leszek Koltunski over 5 years ago

Fixes for memory leak problems uncovered by the 'Rubik' app. (mainly: new method DistortedNode.markForDeletion)

View differences:

src/main/java/org/distorted/examples/rubik/RubikCube.java
29 29
import org.distorted.library.effect.MatrixEffectScale;
30 30
import org.distorted.library.effect.VertexEffectSink;
31 31
import org.distorted.library.main.DistortedEffects;
32
import org.distorted.library.main.DistortedNode;
32 33
import org.distorted.library.main.DistortedScreen;
33 34
import org.distorted.library.main.DistortedTexture;
34 35
import org.distorted.library.mesh.MeshCubes;
......
49 50
    private static final Static3D VectY = new Static3D(0,1,0);
50 51
    private static final Static3D VectZ = new Static3D(0,0,1);
51 52

  
53
    private DistortedNode[][][] mNodes;
52 54
    private MeshCubes[][][] mCubes;
53 55
    private DistortedEffects[][][] mEffects;
54 56
    private Static4D[][][] mQuatScramble;
......
75 77
      mRotAxis= RubikSurfaceView.VECTX;
76 78
      mTexture = new DistortedTexture(TEXTURE_SIZE,TEXTURE_SIZE);
77 79

  
80
      mNodes          = new DistortedNode[mSize][mSize][mSize];
78 81
      mCubes          = new MeshCubes[mSize][mSize][mSize];
79 82
      mEffects        = new DistortedEffects[mSize][mSize][mSize];
80 83
      mQuatScramble   = new Static4D[mSize][mSize][mSize];
......
160 163
            {
161 164
            if( x==0 || x==mSize-1 || y==0 || y==mSize-1 || z==0 || z==mSize-1 )
162 165
              {
163
              screen.attach(mTexture,mEffects[x][y][z],mCubes[x][y][z]);
166
              mNodes[x][y][z] = new DistortedNode(mTexture,mEffects[x][y][z],mCubes[x][y][z]);
167
              screen.attach(mNodes[x][y][z]);
168
              }
169
            }
170
      }
171

  
172
///////////////////////////////////////////////////////////////////////////////////////////////////
173
// all DistortedTextures, DistortedNodes, DistortedFramebuffers, DistortedScreens and all types of
174
// Meshes HAVE TO be markedForDeletion when they are no longer needed- otherwise we have a major
175
// memory leak.
176

  
177
    void releaseResources()
178
      {
179
      mTexture.markForDeletion();
180

  
181
      for(int x=0; x<mSize; x++)
182
        for(int y=0; y<mSize; y++)
183
          for(int z=0; z<mSize; z++)
184
            {
185
            if( x==0 || x==mSize-1 || y==0 || y==mSize-1 || z==0 || z==mSize-1 )
186
              {
187
              mCubes[x][y][z].markForDeletion();
188
              mNodes[x][y][z].markForDeletion();
164 189
              }
165 190
            }
166 191
      }
......
217 242

  
218 243
///////////////////////////////////////////////////////////////////////////////////////////////////
219 244

  
220
    void finishRotationCalledOnNextRender(EffectListener listener)
245
    void finishRotationNow(EffectListener listener)
221 246
      {
222 247
      boolean first = true;
223 248
      float startingAngle = mRotationAngleStatic.get1();
......
249 274

  
250 275
///////////////////////////////////////////////////////////////////////////////////////////////////
251 276

  
252
    void removeRotationCalledOnNextRender(EffectListener listener)
277
    void removeRotationNow(EffectListener listener)
253 278
      {
254 279
      mEffectsListeningForNow.deregisterForMessages(listener);
255 280

  
src/main/java/org/distorted/examples/rubik/RubikRenderer.java
45 45
    private Static4D mQuatCurrent, mQuatAccumulated;
46 46
    private Static4D mTempCurrent, mTempAccumulated;
47 47
    private float mCubeSizeInScreenSpace;
48
    private int mNextCubeSize;
48 49
    private boolean mFinishRotation, mRemoveRotation, mFinishDragCurrent, mFinishDragAccumulated;
49 50
    private boolean mCanRotate;
50 51
    private RubikCube mCube;
......
74 75
      mFinishDragCurrent     = false;
75 76
      mFinishDragAccumulated = false;
76 77

  
78
      mNextCubeSize= 0;
79

  
77 80
      mCanRotate = true;
78 81
      }
79 82

  
......
101 104
        {
102 105
        mCanRotate = false;
103 106
        mFinishRotation=false;
104
        mCube.finishRotationCalledOnNextRender(this);
107
        mCube.finishRotationNow(this);
105 108
        }
106 109

  
107 110
      if( mRemoveRotation )
108 111
        {
109 112
        mRemoveRotation=false;
110
        mCube.removeRotationCalledOnNextRender(this);
113
        mCube.removeRotationNow(this);
111 114
        mCanRotate = true;
112 115
        }
116

  
117
      if( mNextCubeSize!=0 )
118
        {
119
        createCubeNow(mNextCubeSize);
120
        mScreen.detachAll();
121
        mCube.attachToScreen(mScreen);
122
        mNextCubeSize = 0;
123
        }
113 124
      }
114 125

  
115 126
///////////////////////////////////////////////////////////////////////////////////////////////////
......
180 191
///////////////////////////////////////////////////////////////////////////////////////////////////
181 192

  
182 193
   void createCube(int newSize)
194
     {
195
     mNextCubeSize = newSize;
196
     }
197

  
198
///////////////////////////////////////////////////////////////////////////////////////////////////
199

  
200
   void createCubeNow(int newSize)
183 201
     {
184 202
     int oldSize = mCube==null ? 0 : mCube.getSize();
185 203

  
186 204
     if( oldSize!=newSize )
187 205
       {
206
       if( mCube!=null ) mCube.releaseResources();
188 207
       mCube = new RubikCube(newSize, mMove, mScale, mQuatCurrent, mQuatAccumulated);
189 208
       mCube.createTexture();
190 209

  
......
192 211
         {
193 212
         recomputeScaleFactor(mScreenWidth,mScreenHeight);
194 213
         }
195

  
196
       mScreen.detachAll();
197
       mCube.attachToScreen(mScreen);
198 214
       }
199 215
     }
200 216

  
201 217
///////////////////////////////////////////////////////////////////////////////////////////////////
202 218

  
203
   void recomputeScaleFactor(int screenWidth, int screenHeight)
219
   private void recomputeScaleFactor(int screenWidth, int screenHeight)
204 220
     {
205 221
     mCubeSizeInScreenSpace = CUBE_SCREEN_RATIO*(screenWidth>screenHeight ? screenHeight:screenWidth);
206 222
     float texSize = mCube.getTextureSize();
src/main/java/org/distorted/examples/rubik/RubikSurfaceView.java
80 80
        mScreenWidth = mScreenHeight = mScreenMin = 0;
81 81

  
82 82
        mRenderer = new RubikRenderer(this);
83
        mRenderer.createCube(RubikActivity.DEFAULT_SIZE);
83
        mRenderer.createCubeNow(RubikActivity.DEFAULT_SIZE);
84 84

  
85 85
        mQuatCurrent     = new Static4D(0,0,0,1);
86 86
        mQuatAccumulated = mRenderer.initializeQuat();

Also available in: Unified diff