Artifact 69b3d14f45c7047cb6666bdba10f5b169100243a49dd9243b1b4305d55b52cd5:
- File
src/OFPlugin.m
— part of check-in
[9b88ee6978]
at
2011-02-27 14:40:15
on branch trunk
— OFPlugin: Don't close the handle before calling [super dealloc].
If we close the handle before calling [super dealloc], the plugin class
does not exist anymore. Instead, cache the handle, call [super dealloc]
and then close the handle. (user: js, size: 1915) [annotate] [blame] [check-ins using]
/* * Copyright (c) 2008, 2009, 2010, 2011 * Jonathan Schleifer <js@webkeks.org> * * All rights reserved. * * This file is part of ObjFW. It may be distributed under the terms of the * Q Public License 1.0, which can be found in the file LICENSE.QPL included in * the packaging of this file. * * Alternatively, it may be distributed under the terms of the GNU General * Public License, either version 2 or 3, which can be found in the file * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this * file. */ #include "config.h" #include <stdlib.h> #include <string.h> #ifndef _WIN32 #include <dlfcn.h> #endif #import "OFPlugin.h" #import "OFString.h" #import "OFAutoreleasePool.h" #import "OFExceptions.h" #ifdef _WIN32 # define dlopen(file, mode) LoadLibrary(file) # define dlsym(handle, symbol) GetProcAddress(handle, symbol) # define dlclose(handle) FreeLibrary(handle) #endif @implementation OFPlugin + pluginFromFile: (OFString*)path { OFAutoreleasePool *pool; OFMutableString *file; of_plugin_handle_t handle; OFPlugin *(*init_plugin)(); OFPlugin *plugin; pool = [[OFAutoreleasePool alloc] init]; file = [OFMutableString stringWithString: path]; [file appendCString: PLUGIN_SUFFIX]; if ((handle = dlopen([file cString], RTLD_LAZY)) == NULL) @throw [OFInitializationFailedException newWithClass: self]; [pool release]; init_plugin = (OFPlugin*(*)())dlsym(handle, "init_plugin"); if (init_plugin == NULL || (plugin = init_plugin()) == nil) { dlclose(handle); @throw [OFInitializationFailedException newWithClass: self]; } plugin->handle = handle; return plugin; } - init { if (isa == [OFPlugin class]) { Class c = isa; [self release]; @throw [OFNotImplementedException newWithClass: c selector: _cmd]; } return [super init]; } - (void)dealloc { of_plugin_handle_t h = handle; [super dealloc]; dlclose(h); } @end