Forums
New posts
Articles
Product Reviews
Policies
FAQ
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Menu
Log in
Register
Install the app
Install
Forums
macOS & iOS Developer Playground
macOS - Development and Darwin
Problem - Functions looping for no reason
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="guitar24t" data-source="post: 821000" data-attributes="member: 92731"><p>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.</p><p>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).</p><p></p><p>[CODE]</p><p>//AppController.h</p><p>#import <Cocoa/Cocoa.h></p><p></p><p>@interface AppController : NSObject {</p><p> IBOutlet id cmdCompress;//PushButton</p><p> IBOutlet id mnuCompression;//Popup Menu (NSPopupButton)</p><p> IBOutlet id progressBar;//Progress Indicator</p><p> IBOutlet id txtPath;//Text box</p><p> NSTask *tarFile, *compFile;</p><p> NSString *targetPath, *archiveName;</p><p> NSInteger compressionType;</p><p>}</p><p>- (IBAction)compressFiles:(id)sender;</p><p>- (id)compressTar;</p><p>- (id)compressZip;</p><p>- (id)compressBZip2;</p><p>- (id)compressGZip;</p><p>- (id)initTar;</p><p>- (id)initComp;</p><p>- (void)finishedTar:(NSNotification *)aNotification;</p><p>- (void)finishedCompress:(NSNotification *)aNotification;</p><p>@end</p><p></p><p>/////////////////////////////////</p><p>//AppController.m</p><p>#import "AppController.h"</p><p></p><p>@implementation AppController</p><p>- (IBAction)compressFiles:(id)sender {</p><p> [cmdCompress setTitle:@"Running..."];</p><p> [cmdCompress setEnabled:NO];</p><p> [progressBar startAnimation: self];</p><p> targetPath = [[txtPath stringValue] stringByExpandingTildeInPath];</p><p> compressionType = [mnuCompression indexOfSelectedItem];</p><p> if (compressionType == 0) {</p><p> [self compressZip];</p><p> }</p><p> else {</p><p> [self compressTar];</p><p> }</p><p>}</p><p>- (id)compressZip {</p><p> [self initComp];</p><p> archiveName = [targetPath stringByAppendingString:@".zip"];</p><p> compFile = [[NSTask alloc] init];</p><p> [compFile setLaunchPath:@"/usr/bin/zip"];</p><p> [compFile setCurrentDirectoryPath:@"/"];</p><p> [compFile setArguments:[NSArray arrayWithObjects:@"-r",archiveName,targetPath,nil]];</p><p> [compFile launch];</p><p> return self;</p><p>}</p><p></p><p>- (id)compressTar {</p><p> [self initTar];</p><p> archiveName = [targetPath stringByAppendingString:@".tar"];</p><p> tarFile = [[NSTask alloc] init];</p><p> [tarFile setLaunchPath:@"/usr/bin/tar"];</p><p> [tarFile setCurrentDirectoryPath:@"/"];</p><p> [tarFile setArguments:[NSArray arrayWithObjects:@"-cf",archiveName,targetPath,nil]];</p><p> [tarFile launch];</p><p> return self;</p><p>}</p><p>- (id)compressBZip2 {</p><p> [self initComp];</p><p> compFile = [[NSTask alloc] init];</p><p> [compFile setLaunchPath:@"/usr/bin/bzip2"];</p><p> [compFile setCurrentDirectoryPath:@"/"];</p><p> [compFile setArguments:[NSArray arrayWithObject:archiveName]];</p><p> [compFile launch];</p><p> return self;</p><p>}</p><p>- (id)compressGZip {</p><p> [self initComp];</p><p> compFile = [[NSTask alloc] init];</p><p> [compFile setLaunchPath:@"/usr/bin/gzip"];</p><p> [compFile setCurrentDirectoryPath:@"/"];</p><p> [compFile setArguments:[NSArray arrayWithObject:archiveName]];</p><p> [compFile launch];</p><p> return self;</p><p>}</p><p></p><p>- (id)initTar {</p><p> self = [super init];</p><p> [[NSNotificationCenter defaultCenter] addObserver:self </p><p> selector:@selector(finishedTar:) </p><p> name:NSTaskDidTerminateNotification </p><p> object:nil];</p><p> </p><p> tarFile = nil; // This is a good time to initialize the pointer</p><p> return self;</p><p>}</p><p>- (id)initComp {</p><p> self = [super init];</p><p> [[NSNotificationCenter defaultCenter] addObserver:self </p><p> selector:@selector(finishedCompress:) </p><p> name:NSTaskDidTerminateNotification </p><p> object:nil];</p><p> </p><p> compFile = nil; // This is a good time to initialize the pointer</p><p> return self;</p><p>}</p><p></p><p>- (void)finishedTar:(NSNotification *)aNotification {</p><p> [tarFile release];</p><p> tarFile = nil;</p><p> if (compressionType == 1) {</p><p> [self compressBZip2];</p><p> }</p><p> else if (compressionType == 2) {</p><p> [self compressGZip];</p><p> }</p><p>}</p><p>- (void)finishedCompress:(NSNotification *)aNotification {</p><p> [compFile release];</p><p> compFile = nil;</p><p> [progressBar stopAnimation: self];</p><p> [cmdCompress setEnabled:YES];</p><p> [cmdCompress setTitle:@"Compress Files"];</p><p>}</p><p>@end</p><p>[/CODE]</p></blockquote><p></p>
[QUOTE="guitar24t, post: 821000, member: 92731"] 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 [/CODE] [/QUOTE]
Verification
Post reply
Forums
macOS & iOS Developer Playground
macOS - Development and Darwin
Problem - Functions looping for no reason
Top