Tag Archives: C#

Malloc and free. C programming tips

In C programming, we usually have to define a lot of customized structs, they work similar to classes but without the methods.

Usually to construct a struct, we have the following

struct xyz {
int a;
int b;
....
}

Then when you use it, you have to do

struct xyz * apointer= malloc(sizeof(struct xyz));

To simplify the code, we use

typedef struct xyz * xyz

This way, we don't have to type "struct xyz *" every time, but simply use a "xyz".

Problem usually happen when you try to free the memory. After you are done with this struct.
Then you free the memory that "apointer" points to by using free(apointer)

Many times people will get a "invalid size", or "invalid address" error when they try to free the memory and get very confused. I found it very often that this happens very often with one case. So I point it out to remind myself.
xyz curPointer=malloc(sizeof(xyz));
THIS IS VERY WRONG!!!

This means you are only malloc enough space for a xyz pointer!, which is only 8 byte( or 4 byte). You should use
xyz curPointer=malloc(sizeof(struct xyz));

This is very important!
If you did not "malloc" enough space for one struct and write more bytes into the space. You'll overwrite the memory space adjacent to it. (C does not check for this when it writes to this location exceed the malloc space). When you try to free the next memory block, it will not understand what to free, because this block was overwritten.

Suppose you have block A and B adjacent to each other. A is 8 byte, B is also 8 byte.

Then you write 9 bytes to A, and then try to free B. You'll get the error I was talking about.

Windows Service Start up

Visual Studio 2010 has a very nice template for creating a windows service. It's fairly simple to just create it.

Click here to see tutorial on how to create and install a windows service.

Because a windows service cannot be running inside visual studio, it has to be running in a windows service management, therefore the service cannot be debugged in Visual Studio. The alternative option here is to attach a debugger on the service that is running.

Click here to see tutorial on how to attach a debugger on the service.

Few things to note is: The service is not viewable in the processes dialog box if it is not started. Which means you cannot attach your debugger unless you started the service. When the service start, it executes the Onstart() method. Once it started executing that method, you can see it in the process dialog box and attach debugger.

But what if you want to debug the Onstart() method?

You can use System.Threading, and use the static method Thread.sleep(x milisec); Make the service start wait for 1o sec, and attach the debugger during that 10 sec. But here comes another trick, the service management only gives arround 30-40 sec for the Onstart() to execute. If Onstart() takes too long, then error message appears saying your service is not responding. So the longer  you wait, the less time you give Onstart() to execute.

OnContinue()

This method does NOT mean after Onstart(), it will be executed. This method is only called after the service is paused and resumed.

A general structure of a service should be,

Onstart: Use threading, generate thread to start any processes. Do not put the process here, Onstart has very limited time to start!

OnStop: interrupt the thread, disable any thread, or timer. and dispose.