New To Mac-Forums?

Welcome to our community! Join the discussion today by registering your FREE account. If you have any problems with the registration process, please contact us!

Get your questions answered by community gurus Advice and insight from world-class Apple enthusiasts Exclusive access to members-only contests, giveaways and deals

Join today!

 
Start a Discussion
 

Mac-Forums Brief

Subscribe to Mac-Forums Brief to receive special offers from Mac-Forums partners and sponsors

Join the conversation RSS
OS X - Development and Darwin Discussion and questions about development for Mac OS X.

Problem - Functions looping for no reason


Post Reply New Thread Subscribe

 
Thread Tools
guitar24t

 
Member Since: Mar 18, 2009
Posts: 2
guitar24t is on a distinguished road

guitar24t is offline
I am a very new developer to Objective-C. I have no idea what I'm doing. I wrote this program as a proof of concept GUI command line wrapper. The compressZip and compressTar functions work fine, and the initComp when called from compressZip works fine as well same with initTar respectively. However, when finishedTar (which is called by initTar) calls either compressBZip2 or compressGZip, the functions start looping, and it's not only the compress functions. The finishedCompress starts looping along with the initComp function. I already made a version where NSLog logged every start and end of a function but I couldn't figure out a pattern (not saying much though). This version is clean and has no NSLogs. If you have the time, please take a look (I mean a lot of time - I read the source for hours!). Sorry if this is confusing.
NOTE: DO NOT RUN THE BZIP2 OR GZIP COMPRESSION WITHOUT CONSTANT MONITORING AND XCODE AT THE READY TO KILL THE TASK. IT WILL COMPLETELY LOCK UP YOUR COMPUTER IF LEFT RUNNING ON THOSE TWO TASKS FOR MORE THAN 20SECS (WON'T EVEN LET YOU FORCE QUIT).

Code:
//AppController.h
#import <Cocoa/Cocoa.h>

@interface AppController : NSObject {
    IBOutlet id cmdCompress;//PushButton
    IBOutlet id mnuCompression;//Popup Menu (NSPopupButton)
    IBOutlet id progressBar;//Progress Indicator
    IBOutlet id txtPath;//Text box
	NSTask *tarFile, *compFile;
	NSString *targetPath, *archiveName;
	NSInteger compressionType;
}
- (IBAction)compressFiles:(id)sender;
- (id)compressTar;
- (id)compressZip;
- (id)compressBZip2;
- (id)compressGZip;
- (id)initTar;
- (id)initComp;
- (void)finishedTar:(NSNotification *)aNotification;
- (void)finishedCompress:(NSNotification *)aNotification;
@end

/////////////////////////////////
//AppController.m
#import "AppController.h"

@implementation AppController
- (IBAction)compressFiles:(id)sender {
    [cmdCompress setTitle:@"Running..."];
	[cmdCompress setEnabled:NO];
	[progressBar startAnimation: self];
	targetPath = [[txtPath stringValue] stringByExpandingTildeInPath];
	compressionType = [mnuCompression indexOfSelectedItem];
	if (compressionType == 0) {
		[self compressZip];
	}
	else {
		[self compressTar];
	}
}
- (id)compressZip {
	[self initComp];
	archiveName = [targetPath stringByAppendingString:@".zip"];
	compFile = [[NSTask alloc] init];
	[compFile setLaunchPath:@"/usr/bin/zip"];
	[compFile setCurrentDirectoryPath:@"/"];
	[compFile setArguments:[NSArray arrayWithObjects:@"-r",archiveName,targetPath,nil]];
	[compFile launch];
	return self;
}

- (id)compressTar {
	[self initTar];
	archiveName = [targetPath stringByAppendingString:@".tar"];
	tarFile = [[NSTask alloc] init];
	[tarFile setLaunchPath:@"/usr/bin/tar"];
	[tarFile setCurrentDirectoryPath:@"/"];
	[tarFile setArguments:[NSArray arrayWithObjects:@"-cf",archiveName,targetPath,nil]];
	[tarFile launch];
	return self;
}
- (id)compressBZip2 {
	[self initComp];
	compFile = [[NSTask alloc] init];
	[compFile setLaunchPath:@"/usr/bin/bzip2"];
	[compFile setCurrentDirectoryPath:@"/"];
	[compFile setArguments:[NSArray arrayWithObject:archiveName]];
	[compFile launch];
	return self;
}
- (id)compressGZip {
	[self initComp];
	compFile = [[NSTask alloc] init];
	[compFile setLaunchPath:@"/usr/bin/gzip"];
	[compFile setCurrentDirectoryPath:@"/"];
	[compFile setArguments:[NSArray arrayWithObject:archiveName]];
	[compFile launch];
	return self;
}

- (id)initTar {
	self = [super init];
    [[NSNotificationCenter defaultCenter] addObserver:self 
											 selector:@selector(finishedTar:) 
												 name:NSTaskDidTerminateNotification 
											   object:nil];
	
	tarFile = nil; // This is a good time to initialize the pointer
	return self;
}
- (id)initComp {
	self = [super init];
    [[NSNotificationCenter defaultCenter] addObserver:self 
											 selector:@selector(finishedCompress:) 
												 name:NSTaskDidTerminateNotification 
											   object:nil];
	
	compFile = nil; // This is a good time to initialize the pointer
	return self;
}

- (void)finishedTar:(NSNotification *)aNotification {
	[tarFile release];
	tarFile = nil;
	if (compressionType == 1) {
		[self compressBZip2];
	}
	else if (compressionType == 2) {
		[self compressGZip];
	}
}
- (void)finishedCompress:(NSNotification *)aNotification {
	[compFile release];
	compFile = nil;
	[progressBar stopAnimation: self];
	[cmdCompress setEnabled:YES];
	[cmdCompress setTitle:@"Compress Files"];
}
@end
QUOTE Thanks
guitar24t

 
Member Since: Mar 18, 2009
Posts: 2
guitar24t is on a distinguished road

guitar24t is offline
sorry, I got it.
Can't use two NSNotification
Just used one and an if statement.
Thanks,
Robert
QUOTE Thanks

Post Reply New Thread Subscribe


« I need something tested. | How to set text color in a NSPopupButtonCell in an NSTableView »
Thread Tools

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off
Forum Jump

Similar Threads
Thread
Thread Starter
Forum
Replies
Last Post
performance problem with Digital Performer 5 and others software LarryWho Music, Audio, and Podcasting 1 04-12-2009 03:53 PM
problem using Reason 4 music application silas Music, Audio, and Podcasting 2 04-30-2008 03:36 AM
Disk Permissions problem with Reason 3.0.5 jesusholdon OS X - Apps and Games 2 02-22-2007 05:36 PM
G5 Video problem WilliS Apple Desktops 3 05-05-2004 10:16 AM

All times are GMT -4. The time now is 03:31 AM.

Powered by vBulletin
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
X

Welcome to Mac-Forums.com

Create your username to jump into the discussion!

New members like you have made this community the ultimate source for your Mac since 2003!


(4 digit year)

Already a member?