The long way, the short way, the indian way ...



  • So today I'm maintaining the most hellish iPhone app I'm working on !

    The Lada Auto Finder 3000, at first my boss wanted to test indian subcontracting, so he did. Fail, they stopped responding our emails after we paid the first bill (more or less).

    <

    Now I'm stuck with Bad indian code©, no rewrite possible (because we have no time, resources, whatever ...)

    The app has been published on the AppStore, well the wrong version since I didn't commit my latest changes onto the SVN before going to lunch. We had to rush-re-publish one hour or so before the AppStore validation commitee of annoyance closes for it's hollidays.

    While fixing some memory leaks (62 potentials for 25 files), I found a way to create an empty plist file on disk rather interresting:

    	BOOL success;
    	//NSError *error;
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"Favorites.plist"];
    
    success = [fileManager fileExistsAtPath:filePath];
    if (success) 
    {
    	NSLog(@"%@",@" Favorites.plist already exists");
    	return;
    }
    
        /* FTW Begins HERE */
    //NSDictionary *propertyList;
    NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
    //propertyList = [NSDictionary dictionaryWithObjectsAndKeys:array, @"favorite", nil];
    NSData *dataRep = [NSPropertyListSerialization dataFromPropertyList: dict
    													 format: NSPropertyListXMLFormat_v1_0
    										   errorDescription:nil];
    
    success = [fileManager createFileAtPath:filePath contents:dataRep attributes:nil];//:path toPath:filePath error:&error];
    
    if (!success) {
    	NSLog(@"%@",@"Failed to create file Favorites.plist");
    }
        /* Ends HERE */
    


  • @ltouroumov said:

    Lada Auto Finder 3000

    That was a joke, right?

    @ltouroumov said:

    /* FTW Begins HERE */

    I'm probably incredibly obtuse, but I don't get it. Plz can haz more explanation?



  • Yes the name is a joke, I'm not gonna reveal the name of my real employer or I'm gonna get killed by their hordes of secret operatives

    The simples way of doing it would be:

    [[NSDictionary dictionary] writeToFile:path atomically:NO];
    

    Create an empty dictionary and write it to disk, self explanatory !

    The long way creates a serializer object wich will create an XML representation of the previously created dictionary, writes it to file. And creates a memory leak since the object isn't released from memory

    Maybe it's funny only for people who can read objective-c ?



  • @ltouroumov said:

    Maybe it's funny only for people who can read objective-c ?

    It looks convoluted, anyway, though without a point of reference it's hard to find the funny. /shrug ...And of course, some languages look like that all the time.

    I'll leave it to someone who can read objective-c to say if it's funny to them or not.



  • @ltouroumov said:

    Maybe it's funny only for people who can read objective-c ?

    Seeing the final solution, yes, that would help :-)

    @ltouroumov said:

    And creates a memory leak

    Doesn't the return create the same trouble?



  • @BlueKnot said:

    I'll leave it to someone who can read objective-c to say if it's funny to them or not.

    I don't think funny and Objective-C can go in the same sentence, however the name was hilarious, it took me back, remembered those brick like cars



  • @ltouroumov said:

    /* FTW Begins HERE */

    Isn't it funny how "FTW" and "WTF" can be used interchangeably, in a sarcastic way?



  • Dyslexia, my only weakness !!

    @b-redeker said:
    Doesn't the return create the same trouble?

    No, the objects are "temporary", they are automatically destroyed at the end of the function, only objects created with "init*" or "copy" needs to be released (with some exceptions)



  • @serguey123 said:

    @BlueKnot said:

    I'll leave it to someone who can read objective-c to say if it's funny to them or not.

    I don't think funny and Objective-C can go in the same sentence, however the name was hilarious, it took me back, remembered those brick like cars

    I thought "funny and Objective-C" had to always be used together.

     

     



  • @ltouroumov said:

    No, the objects are "temporary", they are automatically destroyed at the end of the function, only objects created with "init*" or "copy" needs to be released (with some exceptions)

    Actually, if you care, they'll be destroyed the next time through the event loop, not when the method exists (unless you set up an autorelease pool that isn't shown in the code you posted).

    I wouldn't say this example is funny. It's just creating an empty object and then serializing it. It's inefficient and stupid (especially since you don't need to create the file, you can just write the favorite info directly to the prefs for your app), but it's minor and not surprising. I wouldn't think an empty NSDictionary would be very large in memory (a few dozen bytes at most?), so while it does leak, it's a small one. And it properly detects that the file already exists before creating the empty dictionary, so after the first time it runs, it will most likely never happen again. So we're talking about one small leak the first time you run the app, then likely never again.


Log in to reply