Thursday, July 22, 2010

Creating a UUID (universally unique identifier)

Most of my blogs are created because I spend too many hours searching, and trying to figure out how something works, and I don't want to do that again (if I need the same feature) and help other people to avoid the search and "experimentation".

What the problem was? I need a Universally unique identifier that works on *nix and Windows, so here's a piece of code I used in linux to create the uuid (this works without any problem):


#include

uuid_t t;
uuid_generate(t);

char ch[36];
memset(ch, 0, 36);
uuid_unparse(t, ch);
string* uuid = new string(ch);


If you need the UUID in linux, use this sample and you will be ready in no time, it works flawless and does not depend on anything else than linux to run.

it's easy right? ok here's how I tried to achieve the same thing in Windows:


UUID uuid;
if (UuidCreate(&uuid) == 0) {
unsigned char* wszUuid = NULL;
UuidToString(&uuid, &wszUuid);
if (wszUuid != NULL) {
string* res = new string((char*)wszUuid);
qDebug(res->c_str());
return res;
} else {
return new string("");
}
}


That's the example I found in microsoft page, and some other blogs, but for some freaking reason the code does not compile... the error I'm getting is:

cannot convert `char**' to `short unsigned int**' for argument `2' to `RPC_STATUS UuidToString(UUID*, short unsigned int**)'

even when the signature is: "UuidToString(UUID*,unsigned char**)", (I know I'm not the smartest guy, but if the header says that the parameter is "unsigned char" why is keeping generating the error?), so after a few worthless hours looking around on internet and changing the definition to "short unsigned int*" and getting a Universally Unique Code with one letter... (yes... only 1 letter in the result) I changed my code to this:


QUuid uuid = QUuid::createUuid();
QString suuid = uuid.toString();

string res = suuid.toStdString();

// removes the { } characters
res = res.substr(1, res.size() - 2);
return new string(res);


And now is working in linux and windows, thanks to Qt project!

Btw, I hate to spend a lot of time looking for some simple feature, when you can use that time to create a full application from scratch.

Thursday, January 21, 2010

Applying a patch with "git am"

I decided to write this because I hate losing hours in a trivial work and found no information to be supported on.

The git tutorial states:

================================================================================================
Importing patches to a project

Git also provides a tool called git-am(1) (am stands for "apply mailbox"), for importing such an emailed series of patches. Just save all of the patch-containing messages, in order, into a single mailbox file, say "patches.mbox", then run

$ git am -3 patches.mbox

Git will apply each patch in order; if any conflicts are found, it will stop, and you can fix the conflicts as described in "Resolving a merge". (The "-3" option tells git to perform a merge; if you would prefer it just to abort and leave your tree and index untouched, you may omit that option.)

Once the index is updated with the results of the conflict resolution, instead of creating a new commit, just run

$ git am --resolved

and git will create the commit for you and continue applying the remaining patches from the mailbox.
================================================================================================

But... what will happened if you get the message:

Failed to merge in the changes.
Patch failed at 0001 Implementacion de excepciones en SOA SaveEntities
When you have resolved this problem run "git am -3 --resolved".
If you would prefer to skip this patch, instead run "git am -3 --skip".
To restore the original branch and stop patching run "git am -3 --abort".

Ok, I lost 3 hours of my beloved time trying to solve this problem, everytime I did the "git mergetool" and solved the problem the command "git am -3 --resolved" show me this:

Applying: Implementacion de excepciones en SOA SaveEntities
No changes - did you forget to use 'git add'?

I was like... WTF? I tried to do a "git status" and nothing was in "unstaged" mode, I tried to add the damn file, and nothing... --abort, reset --hard... all the process again and... nothing!

That was frustrating, but I finally found a solution for this... the problem was with the "git mergetool", this tool does an "Autocommit" and, I don't know why, this does not include the automerged file, what I did?

git am --3way PATHFILE
solve the problem manually with gedit (any editor will do the trick)
git add the fixed files
git am --resolved

and that worked.

I hope this could save some hours to someone else... if it does, let me know... it's highly appreciated when you know that you didn't lose your time writing stuff and nobody read it xD

Thursday, January 7, 2010

pthread_create memory leak?

After a hard work cleaning all the possible memory leaks in my application, I got stuck at the following error:


==24544== 144 bytes in 1 blocks are possibly lost in loss record 1 of 1
==24544== at 0x4023F5B: calloc (vg_replace_malloc.c:418)
==24544== by 0x40109AB: _dl_allocate_tls (dl-tls.c:300)
==24544== by 0x451F102: pthread_create@@GLIBC_2.1 (allocatestack.c:561)
==24544== by 0x451F5E7: pthread_create@GLIBC_2.0 (pthread_create.c:593)
==24544== by 0x424BC3C: Thread::start(void*) (threads.cpp:14)
==24544== by 0x425BB3F: startSocketListener(void*) (networkservice.cpp:161)
==24544== by 0x451E80D: start_thread (pthread_create.c:300)
==24544== by 0x44A17ED: clone (clone.S:130)



I double checked the code and everything works as intented, but the error was still there, some people wrote that this is not a "real" memory leak, because the pthread_create will reuse the allocated memory, but I was willing to get rid of this error, and get the message: "All heap blocks were freed -- no leaks are possible"

After googling I found that you could solve this adding the pthread_detach in order to free the used memory, so here's what I did:



Thread.cpp:

Thread::Thread(void *(*run)(void* arg)) {
runFunction = run;
}

void Thread::start(void* arg) {
int rc = pthread_create(&internal, NULL, runFunction, (void*)arg);
if (rc) {
cout << "Error creating the thread" << endl;
}
}

void Thread::join() {
pthread_join(internal, NULL);
}

Thread::~Thread() {
pthread_detach(internal);
}



Thread.h

class Thread {
private:
pthread_t internal;

public:
void *(*runFunction)(void* arg);

Thread(void *(*run)(void* arg));

~Thread();

void start(void* arg);

void join();
};



As you may noticed I left the detach call at the destructor, and now I don't have a single memory leak to be blame for.