Effective C++ item 17: Store newed objects in smart pointers in standalone statements.

Share Button

Sometimes if you try to do multiple things in one statement, you should think carefully about abnormal behavior especially exceptions. For example:

With this statement, if compile choose to do things in following order:

  1. Execute “new Widget”.
  2. Call priorigy().
  3. Call the std::tr1::shared_ptr constructor.

In addition, if the call to priority yields an exception, you are running into resource leak. Because the pointer returned from “new Widget” will be lost before it’s put into shared_ptr.

So what’s the suggestion? Don’t combine too many calls into one statement, especially dangerous ones like new objects.

“Effective C++” Third Edition by Scott Meyers.

Effective C++ item 16: Use the same form in corresponding uses of new and delete.

Share Button

Of cause I know delete corresponding to new and delete [] corresponding to new []. But sometimes things are not that simple. Imaging you are fixing a bug and need to delete an object somebody else wrote, for example:

How will you delete it? Pretty much everyone will write:

But this is not necessary right if you look a little bit further until you come across this line:

Oops! Foo is actually array of strings. So you need to use

So what’s the lesson learned here? Probably it’s good for you to comment your typedefs, and as a people editing other’s code, look a little bit further. But other than that, my suggestion is to use STL library instead of dynamically allocate arrays of objects unless it’s absolutely necassary.

“Effective C++” Third Edition by Scott Meyers.

Effective C++ item 15: Provide access to raw resources in resource-managing classes.

Share Button

In last two items I talked about managing resources using RAII objects. Now here comes the practical part. Often we will encounter a situation that a API only accept raw resource instead of RAII objects. For example:

In this situation, you need the RAII class return a raw pointer of the resource. And there are commonly two way to do that.

  1. Explicitly provide method to return the raw resource. This is how shared_ptr handles this situation. It provide get() method to return the raw resource it contains. In addition, it also overload operator-> and operator*. I prefer this way cause it’s clear and safe. But some people don’t like this explicit conversion and they will abandon to use this RAII class which, in turn, would increase the chances of leaking memory. In order to satisfy these people, we have the second way to accomplish that.
  2. Implicitly convert RAII objects to raw resource objects.How to do this? Let’s see an example.

    We overload operator FontHandle to implicit convert Font to FontHandle. This provide you the ability to omit .get() no matter the API accept Font or FontHandle. But this method has a downside. The risk is unintentional type conversion. For example:

    In this way, resource may leak due to the implicitly conversion. That’s why I prefer the first approach cause it’s safe and clear.

“Effective C++” Third Edition by Scott Meyers.

Effective C++ item 14: Think carefully about copying behavior in resource-managing classes.

Share Button

In C++, the only code that can be guaranteed to be executed after an exception is thrown are the destructors of objects residing on the stack. Resource management therefore needs to be tied to the lifespan of suitable objects in order to gain automatic allocation and reclamation. They are acquired during initialization, when there is no chance of them being used before they are available, and released with the destruction of the same objects, which is guaranteed to take place even in case of errors. This is why Resource Acquisition Is Initialization(RAII) objects are introduced. Instead of using auto_ptr and shared_ptr, sometimes you need to create your own RAII objects. If you decide to create your own RAII objects, here are four most common rules you will follow:

  1. Prohibit copying. If copying an RAII class doesn’t make sense, you should prohibit it by making copy constructor private. (see item 6)
  2. Reference-count the underlying resource. Sometimes it’s desirable to hold on to a resource until the last object using it has been destroyed. When that’s the case, copying an RAII object should increment the count of the number of objects referring to the resource. This is how shared_ptr implement copying behavior. So RAII class can implement reference-counting copying behavior by containing a shared_ptr data member. In addition, if you don’t want to delete the resource after reference count goes to 0 which is default behavior of shared_ptr, you can provide custom “deleter” for shared_ptr so that every time reference count goes to 0, this “deleter” will be called.
  3. Copy the underlying resource. In this case, the only reason to use resource-managing class is to make sure each copy is released after you are done with it. So you should perform a deep copy of resource object.
  4. Transfer ownership of the underlying resource. Sometimes you want to only keep one RAII object refers to a raw resource, you need to transfer ownership of the underlying resource to a new instance. This kind of behavior is implemented by auto_ptr.

“Effective C++” Third Edition by Scott Meyers.

How to open .ica file and setup Citrix client on Ubuntu.

Share Button

The Citrix ICA Client (=Citrix Receiver) allows access to remote Windows sessions run from a Citrix server. If you want to open .ica file using ICA client on Ubuntu, here is what you should do:

  1. Download and install the 32-bit version of libmotif3 and create a symlink:
  2. Download the Citrix Receiver 12.1 .deb files for ‘Receiver for Linux 12.1 from:http://www.citrix.com/English/ss/downloads/details.asp?downloadId=2323812&productId=1689163
  3. Run hdxcheck.sh, which was installed with the Citrix Receiver:

    Look back through the messages displayed to check that it found libjpeg.so, as it might fail to do so despite it being installed. If it failed to find it then you can edit /opt/Citrix/ICAClient/util/hdxcheck.sh and change
    Lib=find *libjpeg.so*
    Lib=find / -name *libjpeg.so*
    and run  hdxcheck.sh again.
  4.  Firefox will using ICA client to open .ica file, or you can manually choose /opt/Citrix/ICAClient/wfica.sh to open .ica file.