pEpObjCAdapter/PEPSession.m
changeset 233 cb333feb4f0a
parent 219 8b2f3687374f
child 240 90664b3a2988
     1.1 --- a/pEpObjCAdapter/PEPSession.m	Thu Sep 14 14:08:53 2017 +0200
     1.2 +++ b/pEpObjCAdapter/PEPSession.m	Mon Sep 18 10:48:38 2017 +0200
     1.3 @@ -57,6 +57,8 @@
     1.4      });
     1.5  }
     1.6  
     1.7 +static NSString *threadCountKey = @"PEPSession.threadCount";
     1.8 +
     1.9  - (id)init
    1.10  {
    1.11      [PEPSession setupTrustWordsDB];
    1.12 @@ -69,6 +71,8 @@
    1.13  
    1.14      [PEPObjCAdapter bindSession:self];
    1.15  
    1.16 +    [self incSessionCount];
    1.17 +
    1.18      return self;
    1.19  }
    1.20  
    1.21 @@ -77,6 +81,37 @@
    1.22      [PEPObjCAdapter unbindSession:self];
    1.23  
    1.24      release(_session);
    1.25 +
    1.26 +    [self decSessionCount];
    1.27 +}
    1.28 +
    1.29 +- (void)incSessionCount
    1.30 +{
    1.31 +    NSNumber *sessionCount = [[NSThread currentThread] threadDictionary][threadCountKey];
    1.32 +    if (!sessionCount) {
    1.33 +        sessionCount = [NSNumber numberWithInteger:1];
    1.34 +    } else {
    1.35 +        sessionCount = [NSNumber numberWithInteger:sessionCount.integerValue + 1];
    1.36 +    }
    1.37 +    [[NSThread currentThread] threadDictionary][threadCountKey] = sessionCount;
    1.38 +}
    1.39 +
    1.40 +- (void)decSessionCount
    1.41 +{
    1.42 +    NSNumber *sessionCount = [[NSThread currentThread] threadDictionary][threadCountKey];
    1.43 +    if (sessionCount) {
    1.44 +        sessionCount = [NSNumber numberWithInteger:sessionCount.integerValue - 1];
    1.45 +        [[NSThread currentThread] threadDictionary][threadCountKey] = sessionCount;
    1.46 +    } else {
    1.47 +        NSAssert(NO, @"Session count on thread below 0, thread mixup between init and dealloc?");
    1.48 +    }
    1.49 +}
    1.50 +
    1.51 +- (void)dumpThreadCount:(NSString *)contextName
    1.52 +{
    1.53 +    NSNumber *sessionCount = [[NSThread currentThread] threadDictionary][threadCountKey];
    1.54 +    NSLog(@"%@: (%@) %ld", contextName,
    1.55 +          [[NSThread currentThread] name], (long) sessionCount.integerValue);
    1.56  }
    1.57  
    1.58  /**