Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Replace all non-cyclic NSAutoreleasepools with @autoreleasepool
  • Loading branch information
dmoagx committed May 3, 2018
1 parent 7f35608 commit b49edf6
Show file tree
Hide file tree
Showing 32 changed files with 2,755 additions and 2,859 deletions.
102 changes: 50 additions & 52 deletions Frameworks/PostgresKit/Source/PGPostgresConnection.m
Expand Up @@ -297,64 +297,62 @@ - (BOOL)cancelCurrentQuery:(NSError **)error
*/
- (void)_pollConnection:(NSNumber *)isReset
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

BOOL reset = isReset && [isReset boolValue];

int sock = PQsocket(_connection);

if (sock == -1) {
[pool release];
return;
}

struct pollfd fdinfo[1];

fdinfo[0].fd = sock;
fdinfo[0].events = POLLIN|POLLOUT;

PostgresPollingStatusType status;

do
{
status = reset ? PQresetPoll(_connection) : PQconnectPoll(_connection);

if (status == PGRES_POLLING_READING || status == PGRES_POLLING_WRITING) {
if (poll(fdinfo, 1, -1) < 0) break;
}
}
while (status != PGRES_POLLING_OK && status != PGRES_POLLING_FAILED);

if (status == PGRES_POLLING_OK && [self isConnected]) {

// Increase error verbosity
PQsetErrorVerbosity(_connection, PQERRORS_VERBOSE);

// Set notice processor
PQsetNoticeProcessor(_connection, _PGPostgresConnectionNoticeProcessor, self);

// Register or clear type extensions
NSInteger success = reset ? PQclearTypes(_connection) : PQinitTypes(_connection);

if (!success) {
NSLog(@"PostgresKit: Error: Failed to initialise or clear type extensions. Connection might return unexpected results!");
@autoreleasepool {
BOOL reset = isReset && [isReset boolValue];

int sock = PQsocket(_connection);

if (sock == -1) {
[pool release];
return;
}

[self _loadDatabaseParameters];

if (reset) {
if (_delegate && [_delegate respondsToSelector:@selector(connectionReset:)]) {
[_delegate performSelectorOnMainThread:@selector(connectionReset:) withObject:self waitUntilDone:NO];

struct pollfd fdinfo[1];

fdinfo[0].fd = sock;
fdinfo[0].events = POLLIN|POLLOUT;

PostgresPollingStatusType status;

do
{
status = reset ? PQresetPoll(_connection) : PQconnectPoll(_connection);

if (status == PGRES_POLLING_READING || status == PGRES_POLLING_WRITING) {
if (poll(fdinfo, 1, -1) < 0) break;
}
}
else {
if (_delegate && [_delegate respondsToSelector:@selector(connectionEstablished:)]) {
[_delegate performSelectorOnMainThread:@selector(connectionEstablished:) withObject:self waitUntilDone:NO];
while (status != PGRES_POLLING_OK && status != PGRES_POLLING_FAILED);

if (status == PGRES_POLLING_OK && [self isConnected]) {

// Increase error verbosity
PQsetErrorVerbosity(_connection, PQERRORS_VERBOSE);

// Set notice processor
PQsetNoticeProcessor(_connection, _PGPostgresConnectionNoticeProcessor, self);

// Register or clear type extensions
NSInteger success = reset ? PQclearTypes(_connection) : PQinitTypes(_connection);

if (!success) {
NSLog(@"PostgresKit: Error: Failed to initialise or clear type extensions. Connection might return unexpected results!");
}

[self _loadDatabaseParameters];

if (reset) {
if (_delegate && [_delegate respondsToSelector:@selector(connectionReset:)]) {
[_delegate performSelectorOnMainThread:@selector(connectionReset:) withObject:self waitUntilDone:NO];
}
}
else {
if (_delegate && [_delegate respondsToSelector:@selector(connectionEstablished:)]) {
[_delegate performSelectorOnMainThread:@selector(connectionEstablished:) withObject:self waitUntilDone:NO];
}
}
}
}

[pool release];
}

/**
Expand Down
46 changes: 22 additions & 24 deletions Frameworks/PostgresKit/Source/PGPostgresConnectionParameters.m
Expand Up @@ -138,32 +138,30 @@ - (id)valueForParameter:(NSString *)parameter
*/
- (void)_loadParameters:(id)object
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

pthread_mutex_lock(&_readLock);

NSArray *parameters = (NSArray *)object;

if (!_parameters) {
_parameters = [[NSMutableDictionary alloc] initWithCapacity:[parameters count]];
}

for (NSString *parameter in parameters)
{
const char *value = PQparameterStatus([_connection postgresConnection], [parameter UTF8String]);

if (!value) continue;

NSString *stringValue = [NSString stringWithUTF8String:value];
@autoreleasepool {
pthread_mutex_lock(&_readLock);

id paramObject = [self _isBooleanParameterValue:stringValue] ? (id)[NSNumber numberWithBool:[self _booleanForParameterValue:stringValue]] : stringValue;

[_parameters setObject:paramObject forKey:parameter];
NSArray *parameters = (NSArray *)object;

if (!_parameters) {
_parameters = [[NSMutableDictionary alloc] initWithCapacity:[parameters count]];
}

for (NSString *parameter in parameters)
{
const char *value = PQparameterStatus([_connection postgresConnection], [parameter UTF8String]);

if (!value) continue;

NSString *stringValue = [NSString stringWithUTF8String:value];

id paramObject = [self _isBooleanParameterValue:stringValue] ? (id)[NSNumber numberWithBool:[self _booleanForParameterValue:stringValue]] : stringValue;

[_parameters setObject:paramObject forKey:parameter];
}

pthread_mutex_unlock(&_readLock);
}

pthread_mutex_unlock(&_readLock);

[pool release];
}

/**
Expand Down

0 comments on commit b49edf6

Please sign in to comment.