Resolve alias' original item using bookmarkData This is my first Objective-C++ code, so I'm a bit lacking in the knowledge of methods.

I need to resolve a macOS alias and get its original location. I have access to the C string, which is the file path (in a human readable form). It also needs to be verified in this code itself that it is a true alias, and contains bookmark data. 

const char *class::resolveAliasFromURL(const char *filepath) const{

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    char filepath[] = "/Users/me/alias"; /*you need to make an alias here*/
    NSError *error = nil;
    NSString *string = [[NSString alloc] initWithUTF8String:filepath];
    NSURL *url = [NSURL fileURLWithPath:string]; 
    NSData *bmdata = [NSURL bookmarkDataWithContentsOfURL:url error:&amp;error];
    if (!error) { /*needs better error catching*/
        NSLog(@"Error: %@",error);
    }
    else {
        NSLog(@"URL: %@",url); // Useless; sanity check 
    }

    error = nil;
    BOOL isstale = NO;
    BOOL *isStalePointer = &amp;isstale;
    char ref[] = "";
    NSString *relString = [[NSString alloc] initWithUTF8String:ref];
    NSURL *relativeURL = [NSURL URLWithString:relString];
    NSURLBookmarkResolutionOptions option = NSURLBookmarkResolutionWithoutUI;
    NSURL *actual = [[NSURL alloc]
        initByResolvingBookmarkData:bookmarkData
                            options:option
                      relativeToURL:relativeURL
                bookmarkDataIsStale:isStalePointer
                              error:&amp;error]; /*resolved NSURL object file://&lt;some numbers&gt;*/

    char resolved[100]; /*resolved path in C; need to get length of the string, not arbitrarily 100 */
    strcpy(resolved, [actual fileSystemRepresentation]);
    printf(" %s\n",resolved);

    /*Need to check if the resolved object is a file or a directory*/

    [pool drain];
    return tempPath;
}


bookmarkDataWithContentsOfURL:

+ (NSData *)bookmarkDataWithContentsOfURL:(NSURL *)bookmarkFileURL 
                                    error:(NSError * _Nullable *)error;


initByResolvingBookmarkData

- (instancetype)initByResolvingBookmarkData:(NSData *)bookmarkData 
                                    options:(NSURLBookmarkResolutionOptions)options 
                              relativeToURL:(NSURL *)relativeURL 
                        bookmarkDataIsStale:(BOOL *)isStale 
                                      error:(NSError * _Nullable *)error;



Is there some improvements I can make given the goal and constraints?
Memory management

The use of NSAutoreleasePool makes it apparent that this code is compiled  manual reference counting. That is error-prone, as you'll see now: Here

NSString *string = [[NSString alloc] initWithUTF8String:filepath];
NSString *relString = [[NSString alloc] initWithUTF8String:ref];
NSURL *actual = [[NSURL alloc] initByResolvingBookmarkData ...];


are objects allocated but never released. That makes several memory leaks on each invocation of the method.

The easiest fix would be to use automatic reference counting (ARC) instead. ARC is available since Mac OS 10.6, and there is no reason not to use it. You only have to adjust the compiler flags (i.e. do not pass -fno-objc-arc to the compiler) and replace (compare this)

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
// ...
[pool drain];


by 

@autoreleasepool {
    // ...
}


The compiler takes care of everything else and inserts retain and release calls where appropriate.

If – for whatever reason – switching to ARC is not feasible then you have to fix the above-mentioned issues. You can add the needed release calls, e.g. 

NSString *string = [[NSString alloc] initWithUTF8String:filepath];
// ...
[string release];


or (perhaps simpler) create autoreleased objects (which are released on return of the function, when the autorelease pool is drained):

NSString *string = [NSString stringWithUTF8String:filepath];
NSURL *actual = [NSURL URLByResolvingBookmarkData ...];


string can also more simply be created as a “boxed C string”:

NSString *string = @(filepath);


Another error is here:

char resolved[100]; /*resolved path in C; need to get length of the string, not arbitrarily 100 */
strcpy(resolved, [actual fileSystemRepresentation]);
// ...
return tempPath;


because the function returns the address of a local variable, which is undefined behavior.

You can make the character buffer static: That seems to be what similar functions in the blender project do. The disadvantage is that subsequent calls to the function overwrite the buffer.

Or you can allocate a new string, and the caller has to release the memory eventually.

Error handling

The correct way to check for success or failure of Cocoa (Touch) methods is 
documented in
"Handling Error Objects Returned From Methods" in the "Error Handling Programming Guide":


  Important: Success or failure is indicated by the return value of the
  method. Although Cocoa methods that indirectly return error objects in
  the Cocoa error domain are guaranteed to return such objects if the
  method indicates failure by directly returning nil or NO, you should
  always check that the return value is nil or NO before attempting to
  do anything with the NSError object.


In your case, the correct way to check for the success of 

NSData *bmdata = [NSURL bookmarkDataWithContentsOfURL:url error:&amp;error];


would be

if (bmdata == nil) {
    NSLog(@"Error: %@",error);
}


But NSLog()ing the error is not sufficient: You have to report the failure to the caller, e.g. by returning a nullptr:

if (bmdata == nil) {
    NSLog(@"Error reading bookmark data for %@: %@",url, error);
    [pool drain];
    return nullptr;
}


Note that (if you keep the manual reference counting) you must take care that the autorelease pool is drained on any return from the function.

The same error handling must be done when resolving the bookmark data, that is missing completely.

More remarks


relString and relativeURL are not needed, you can simply pass nil to the relativeToURL argument.
isStalePointer is not needed, you can pass &amp;isstale directly to the bookmarkDataIsStale argument. Or pass nil is you are not interested in the outcome (at present, the staleness is not evaluated in your code).


File system paths conversions

The conversion from an NSURL back to a C string with

char resolved[100];
strcpy(resolved, [actual fileSystemRepresentation]);


suggests that the C strings represent “file system paths” and in that case the correct counterpart for the conversion from the given filepath to an NSURL is 

NSURL *url = [NSURL fileURLWithFileSystemRepresentation:filepath
                                            isDirectory:NO
                                          relativeToURL:nil];


But a buffer with 100 characters may be too short: Either determine the needed length dynamically, or use MAXPATHLEN from the system include files. Also strcpy does not check if the target buffer is large enough (potentially causing a memory corruption). Better use

static char resolved[MAXPATHLEN];
if (![actual getFileSystemRepresentation:resolved maxLength:sizeof(resolved)]) {
    NSLog(@"File system path too long for %@", url);
    [pool drain];
    return nullptr;
}


Putting it together

Putting all the above suggestions together, the code (with manual reference counting) would be

const char *YourClass::resolveAlias(const char *filepath) const {

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    NSURL *url = [NSURL fileURLWithFileSystemRepresentation:filepath
                                                isDirectory:NO
                                              relativeToURL:nil];
    NSError *error = nil;
    NSData *bmdata = [NSURL bookmarkDataWithContentsOfURL:url error:&amp;error];
    if (bmdata == nil) {
        NSLog(@"Error reading bookmark data for %@: %@",url, error);
        [pool drain];
        return nullptr;
    }
    NSURL *actual = [NSURL URLByResolvingBookmarkData:bmdata
                                              options:NSURLBookmarkResolutionWithoutUI
                                        relativeToURL:nil
                                  bookmarkDataIsStale:nil
                                                error:&amp;error];
    if (actual == nil) {
        NSLog(@"Error resolving bookmark data for %@: %@", url, error);
        [pool drain];
        return nullptr;
    }

    static char resolved[MAXPATHLEN];
    if (![actual getFileSystemRepresentation:resolved maxLength:sizeof(resolved)]) {
        NSLog(@"File system path too long for %@", url);
        [pool drain];
        return nullptr;
    }

    [pool drain];
    return resolved;
}


Further simplification

As of macOS 10.10 the NSURL class has a 

+ (instancetype)URLByResolvingAliasFileAtURL:(NSURL *)url 
                                     options:(NSURLBookmarkResolutionOptions)options 
                                       error:(NSError * _Nullable *)error;


initializer, which both reads and resolves the bookmark data. The function then simplifies to (now with automatic reference counting):

const char *YourClass::resolveAlias(const char *filepath) const {
    static char resolved[MAXPATHLEN];
    @autoreleasepool {

        NSURL *url = [NSURL fileURLWithFileSystemRepresentation:filepath
                                                    isDirectory:NO
                                                  relativeToURL:nil];
        NSError *error = nil;
        NSURL *resolvedURL = [NSURL URLByResolvingAliasFileAtURL:url
                                                      options:NSURLBookmarkResolutionWithoutUI
                                                        error:&amp;error];
        if (![resolvedURL getFileSystemRepresentation:resolved maxLength:sizeof(resolved)]) {
            NSLog(@"File system path too long for %@", url);
            return nullptr;
        }
    }
    return resolved;
}