Monthly Archives: December 2015

Copying and placing folders

For local paths:

For full paths:

Objc/c++: Can local variable be accessed outside of its scope

If you push a local variable onto the stack, and return its address. Then in the parent scope, you have a pointer point to the return address.

After the function completes, you dereference the pointer. The result is unstable. …and really depends on whether the system decides to leave your space alone, or have someone else overwrite it.

You rent a hotel room.
You point your pointer to the return address of the local variable

You put a book in the top drawer of the bedside table and go to sleep.
In your local variable, you do stuff with it

You check out the next morning. your function finishes running
… but “forget” to give back your key. You steal the key! You do not point your pointer to NULL

A week later, you return to the hotel, do not check in, sneak into your old room with your stolen key, and look in the drawer. Your book is still there. Astonishing!

Later in your program, you decide to dereference your pointer. Depending on the circumstances, if the system has not over-written the space for that local variable, then you still see your value there! If the system have over-written it, then you will access something that is bizarre and your program may crash.

How can that be? Aren’t the contents of a hotel room drawer inaccessible if you haven’t rented the room?

Well, obviously that scenario can happen in the real world no problem. There is no mysterious force that causes your book to disappear when you are no longer authorized to be in the room. Nor is there a mysterious force that prevents you from entering a room with a stolen key.

The hotel management is not required to remove your book. You didn’t make a contract with them that said that if you leave stuff behind, they’ll shred it for you. If you illegally re-enter your room with a stolen key to get it back, the hotel security staff is not required to catch you sneaking in. You didn’t make a contract with them that said “if I try to sneak back into my room later, you are required to stop me.” Rather, you signed a contract with them that said “I promise not to sneak back into my room later”, a contract which you broke.

In this situation anything can happen. The book can be there — you got lucky. Someone else’s book can be there and yours could be in the hotel’s furnace. Someone could be there right when you come in, tearing your book to pieces. The hotel could have removed the table and book entirely and replaced it with a wardrobe. The entire hotel could be just about to be torn down and replaced with a football stadium, and you are going to die in an explosion while you are sneaking around.

You don’t know what is going to happen; when you checked out of the hotel and stole a key to illegally use later, you gave up the right to live in a predictable, safe world because you chose to break the rules of the system.

Javascript garbage collection and memory

As a programmer, in JavaScript you do not have control over stack or heap allocation.

You create objects or primitives that are located somewhere in the memory.

Internally JS engines (such as V8 or Spidermonkey) perform all kinds of optimizations.

Generally you can think of everything as if it’s stored on heap.

To get started you need to know that GC frees the memory from the objects that are not referenced. While you hold a reference to an object somewhere in your code, it will not be destroyed.

Browsers (JS engines) do not leak memory allocation abstraction to you. The error you’re referring to is probably call stack exceeded that happens when too many functions are called (mostly due to recursion).

Does the garbage collector sweep stack memory?

No. The garbage collector does only manage heap memory. All values on the stack are expected to be needed again when the program returns to that stack frame, so they must not be collected. The references from the stack into the heap are indeed considered alive.

The stack memory is cleared automatically when a function exits.

Of course, what parts of a program go onto the stack and which go into the heap is not easy to decide in a dynamic language like JavaScript. Some optimizations allow objects to be allocated on the stack, and closures might require that variable environments are allocated in the heap.

“The stack” is just a (typically fixed-size) region of your computers memory, dedicated to be “the stack” by some process. Indeed there are many stacks living in your memory, one for each thread, and interpreters (e.g. for JS) create their own stacks as well.

Mark and Sweep algorithm for GC

This algorithm reduces the definition of “an object is not needed anymore” to “an object is unreachable”.

  This algorithm assumes the knowledge of a set of objects called roots Periodically, the garbage-collector will start from these roots, find all objects that are referenced from these roots, then all objects referenced from these, etc. Starting from the roots, the garbage collector will thus find all reachable objects and collect all non-reachable objects.

  This algorithm is better than the previous one since “an object has zero reference” leads to this object being unreachable. The opposite is not true as we have seen with cycles.

  As of 2012, all modern browsers ship a mark-and-sweep garbage-collector. All improvements made in the field of JavaScript garbage collection (generational/incremental/concurrent/parallel garbage collection) over the last few years are implementation improvements of this algorithm, but not improvements over the garbage collection algorithm itself nor its reduction of the definition of when “an object is not needed anymore”.

Cycles are not a problem anymore

In the first above example, after the function call returns, the 2 objects are not referenced anymore by something reachable from the global object. Consequently, they will be found unreachable by the garbage collector.

The same thing goes with the second example. Once the div and its handler are made unreachable from the roots, they can both be garbage-collected despite referencing each other.

Limitation: objects need to be made explicitly unreachable

Although this is marked as a limitation, it is one that is rarely reached in practice which is why no one usually cares that much about garbage collection.

Javascript GC

Currently, Javascript V8’s garbage collection algorithm adopts the mark and sweep algorithm.
Hence, you do not have to worry about circular references.

Modern JavaScript implementations perform garbage collection through a “mark and sweep” algorithm. First they scan through your web app’s entire memory structure starting from the global object, and mark everything they find. Then they sweep through every object stored in memory and garbage collect anything that wasn’t marked. As long as there isn’t a reference to your object from the global object or any stored function, it can be garbage collected.

In Apple’s xCode, the obj-c garbage collector does not. Hence you DO NEED to worry and eliminate any code that has a circular reference, which includes parent/child, blocks, and delegates.

Handoff from  Watch to iOS app

download source

On the Watch side, in a button click responder, we call updateUserActivity method, which is a way to hand off data that the user is currently working on. This method is part of WKInterfaceController.h.


Notice the string key com.rtsao.handoff.

You need to go to your project folder’s Info.plist, add NSUserActivityTypes as an Array, then in that array, add com.rtsao.handoff


Then in your Watch Extension folder’s Info.plist, do the same thing.


iOS app


Run the program. Also, click the power button on your iphone to lock it.

When your  watch turns on, click on the button to activate the handoff. It calls the updateUserActivity method, which means its ready to pass off the current User’s activities to the paired iOS app.

Now click on the home button of your iphone. You’ll see that your iphone is on the “unlock screen”. You will also see that your app icon appears on the lower left hand side. This means that once the  watch passes off the user’s activity to the iOS app, you can activate the continuation of that user’s activity by sliding this icon up.


Once you slide up, your iOS app will appear, and the thread of execution will continue in AppDelegate.m’s continueUserActivity. The continueUserActivity method is part of UIApplication delegate protocol methods.

Look at the parameter userActivity. That contains all of the data you need to start processing the user’s activities. Use the activityType to retrieve the id, and the userInfo to get the data, among other data.

Also notice the restorationHandler block. This block takes an array of UIViewControllers. It then sends each of these UIViewControllers a restoreUserActivityState: message, passing in the resuming activity’s NSUserActivity object.

The window controllers inherit the restoreUserActivityState: method from NSResponder, and each controller object overrides that method to configure its window, using the information in the activity object’s userInfo dictionary.

Hence since we only have 1 main ViewController, let’s pass that in. In is simply self.window.rootViewController.

Then in our ViewController.m, we implement:

You now have the user’s activity in your particular UIViewController and its view hierarchy. Thus, this let’s you take care of processing the data in whatever part of your app.

Adding LumberJack via source code to your project

ref ––mobile-15287

Go to CocoaLumberJack’s site and download their source code here

Unzip the LumberJack project and take a look at the LumberJack class folder’s source files.


Import LumberJack’s Source files

Start a new xCode project.

Drag the listed files:

  • DDLog.h/.m
  • DDASLLogger.h/.m
  • DDTTYLogger.h/.m
  • DDFileLogger.h/.m
  • DDLegacyMacros.h
  • DDLegacyMacros.h


from the LumberJack project’s class folder, and into your project.

Creating the PCH file

Long ago, if there is a utility class that a developer needs for every of his source files, its very tiring for them to import that utility .h file in every of their source file. Hence, Prefix.pch was invented.

It is a precompiled header. It implicitly is included at the start of every source file. In other words, it’s like as if each source file adds:

Hence, you can easily include lumberjack’s functionality in all of your source code by using PCH file.

File >> New >> File

Other >> PCH file


use “Logging-PrefixHeader.pch”
make sure you check the checkbox for adding it to our target.



inside the ifndef statements.

Then at the end of the file, add

Your Logging-PrefixHeader.pch file should look like this:

Project Build Settings

Then in your Project, select your Target, Build Settings, and in the search box, type prefix. Scroll down and you’ll see Apple LLVM 7.0 – Language.

In Prefix Header, enter your .pch file name. In our case it would be Logging_PrefixHeader.pch.

Using the code

Add the following imports for DD in your AppDelegate.m file:

Then also in AppDelegate.m, insert the code as shown below:

Build and run the project. You should now see the log messages in your console.

Go to your Logging-PrefixHeader.pch file, highlight the macro LOG_LEVEL_VERBOSE, right click, and select “Jump To Definition”. Use all the different MACROS for setting what kind of log messages you want to see.

Installing xCode Colors Plugin

Install xCode color plugin by following the directions provided here.

Make sure you restart xCode.

Open up your project and in AppDelegate.m:

You should now see that your log messages are color-coded.

Adding colors to xCode debug console

download the project from here

1) Open the XcodeColors project with Xcode
2) select the XcodeColors Plugs in your target.


Then compile and build it.


When you do this, the Xcode plugin is automatically copied to the proper location.
This is done via the build settings.

Validate by opening up Finder. While the window is opened, press command + Shift + g.

Then copy and paste the below into it.

You should see the plugin file there.

Now completely Quit Xcode
Re-Launch Xcode