[sword-svn] r3373 - trunk/bindings/objc/src
mdbergmann at crosswire.org
mdbergmann at crosswire.org
Wed Apr 22 00:25:17 MST 2015
Author: mdbergmann
Date: 2015-04-22 00:25:16 -0700 (Wed, 22 Apr 2015)
New Revision: 3373
Modified:
trunk/bindings/objc/src/SwordInstallSource.h
trunk/bindings/objc/src/SwordInstallSource.mm
trunk/bindings/objc/src/SwordInstallSourceManager.mm
trunk/bindings/objc/src/SwordManager.h
trunk/bindings/objc/src/SwordManager.mm
Log:
Fixed another memory management problem where ARC would release too early.
Modified: trunk/bindings/objc/src/SwordInstallSource.h
===================================================================
--- trunk/bindings/objc/src/SwordInstallSource.h 2015-04-20 14:55:34 UTC (rev 3372)
+++ trunk/bindings/objc/src/SwordInstallSource.h 2015-04-22 07:25:16 UTC (rev 3373)
@@ -24,9 +24,6 @@
#ifdef __cplusplus
sword::InstallSource *swInstallSource;
#endif
-
- /** the sword manager for this source */
- SwordManager *swordManager;
}
// init
@@ -67,7 +64,6 @@
/** Returns the SwordManager attached to this SwordInstallSourceController */
- (SwordManager *)swordManager;
-- (void)setSwordManager:(SwordManager *)swManager;
#ifdef __cplusplus
- (sword::InstallSource *)installSource;
Modified: trunk/bindings/objc/src/SwordInstallSource.mm
===================================================================
--- trunk/bindings/objc/src/SwordInstallSource.mm 2015-04-20 14:55:34 UTC (rev 3372)
+++ trunk/bindings/objc/src/SwordInstallSource.mm 2015-04-22 07:25:16 UTC (rev 3373)
@@ -22,9 +22,6 @@
- (id)init {
self = [super init];
if(self) {
- // at first we have no sword manager
- [self setSwordManager:nil];
-
swInstallSource = new sword::InstallSource("", "");
self.deleteSwInstallSource = YES;
}
@@ -45,8 +42,6 @@
- (id)initWithSource:(sword::InstallSource *)is {
self = [super init];
if(self) {
- // at first we have no sword manager
- [self setSwordManager:nil];
swInstallSource = is;
self.deleteSwInstallSource = NO;
}
@@ -61,10 +56,6 @@
}
}
-- (void)setSwordManager:(SwordManager *)swManager {
- swordManager = swManager;
-}
-
- (NSString *)caption {
const char *str = swInstallSource->caption;
return [[NSString alloc] initWithCString:str encoding:NSUTF8StringEncoding];
@@ -151,26 +142,23 @@
// get associated SwordManager
- (SwordManager *)swordManager {
-
- if(swordManager == nil) {
+ // create SwordManager from the SWMgr of this source
+ sword::SWMgr *mgr;
+ if([self isLocalSource]) {
+ // create SwordManager from new SWMgr of path
+ mgr = new sword::SWMgr([[self directory] UTF8String], true, NULL, false, false);
+ } else {
// create SwordManager from the SWMgr of this source
- sword::SWMgr *mgr;
- if([self isLocalSource]) {
- // create SwordManager from new SWMgr of path
- mgr = new sword::SWMgr([[self directory] UTF8String], true, NULL, false, false);
- } else {
- // create SwordManager from the SWMgr of this source
- mgr = swInstallSource->getMgr();
- }
-
- if(mgr == nil) {
- ALog(@"Have a nil SWMgr!");
- } else {
- swordManager = [[SwordManager alloc] initWithSWMgr:mgr];
- }
+ mgr = swInstallSource->getMgr();
}
-
- return swordManager;
+
+ if(mgr == nil) {
+ ALog(@"Have a nil SWMgr!");
+ return nil;
+
+ } else {
+ return [[SwordManager alloc] initWithSWMgr:mgr];
+ }
}
/** low level API */
Modified: trunk/bindings/objc/src/SwordInstallSourceManager.mm
===================================================================
--- trunk/bindings/objc/src/SwordInstallSourceManager.mm 2015-04-20 14:55:34 UTC (rev 3372)
+++ trunk/bindings/objc/src/SwordInstallSourceManager.mm 2015-04-22 07:25:16 UTC (rev 3373)
@@ -250,9 +250,12 @@
}
- (void)updateInstallSource:(SwordInstallSource *)is {
+ // hold a ref to the is
+ SwordInstallSource *save = is;
// first remove, then add again
- [self removeInstallSource:is];
- [self addInstallSource:is];
+ [self removeInstallSource:save];
+ [self addInstallSource:save];
+ save = nil;
}
// installation/unInstallation
@@ -309,12 +312,9 @@
}
/**
- returns an array of Modules with status set, nil on error
+ returns an array of Modules with status set
*/
- (NSArray *)moduleStatusInInstallSource:(SwordInstallSource *)is baseManager:(SwordManager *)baseMgr {
-
- NSArray *ret = nil;
-
// get modules map
NSMutableArray *ar = [NSMutableArray array];
std::map<sword::SWModule *, int> modStats = swInstallMgr->getModuleStatus(*[baseMgr swManager], *[[is swordManager] swManager]);
@@ -328,12 +328,8 @@
[mod setStatus:status];
[ar addObject:mod];
}
-
- if(ar) {
- ret = [NSArray arrayWithArray:ar];
- }
-
- return ret;
+
+ return [NSArray arrayWithArray:ar];
}
- (BOOL)userDisclaimerConfirmed {
Modified: trunk/bindings/objc/src/SwordManager.h
===================================================================
--- trunk/bindings/objc/src/SwordManager.h 2015-04-20 14:55:34 UTC (rev 3372)
+++ trunk/bindings/objc/src/SwordManager.h 2015-04-22 07:25:16 UTC (rev 3373)
@@ -109,14 +109,10 @@
SwordFilter *thmlStripFilter, *gbfStripFilter, *osisStripFilter, *teiStripFilter;
#endif
- NSDictionary *modules;
- NSString *modulesPath;
- NSLock *managerLock;
- BOOL temporaryManager;
}
// ------------------- getter / setter -------------------
- at property (strong, readwrite) NSDictionary *modules;
+ at property (strong, readonly) NSDictionary *modules;
@property (strong, readwrite) NSString *modulesPath;
@property (strong, readwrite) NSLock *managerLock;
@property (readwrite) BOOL temporaryManager;
Modified: trunk/bindings/objc/src/SwordManager.mm
===================================================================
--- trunk/bindings/objc/src/SwordManager.mm 2015-04-20 14:55:34 UTC (rev 3372)
+++ trunk/bindings/objc/src/SwordManager.mm 2015-04-22 07:25:16 UTC (rev 3373)
@@ -19,105 +19,18 @@
using std::string;
using std::list;
- at interface SwordManager (PrivateAPI)
+ at interface SwordManager ()
+ at property (strong, readwrite) NSDictionary *modules;
+
- (void)refreshModules;
- (void)addFiltersToModule:(SwordModule *)mod;
@end
- at implementation SwordManager (PrivateAPI)
-- (void)refreshModules {
- NSMutableDictionary *dict = [NSMutableDictionary dictionary];
-
- // loop over modules
- sword::SWModule *mod;
- for(sword::ModMap::iterator it = swManager->Modules.begin(); it != swManager->Modules.end(); it++) {
- mod = it->second;
-
- if(mod) {
- // temporary instance
- SwordModule *swMod = [SwordModule moduleForSWModule:mod];
- NSString *type = [swMod typeString];
-
- ModuleType aType = [SwordModule moduleTypeForModuleTypeString:type];
- SwordModule *sm = [SwordModule moduleForType:aType swModule:mod swordManager:self];
- dict[[[sm name] lowercaseString]] = sm;
-
- [self addFiltersToModule:sm];
- }
- }
-
- // set modules
- self.modules = dict;
-}
-
-- (void)addFiltersToModule:(SwordModule *)mod {
- // prepare display filters
-
- id<FilterProvider> filterProvider = [[FilterProviderFactory providerFactory] get];
-
- switch([mod swModule]->getMarkup()) {
- case sword::FMT_GBF:
- if(!gbfFilter) {
- gbfFilter = [filterProvider newGbfRenderFilter];
- }
- if(!gbfStripFilter) {
- gbfStripFilter = [filterProvider newGbfPlainFilter];
- }
- [mod addRenderFilter:gbfFilter];
- [mod addStripFilter:gbfStripFilter];
- break;
- case sword::FMT_THML:
- if(!thmlFilter) {
- thmlFilter = [filterProvider newThmlRenderFilter];
- }
- if(!thmlStripFilter) {
- thmlStripFilter = [filterProvider newThmlPlainFilter];
- }
- [mod addRenderFilter:thmlFilter];
- [mod addStripFilter:thmlStripFilter];
- break;
- case sword::FMT_OSIS:
- if(!osisFilter) {
- osisFilter = [filterProvider newOsisRenderFilter];
- }
- if(!osisStripFilter) {
- osisStripFilter = [filterProvider newOsisPlainFilter];
- }
- [mod addRenderFilter:osisFilter];
- [mod addStripFilter:osisStripFilter];
- break;
- case sword::FMT_TEI:
- if(!teiFilter) {
- teiFilter = [filterProvider newTeiRenderFilter];
- }
- if(!teiStripFilter) {
- teiStripFilter = [filterProvider newTeiPlainFilter];
- }
- [mod addRenderFilter:teiFilter];
- [mod addStripFilter:teiStripFilter];
- break;
- case sword::FMT_PLAIN:
- default:
- if(!plainFilter) {
- plainFilter = [filterProvider newOsisPlainFilter];
- }
- [mod addRenderFilter:plainFilter];
- break;
- }
-}
-
- at end
-
@implementation SwordManager
- at synthesize modules;
- at synthesize modulesPath;
- at synthesize managerLock;
- at synthesize temporaryManager;
-
# pragma mark - class methods
+ (NSArray *)moduleTypes {
@@ -143,7 +56,7 @@
if((self = [super init])) {
// this is our main swManager
- temporaryManager = NO;
+ self.temporaryManager = NO;
self.modulesPath = path;
@@ -167,7 +80,7 @@
if(self) {
swManager = aSWMgr;
// this is a temporary swManager
- temporaryManager = YES;
+ self.temporaryManager = YES;
self.modules = [NSDictionary dictionary];
self.managerLock = (id) [[NSRecursiveLock alloc] init];
@@ -180,27 +93,28 @@
- (void)dealloc {
- if(!temporaryManager) {
+ if(!self.temporaryManager) {
+ ALog(@"Deleting SWMgr!");
delete swManager;
}
}
- (void)reInit {
- [managerLock lock];
- if(modulesPath && [modulesPath length] > 0) {
+ [self.managerLock lock];
+ if(self.modulesPath && [self.modulesPath length] > 0) {
NSFileManager *fm = [NSFileManager defaultManager];
- if(![fm fileExistsAtPath:modulesPath]) {
+ if(![fm fileExistsAtPath:self.modulesPath]) {
[self createModuleFolderTemplate];
}
// modulePath is the main sw manager
- swManager = new sword::SWMgr([modulesPath UTF8String], true, new sword::EncodingFilterMgr(sword::ENC_UTF8));
+ swManager = new sword::SWMgr([self.modulesPath UTF8String], true, new sword::EncodingFilterMgr(sword::ENC_UTF8));
if(!swManager) {
ALog(@"Cannot create SWMgr instance for default module path!");
} else {
- NSArray *subDirs = [fm contentsOfDirectoryAtPath:modulesPath error:NULL];
+ NSArray *subDirs = [fm contentsOfDirectoryAtPath:self.modulesPath error:NULL];
// for all sub directories add module
BOOL directory;
NSString *fullSubDir;
@@ -211,7 +125,7 @@
![subDir isEqualToString:@"InstallMgr"] &&
![subDir isEqualToString:@"mods.d"] &&
![subDir isEqualToString:@"modules"]) {
- fullSubDir = [modulesPath stringByAppendingPathComponent:subDir];
+ fullSubDir = [self.modulesPath stringByAppendingPathComponent:subDir];
fullSubDir = [fullSubDir stringByStandardizingPath];
//if its a directory
@@ -231,18 +145,18 @@
SendNotifyModulesChanged(NULL);
}
}
- [managerLock unlock];
+ [self.managerLock unlock];
}
- (void)createModuleFolderTemplate {
NSFileManager *fm = [NSFileManager defaultManager];
- [fm createDirectoryAtPath:modulesPath withIntermediateDirectories:NO attributes:nil error:NULL];
- [fm createDirectoryAtPath:[modulesPath stringByAppendingPathComponent:@"mods.d"] withIntermediateDirectories:NO attributes:nil error:NULL];
- [fm createDirectoryAtPath:[modulesPath stringByAppendingPathComponent:@"modules"] withIntermediateDirectories:NO attributes:nil error:NULL];
+ [fm createDirectoryAtPath:self.modulesPath withIntermediateDirectories:NO attributes:nil error:NULL];
+ [fm createDirectoryAtPath:[self.modulesPath stringByAppendingPathComponent:@"mods.d"] withIntermediateDirectories:NO attributes:nil error:NULL];
+ [fm createDirectoryAtPath:[self.modulesPath stringByAppendingPathComponent:@"modules"] withIntermediateDirectories:NO attributes:nil error:NULL];
}
- (void)addModulesPath:(NSString *)path {
- [managerLock lock];
+ [self.managerLock lock];
if(swManager == nil) {
swManager = new sword::SWMgr([path UTF8String], true, new sword::EncodingFilterMgr(sword::ENC_UTF8));
} else {
@@ -250,14 +164,95 @@
}
[self refreshModules];
- [managerLock unlock];
+ [self.managerLock unlock];
SendNotifyModulesChanged(NULL);
}
+- (void)refreshModules {
+ NSMutableDictionary *dict = [NSMutableDictionary dictionary];
+
+ // loop over modules
+ sword::SWModule *mod;
+ for(sword::ModMap::iterator it = swManager->Modules.begin(); it != swManager->Modules.end(); it++) {
+ mod = it->second;
+
+ if(mod) {
+ // temporary instance
+ SwordModule *swMod = [SwordModule moduleForSWModule:mod];
+ NSString *type = [swMod typeString];
+
+ ModuleType aType = [SwordModule moduleTypeForModuleTypeString:type];
+ SwordModule *sm = [SwordModule moduleForType:aType swModule:mod swordManager:self];
+ dict[[[sm name] lowercaseString]] = sm;
+
+ [self addFiltersToModule:sm];
+ }
+ }
+
+ // set modules
+ self.modules = dict;
+}
+
+- (void)addFiltersToModule:(SwordModule *)mod {
+ // prepare display filters
+
+ id<FilterProvider> filterProvider = [[FilterProviderFactory providerFactory] get];
+
+ switch([mod swModule]->getMarkup()) {
+ case sword::FMT_GBF:
+ if(!gbfFilter) {
+ gbfFilter = [filterProvider newGbfRenderFilter];
+ }
+ if(!gbfStripFilter) {
+ gbfStripFilter = [filterProvider newGbfPlainFilter];
+ }
+ [mod addRenderFilter:gbfFilter];
+ [mod addStripFilter:gbfStripFilter];
+ break;
+ case sword::FMT_THML:
+ if(!thmlFilter) {
+ thmlFilter = [filterProvider newThmlRenderFilter];
+ }
+ if(!thmlStripFilter) {
+ thmlStripFilter = [filterProvider newThmlPlainFilter];
+ }
+ [mod addRenderFilter:thmlFilter];
+ [mod addStripFilter:thmlStripFilter];
+ break;
+ case sword::FMT_OSIS:
+ if(!osisFilter) {
+ osisFilter = [filterProvider newOsisRenderFilter];
+ }
+ if(!osisStripFilter) {
+ osisStripFilter = [filterProvider newOsisPlainFilter];
+ }
+ [mod addRenderFilter:osisFilter];
+ [mod addStripFilter:osisStripFilter];
+ break;
+ case sword::FMT_TEI:
+ if(!teiFilter) {
+ teiFilter = [filterProvider newTeiRenderFilter];
+ }
+ if(!teiStripFilter) {
+ teiStripFilter = [filterProvider newTeiPlainFilter];
+ }
+ [mod addRenderFilter:teiFilter];
+ [mod addStripFilter:teiStripFilter];
+ break;
+ case sword::FMT_PLAIN:
+ default:
+ if(!plainFilter) {
+ plainFilter = [filterProvider newOsisPlainFilter];
+ }
+ [mod addRenderFilter:plainFilter];
+ break;
+ }
+}
+
- (SwordModule *)moduleWithName:(NSString *)name {
- SwordModule *ret = modules[[name lowercaseString]];
+ SwordModule *ret = self.modules[[name lowercaseString]];
if(ret == nil) {
sword::SWModule *mod = [self getSWModuleWithName:name];
if(mod == NULL) {
@@ -271,7 +266,7 @@
ret = [SwordModule moduleForType:aType swModule:mod swordManager:self];
if(ret != nil) {
- NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithDictionary:modules];
+ NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithDictionary:self.modules];
dict[[name lowercaseString]] = ret;
self.modules = dict;
}
@@ -282,17 +277,17 @@
}
- (void)setCipherKey:(NSString *)key forModuleNamed:(NSString *)name {
- [managerLock lock];
+ [self.managerLock lock];
swManager->setCipherKey([name UTF8String], [key UTF8String]);
- [managerLock unlock];
+ [self.managerLock unlock];
}
#pragma mark - module access
- (void)setGlobalOption:(NSString *)option value:(NSString *)value {
- [managerLock lock];
+ [self.managerLock lock];
swManager->setGlobalOption([option UTF8String], [value UTF8String]);
- [managerLock unlock];
+ [self.managerLock unlock];
}
- (BOOL)globalOption:(NSString *)option {
@@ -300,10 +295,10 @@
}
- (NSArray *)listModules {
- return [modules allValues];
+ return [self.modules allValues];
}
- (NSArray *)moduleNames {
- return [modules allKeys];
+ return [self.modules allKeys];
}
- (NSArray *)sortedModuleNames {
@@ -312,7 +307,7 @@
- (NSArray *)modulesForFeature:(NSString *)feature {
NSMutableArray *ret = [NSMutableArray array];
- for(SwordModule *mod in [modules allValues]) {
+ for(SwordModule *mod in [self.modules allValues]) {
if([mod hasFeature:feature]) {
[ret addObject:mod];
}
@@ -327,7 +322,7 @@
- (NSArray *)modulesForType:(ModuleType)type {
NSMutableArray *ret = [NSMutableArray array];
- for(SwordModule *mod in [modules allValues]) {
+ for(SwordModule *mod in [self.modules allValues]) {
if([mod type] == type || type == All) {
[ret addObject:mod];
}
@@ -342,7 +337,7 @@
- (NSArray *)modulesForCategory:(ModuleCategory)cat {
NSMutableArray *ret = [NSMutableArray array];
- for(SwordModule *mod in [modules allValues]) {
+ for(SwordModule *mod in [self.modules allValues]) {
if([mod category] == cat) {
[ret addObject:mod];
}
@@ -364,9 +359,9 @@
- (sword::SWModule *)getSWModuleWithName:(NSString *)moduleName {
sword::SWModule *module;
- [managerLock lock];
+ [self.managerLock lock];
module = swManager->Modules[[moduleName UTF8String]];
- [managerLock unlock];
+ [self.managerLock unlock];
return module;
}
More information about the sword-cvs
mailing list