Showing posts with label boost. Show all posts
Showing posts with label boost. Show all posts

Shared pointers hell

I just fixed a problem in my program which was causing a lot of runtime exceptions thanks to my ignorance on how to use the boost::shared_ptr class and I think is important to prevent people of going through the same. When I created or reset a shared pointer using the contents of other pointer this is what I did:

shared_ptr source(new int);
*source = 3;
// here begin my problems
shared_ptr
destination(source.get());
destination.reset(source.get());

So, what's wrong with the code here? Well, basically that in this way, since I was using the raw pointers, the counter (which is used to free the memory) is not updated which would lead to a double call to the destructor of the objects (which was exactly my problem). The right way of doing that is:

shared_ptr source(new int);
*source = 3;
shared_ptr
destination = source.get();
destination = source;

In this way, the pointer counters are updated and no problems occur while running the program, since the operator = is properly overrided in the class.

Wyld is alive and parsing

The last few days have been a mixture of failure and success. Failure in the sense that I couldn't give as much time to my project as I would have liked to. This because I had to work this week and yesterday was my gf birthday so I came to bed really late and woke up really late; on top of it, it was so hot that I couldn't concentrate.

The success story is much more interesting. At the beginning I was thinking I had to write the parser from scratch. I already made a parser once for a very simple calculator and I know how such a pain in the ass that could be. However, at some point I saw some libraries that help you to produce parsers. Finally, in order to reduce the program libraries needs I decided to use boost::spirit. The first day I tried to use the version 2.0 of the library but it stills in beta but the documentation seems to be in pre alpha. I liked it a lot, but I faced a problem which couldn't be solved with the documentation, therefore I decided to go back to the classic stable version. It was not as nice designed as the 2.0 but it worked and it took me less than one day to have a test application running and another half day to add the parser to the trunk of the application. I was really simple to define the grammar, the operators are overload in a way that is almost like defining a mathematical grammar. I really recommend using spirit when you have to write a parser. So yes, now we have a parser of a small prolog language which I built modifying a C parser example. The nicest part of all is that it is a header only library so you don't need to add more linker requirements (although it the compilation is really slower).

I've been also relearning a lot of C++ and a lot of quirks that I don't like and that I forgot completely about. Specially in the behavior of objects attributes and the sort. Well, it is nice to see one of the modules already working and not having to worry about it again.

I also decided to do some other modifications to the architecture of the application. I added a package common which will contain the structures of the logic programs. I also decided to create a new module, which would be the most reusable, it is called garou and it is the one in charge of the neural networks. At the beginning I won't include any king of backpropagation training but I think I will do it some time in the future. Garou will be loosely based in the neural network implementation of weka. Curiously enough, this will be the next module I will work on, starting right now. So, I leave now to work on it.