IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 14903


Ignore:
Timestamp:
Sep 20, 2007, 9:14:16 AM (19 years ago)
Author:
magnier
Message:

drop psImageFreeChildren : not a sensible function; fix memory management of parents and children -- inc ref counter on pointer from child to parent

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/eam_branch_20070830/psLib/src/mathtypes/psImage.c

    r12813 r14903  
    99 *  @author Ross Harman, MHPCC
    1010 *
    11  *  @version $Revision: 1.129 $ $Name: not supported by cvs2svn $
    12  *  @date $Date: 2007-04-12 18:52:57 $
     11 *  @version $Revision: 1.129.8.1 $ $Name: not supported by cvs2svn $
     12 *  @date $Date: 2007-09-20 19:14:16 $
    1313 *
    1414 *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
     
    2828#include "psError.h"
    2929#include "psAssert.h"
     30#include "psAbort.h"
     31
    3032#include "psImage.h"
    3133#include "psString.h"
     
    3840    }
    3941
    40     if (image->parent != NULL) {
    41         psMemBlock* ptr = ((psMemBlock*)image)-1;
    42         int ref = ptr->refCounter;
    43         ptr->refCounter = 2;  // make sure psFree is not retriggered
    44         psArrayRemoveData(image->parent->children,image);
    45         ptr->refCounter = ref; // restore previous count (not assuming zero, but should be)
    46 
    47         image->parent = NULL;
    48     }
    49 
    50     psImageFreeChildren(image);
    51 
     42    psImage *parent = (psImage *) image->parent;
     43
     44    // if I am a child, remove me from my parent's array of children
     45    if (parent != NULL) {
     46        // sanity check : a child cannot also be a parent
     47        if ((image->children != NULL) && (image->children->n > 0)) {
     48            psAbort ("psImage cannot be both child and parent!");
     49        }
     50
     51        // break the back-pointer first so we don't loop
     52        image->parent = NULL;
     53
     54        // drop my entry on my parent's array of children
     55        psArrayRemoveDataNoFree (parent->children, image);
     56       
     57        // drop my reference to my parent
     58        psFree (parent);
     59    }
     60   
     61    // sanity check: this function should never be reached if an image still has live children;
     62    // they should each be holding a pointer to the image, forcing the number of references to
     63    // be > 1.
     64    if (image->children && (image->children->n > 0)) {
     65        psAbort ("psImage memory management programming error : imageFree called on image with live children");
     66    }
     67
     68    psFree(image->children);
    5269    psFree(image->p_rawDataBuffer);
    5370    psFree(image->data.V);
     
    524541}
    525542
    526 
    527 int psImageFreeChildren(psImage* image)
    528 {
    529     psS32 numFreed = 0;
    530 
    531     if (image == NULL) {
    532         return numFreed;
    533     }
    534 
    535     if (image->children != NULL) {
    536         psImage** children = (psImage**)image->children->data;
    537         numFreed = image->children->n;
    538 
    539         // orphan the children first
    540         // (so psFree doesn't try to modify the parent's children array while I'm using it)
    541         for (psS32 i=0;i<numFreed;i++) {
    542             children[i]->parent = NULL;
    543         }
    544 
    545         psFree(image->children);
    546         image->children = NULL;
    547     }
    548 
    549     return numFreed;
    550 }
    551 
    552543bool p_psImagePrint (int fd,
    553544                     psImage *a,
Note: See TracChangeset for help on using the changeset viewer.