Wednesday, February 29, 2012

Memory leak detection in C,C++ codes and Apps

On of the major part of C/C++ native application development is the memory management.Allocation and Deallocation of memory on runtime and cleaning memory before exit is hard to keep in touch while development. If OOP is in the best use this can be minimized but still can be a issue. Specially while loading and using 3rd party dll and libraries can cause unknown issues.

Detection of leaks can be done in many ways,

  1. code level
  2. using detection tools.

Code level memory detection is supported by most compilers and there are many libraries to do so. This is almost done in debug mode.
_CRT library example of memory detection.

This is a set of libraries given my Microsoft to monitor memory status in the application:

Before using this you have to include following lines of codes:



Then after you have to go to a certain function call and check whether there was a memory leak within execution of those lines of codes.

_CrtMemState s1, s2, s3;
_CrtMemCheckpoint( &s1 );
   //Run the required function call here...
_CrtMemCheckpoint( &s2 );
if ( _CrtMemDifference( &s3, &s1, &s2) ){
_CrtMemDumpStatistics( &s3 );}

This will print the memory leak details on the debug window.

0 bytes in 0 Free Blocks.
481371 bytes in 867 Normal Blocks.
1284 bytes in 4 CRT Blocks.
0 bytes in 0 Ignore Blocks.
0 bytes in 0 Client Blocks.
Largest number used: 1056688 bytes.
Total allocations: 2270062 bytes.

You can Google and find out what the exact meaning of those block types. But what we most identify are the Normal Blocks.
So, to avoid memory leaks, you have to go in to that function and add more of those code and find out the last set of minor codes which cause the memory leak and fix it. Unfortunately there is no easy method to fix those. Because all those are due to you are forgetting to deallocate memory objects allocated earlier.

There are many Memory detection and monitoring tools.Most of those tools are hardware dependent. Because in order to monitor an executing application, it needs to go deep into the application execution. So tools become best when they can go deep in to the Hardware Layer. There for most memory detection tools are products of processor manufacturer like Intel and AMD.

  1. Valgrind [only for linux but superb] [ ]
  2. Very Sleepy[ ]
  3. AMD CodeAnalyzer[ ]
  4. Microsoft Task Manager [a general tool, but to that informative]
You can run your app using those tools and they will give you how each functions, threads, objects are occupying memory in run time. So you can go back to the code and identify those locations and fix.

Memory leak detection, locating and fixing is a time consuming process and it could not be 100% fixable task. But you can identify re-occurring leaks and fix those at first. This will make application long last on run and more reliable. So the application will make more stable. Some memory leaks which occurs on initial memory allocation will not continue to grow through continuously, so may not be that much critical.

So, finally its all about making your critical application more stable and run last long more reliably.

Monday, February 27, 2012

Apache Bar Camp- Colombo 2012

Today sri lankan techies had a wonderful time with apache at UCSC on 
with the help of WSO2 and commitment of special apache people here at colombo.

The session was with 3 keynote speakers, started by Dr.sanjeewa weerawarna, CEO of WSO2. He was giving his valuable time for the newbees and followups with great information of his lifetime story with FOSS.

"The Apache Way" could bring the audience to the interesting of being part of the apache comminuty for the community in more long term basis. The simplest form of "Just F***** Do It" [JFDI] was introduced and mentored on the apache community. Meritocracy vs Democracy was a key on the success of apache, and was only used in case of a evading. Otherwise every individual was the key node on the apache community, not either a big part of huge business could break that synopsis.

Subversion system is the first time machine developed[only usable by committees], which make you secure on pushing any FOSS project on your way and give a try bu JFDI. Its secured, because in any case of fire... you can travel backward through your code base and fix your history to continue with greater attitude in the next comeback.Ross  Gardler was stunning on JDFI always.. ;)

So, its all about contribution to all apache with what you can, and its not what they MAY want from you. Let community to decide whether you path is on track and its your responsible to listen to them and make your contributions to better path.Your start might me embarrassing like all committees, members. But its the only way to make sure apache users may not get embarrassing.

So, there have bee the announcement of Google Summer Codes. Prepare you time for it as well.

I would like to thank Nick Burch, Ross, Dr.sanjeewa and the srilanan officials and the community for the great event.

Tomorrow is time for BigData at 2nd day. so its time to have a snap before being late tomorrow at virtusa auditorium.

see you with some words about BigData. Thank you.

Sunday, February 26, 2012

New programming slang - Programming

There was some interesting post on a site which sumarizes content from As it was pretty nice, i thought of republishing here for my personal reference. So, you guys n grls please don't read this. It can be a copyright issue for me.... ;)

Yoda Conditions
The act of using if (constant == variable) instead of natural if (variable == constant),; for example, if (4 == foo). Because it is like "if it is the blue – this is the sky" or "if that is tall – this is a man."


Pokémon Exception Handling
When you catch all the exceptions and then you try somehow to analyze them.

}catch ( Exception ex){
if( ex instanceof SubException){
}else if(ex instanceof SubSubException){

Discuss an example here

Egyptian brackets
This is the style of brackets, when an opening bracket is placed at the end of this line:

if (a == b) {

Why do we call this style "Egyptian brackets?" Compare the location of the brackets with the location of the hands in this picture:


(This style of brackets was used in the famous book of Kernighan and Ritchie "C Programming Language", so it is also known as the style of K&R).

Different types of bug reports

Smug Report
A bug report submitted by a user who thinks he knows a lot more about the system’s design than he really does. Filled with irrelevant technical details and one or more suggestions (always wrong) about what he thinks is causing the problem and how we should fix it.

Drug Report
This is a bug report so utterly incomprehensible that whoever submitted it must have been smoking crack. The lesser version is a chug report, where the submitter is thought have had one too many.

Shrug Report
This is a bug report with no error message or “how to reproduce” steps and only a vague description of the problem. Usually contains the phrase "doesn’t work."


A Duck
A feature added for no other reason than to draw management attention and be removed, thus avoiding unnecessary changes in other aspects of the product.
This started as a piece of Interplay corporate lore. It was well known that producers (a game industry position, roughly equivalent to PMs) had to make a change to everything that was done. The assumption was that subconsciously they felt that if they didn’t, they weren’t adding value.

The artist working on the queen animations for Battle Chess was aware of this tendency, and came up with an innovative solution. He did the animations for the queen the way that he felt would be best, with one addition: he gave the queen a pet duck. He animated this duck through all of the queen’s animations, had it flapping around the corners. He also took great care to make sure that it never overlapped the "actual" animation.

Eventually, it came time for the producer to review the animation set for the queen. The producer sat down and watched all of the animations. When they were done, he turned to the artist and said, "That looks great. Just one thing – get rid of the duck."


The process of taking a well-designed piece of code and, through a series of small, reversible changes, making it completely unmaintainable by anyone except yourself.

Stringly Typed
It is a riff on strongly-typed. It is used to describe an implementation that needlessly relies on strings when programmer- and refactor-friendly options are available.
Examples: Method parameters that take strings when other more appropriate types should be used. On the occasion that a string is required in a method call (e.g. network service), the string is then passed and used throughout the rest of the call graph without first converting it to a more suitable internal representation (e.g. parse it and create an enum, then you have strong typing throughout the rest of your codebase).
This is message passing without using typed messages etc. Excessively stringly typed code is usually a pain to understand and detonates at runtime with errors that the compiler would normally find.


Doctype Decoration
When web designers add a proper DOCTYPE declaration at the beginning of an HTML document, but then don not bother to write correct markup for the rest of it.

It is an error, which disappears or alters when it is attempted to be identified by analogy to Heisenberg uncertainty principle in the quantum physics.


A generalized name for the clueless/new developer. The submitter at Stack Overflow writes:
We found as we were developing a framework component that required minimal knowledge of how it worked for the other developers. We would always phrase our questions as: "What if Jimmy forgets to update the attribute?"

This led to the term "Jimmy-proof" when referring to well designed framework code.


It is a hypothetical error, which existence is predicted on the basis of a small number (possibly) related to the log and vague anecdotal reports from the users, which is difficult to reproduce on the developer machine, because it is impossible to know whether it actually exists, and if there is something that caused it.

Here is a link to the Higgs boson - a hypothetical massive elementary particle that is predicted to exist by the Standard Model SM of particle physics.

It is not napping, but simply zoning out. It comes from the assembly language instruction NOP, for No OPeration, which does nothing.


Other types of errors

It is an error that causes the catastrophic data destruction.

Here is a link to Hindenburg dirigible, was destroyed during its attempt to dock with its mooring mast at the Lakehurst Naval Air Station.

It is a defensive move useful for code reviews. If someone reviewing your code presents you with a bug that is your fault, you counter with a counterbug: a bug caused by the reviewer.

This is the error that randomly generates money.
Perhaps, this is a link to the Bloomberg company.

An adjective to describe a feature that’s so early in the planning stages that it might as well be imaginary. This one comes from Rails Core Team member Yehuda Katz, who used it in his closing keynote at last year’s Windy City Rails to describe some of Rails’ upcoming features.

Barack Obama
It is a project management account that we assign the most aspirational tasks, the stuff we would really like to do, but we will probably never get an approval for it.


Loch Ness Monster bug
It is an error that will not repeat and only one person had seen it.


Baklava code
This is a code, which has too many layers.


This is the feeling, when a cracked approach still can or cannot be cracked further.

Fear Driven Development
When project management adds more pressure, such as by firing a member of the team.


Common Law Feature
This is an error in the application, which has existed for so long that now it is a part of the expected functionality.

Here is a link to the "Common law", also it is known as case law or precedent.

Adding 2 to a variable.

Idioms that come from the food

Programmer fuel
It could be: Lemonade Mountain Dew, coffee and anything that fills you with caffeine.

Hot potato / Hot potatoes
It is a fun way to pronounce http:// and https://.

Send all the information about the client through the service authorization at «hot potatoes»: // company

It depends on the context, usually it refers to some action on the noun. This noun should consist of one syllable for easy communication.

Banana banana banana
Placeholder text that indicates the documentation is in progress or yet to be completed. Mostly used because FxCop complains when a public function lacks documentation.
(Example: SVNStyleCop в C# или FxCop в C++).

/// banana banana banana
public CustomerValidationResponse Validate(CustomerValidationRequest request, booluseStrictValidation, bool throwIfSessionExpired)

Chunky salsa
This is a single critical error or bug that renders an entire system unusable, especially in a production environment.
It is based on the chunky salsa rule. Any situation that would reduce a character’s head to the consistency of chunky salsa dip is fatal, regardless of other rules.
The whole system turned chunky salsa after Bob's change to the login screen locked everyone out of their accounts.


A horribly mispronounced version of the file extension .sql.gz
We made it up while setting up a server and looking for something easier/more fun to say than "dot ess que ell dot gee zee file" or "gzipped sequel file."

Hooker Code
Code that is problematic and causes application instability (application "goes down" often). Example: “Did the site go down again? Yeah, Jim must still have some hooker code in there”.


Reality 101 failure
The program (or more likely feature of a program) does exactly what was asked for, but when it’s deployed it turns out that the problem was misunderstood and the program is basically useless.

Hydra Code
A code that cannot be fixed. One fix causes two new bugs.
It should be rewritten.


This is a prototype that ends up in production.


Ninja Comments
It is also known as invisible comments, secret comments, or no comments.


Sometimes, you just have to talk a problem out; for example: Someone put a rubber duck on the monitor, so he could talk to it, therefore, rubberducking is talking your way through a problem.

Using a database to store information.
Example: "Hey, I'll put all of our customers into a Word document and then we can X." "No, we should do that databasically so that we can keep that list up to date."

The author of this idiom states that he named his company - “Databasically”.


Smurf Naming Convention
When almost every class has the same prefix. IE, when a user clicks on the button, a SmurfAccountView passes a SmurfAccountDTO to the SmurfAccountController. The SmurfID is used to fetch a SmurfOrderHistory which is passed to the SmurfHistoryMatch before forwarding to either SmurfHistoryReviewView or SmurfHistoryReportingView. If a SmurfErrorEvent occurs it is logged by SmurfErrorLogger to ${app}/smurf/log/smurf/smurflog.log.

MEGA MOnolithic meTHod. Usually, it stretches over two screens in height and often contained inside aGod Object (an object that knows or does too much). Megamoths of greater size than 2k LOC have been sighted. Beware of the megamoth!


apt-got and wgot
It designates the programs that were installed (on Unix) using the command apt-get and files that were downloaded using the command wget.
Examples: “I apt-got 100MB of updates this morning”. “Once this loop has wgot that file, it takes the md5 hash”.