def gcDump():
    try:
        import gc
    except ImportError:
        ERROR_MSG('Could not import gc module; ' + 'garbage collection support is not compiled in')
        return

    leakCount = 0
    gcDebugEnable()
    DEBUG_MSG('Forcing a garbage collection...')
    leakCount = gc.collect()
    s = 'Total garbage: %u' % (leakCount,)
    gcWriteLog(None, s, isError=leakCount > 0)
    if leakCount:
        gc_dump = gc.garbage[:]
        if len(gc_dump) > 0:
            garbage_ids = {id(x):x for x in gc_dump}
            garbage_list = []
            gc_refs, _ = get_refs(gc_dump, garbage_list, garbage_ids)
            del garbage_list[:]
            graph_info = get_graph_text_repr(gc_refs, garbage_ids, shortnames=False)
            for obj in graph_info['nodes'].values():
                gcWriteLog(None, repr(obj))

            for obj in graph_info['edges']:
                gcWriteLog(None, repr(obj))

            graph_info['nodes'].clear()
            del graph_info['edges'][:]
            garbage_ids.clear()
            del gc_refs[:]
            del gc_dump[:]
    del gc.garbage[:]
    return leakCount
Example #2
0
def createComplexLeak():
    """
    For testing if the garabage collection log works.
    
    Creates a reference cycle with three objects.
    
    There should be six items found in gc.garbage:
    - the "refChain" instance object
    - the dictionary of "refChain", which contains "badRefStart"
    - the "refLink1" instance object
    - the dictionary of "refLink1", which contains "badRefMiddle"
    - the "refLink2" instance object
    - the dictionary of "refLink2", which contains "badRefEnd"
    """
    from bwdebug import DEBUG_MSG
    DEBUG_MSG('Creating a complex test leak..')
    refChain = TestLeak()
    refLink1 = TestLeak()
    refLink2 = TestLeak()
    refChain.badRefStart = refLink1
    refLink1.badRefMiddle = refLink2
    refLink2.badRefEnd = refChain
    refChain = None
    refLink1 = None
    refLink2 = None
    return
Example #3
0
def gcWriteLog(file, s, isError = False):
    if isError:
        ERROR_MSG(s)
    else:
        DEBUG_MSG(s)
    if file is not None:
        file.write(s + '\n')
        file.flush()
    return
def createComplexLeak():
    DEBUG_MSG('Creating a complex test leak..')
    refChain = TestLeak()
    refLink1 = TestLeak()
    refLink2 = TestLeak()
    saltLink = TestLeak()
    refChain.badRefStart = refLink1
    refLink1.badRefMiddle = refLink2
    refLink2.saltValue = saltLink
    refLink2.badRefEnd = refChain
    refChain = None
    refLink1 = None
    refLink2 = None
    return
Example #5
0
def createBasicLeak():
    """
    For testing if the garabage collection log works.
    
    Creates a circular reference with a self-referencing object.
    
    So there should be two items found in gc.garbage:
    - the "ref" instance object
    - the dictionary of "ref", which contains "selfref"
    """
    DEBUG_MSG('Creating a simple test leak..')
    ref = TestLeak()
    ref.selfRef = ref
    ref = None
    return
Example #6
0
    def initCollision(self, restore):
        collisionBBoxes = []
        partsData = []
        partStatesMap = dict(self.partStates)
        presentPartsMap = buildPresentPartsMap(db.DBLogic.g_instance.getDestructibleObjectData(self).partsSettings, self.partTypes)
        for partID, upgrade in presentPartsMap.iteritems():
            bBoxes = upgrade.bboxes
            partState = partStatesMap[partID]
            partsData.append({'partId': partID,
             'upgradeId': upgrade.id,
             'stateId': partState})
            for i in range(partState, 0, -1):
                stateObj = upgrade.states.get(i, None)
                if stateObj and stateObj.bboxes:
                    bBoxes = stateObj.bboxes
                    break

            for bbox in bBoxes.getList():
                collisionBBoxes.append({'partId': partID,
                 'bbox': bbox,
                 'aimable': self.isPartStateAimable(presentPartsMap, partID, partState)})

        if self.useCollisionModel():
            data = db.DBLogic.g_instance.getDestructibleObjectData(self)
            self.collisionModel(partsData, data)
            if COLLISION_RECORDER:
                msgs = ['typeName = "{0}"'.format(data.typeName)]
                for partData in partsData:
                    msgs.append('partId = {0}, upgradeId = {1}, stateId = {2}'.format(partData['partId'], partData['upgradeId'], partData['stateId']))

                self.markPosition(0, self.position, '\n'.join(msgs))
                DEBUG_MSG('[{0}]'.format(']['.join(msgs)))
        self.collisionBBoxes(collisionBBoxes)
        self.staticCollision(self.useStaticCollision())
        magnetPoints = db.DBLogic.g_instance.getDestructibleObjectData(self).magnetPoints
        self.autoAimMode(3 if magnetPoints else self.useAutoAimMode())
        self.magnetPoints(magnetPoints)
        if not restore:
            self.deflectionMode(self.useDeflectionMode())
        return
Example #7
0
def gcDump(doFullCheck=False, logName=DUMP_PATH):
    """
    Performs a garbage collect and then print a dump of what has been
    collected.
    
    doFullCheck if True, do a collect and iterate over collected garbage
    and print info.
    Otherwise, do a collect and just print the number of objects collected.
    logName the name of the file to log to.
    """
    from bwdebug import DEBUG_MSG
    from bwdebug import ERROR_MSG
    try:
        import gc
    except ImportError:
        ERROR_MSG('Could not import gc module; ' +
                  'garbage collection support is not compiled in')
        return

    createTestLeaks()
    leakCount = 0
    gcDebugEnable()
    DEBUG_MSG('Forcing a garbage collection...')
    leakCount = gc.collect()
    if doFullCheck:
        copy = gc.garbage[:]
        del gc.garbage[:]
        try:
            file = open(logName, 'w')
            s = 'Total garbage: %u' % (leakCount, )
            DEBUG_MSG(s)
            file.write(s + '\n')
            if len(copy) > 0:
                DEBUG_MSG('Writing objects in garbage to file: "%s"' %
                          (logName, ))
                file.write('Objects in garbage:\n')
            i = 0
            for g in copy:
                try:
                    s = 'Garbage item %u\n' % (i, )
                    s += getObjectData(g)
                    s += getObjectReferrers(g, [gc.garbage, copy])
                    file.write('\n' + s)
                    file.flush()
                except ReferenceError as e:
                    s = 'Error: Object referenced in garbage list no longer exists: %s' % (
                        e, )
                    file.write('\n' + s)
                    ERROR_MSG(s)

                i += 1

            file.flush()
            file.close()
        except IOError as e:
            ERROR_MSG(e)
        except:
            file.write('Error printing garbage dump, see console.\n')
            ERROR_MSG('Error printing garbage dump.')
            file.flush()
            file.close()
            raise

    else:
        del gc.garbage[:]
    return leakCount
def createBasicLeak():
    DEBUG_MSG('Creating a simple test leak..')
    ref = TestLeak()
    ref.selfRef = ref
    ref = None
    return