Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Code Analysis tools  (Read 3108 times)

0 Members and 1 Guest are viewing this topic.

Raphman

  • Newbie
  • *
  • Posts: 40
    • View Profile
    • Email
Code Analysis tools
« on: April 05, 2013, 01:12:27 am »
Hey there SFML Community!

I'm developing a J-Styled RPG, and I want to run some Static/Dynamic Analysis on my code.

Now I tried using  pretty much every free tool listed here:
http://stackoverflow.com/questions/413477/is-there-a-good-valgrind-substitute-for-windows

And they all didn't work. They all failed to various extents. Hell AMD Code Analyst screwed up my registry so badly I had to do a system restore! Otherwise It'd tell me my images couldn't load.

Currently I'm using VS 2010 professional C++. I switch between an x64 Win8 machine and an x64 Win7 machine for development. I tried doing a search on the forums, but I turned up pretty much nothing.

I would appreciate any help! Thanks in advance!
« Last Edit: April 05, 2013, 01:13:59 am by Raphman »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Code Analysis tools
« Reply #1 on: April 05, 2013, 01:42:13 am »
What do you exactly want to analyze?

AMD code analyst is a profiler, while valgrind controls the memory. Concerning memory leaks, there's no need to check them if you consequently use RAII ;)

Visual Studio also comes with a profiler, but you have to install it separately in the Professional edition, see here.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Raphman

  • Newbie
  • *
  • Posts: 40
    • View Profile
    • Email
Re: Code Analysis tools
« Reply #2 on: April 05, 2013, 01:49:13 am »
I mean, I would like to check memory leaks, since I do use pointers. I can't help it, they're helpful. Although I don't often use the new command, and when I do I am sure to delete 'em. My main worry is CPU usage though.

I'll check out that profiler, thanks! I hope it doesn't only apply to managed code though!
« Last Edit: April 05, 2013, 01:55:18 am by Raphman »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Code Analysis tools
« Reply #3 on: April 05, 2013, 02:00:37 am »
I mean, I would like to check memory leaks, since I use do use pointers. I can't help it, they're helpful.
Of course, I also use them. But you should not use raw pointers that own objects and therefore imply manual memory management, since it's inherently error-prone and there are easy-to-use alternatives.

Take a look at my link in the last post, it shows nicely how to replace low-level techniques with modern alternatives. The RAII (resource acquisition is initialization) idiom means that every resource, including memory, is contained by an object that is responsible for its construction and destruction. That is, you don't explicitly need to destroy or deallocate objects.
// Manual resource management
MyClass* p = new MyClass;
delete p;

sf::Mutex mutex;
mutex.lock();
mutex.unlock();

FILE* f = fopen(...);
fclose(f);
// RAII - everything is cleaned up automatically at scope exit
std::unique_ptr<MyClass> p(new MyClass);
// calls delete

sf::Mutex mutex;
sf::Lock lock(mutex);
// calls unlock()

std::fstream f(...);
// calls close()

The great advantage is that cleanup is guaranteed, independent of how complex your code is, how many return paths you use and how many exceptions are thrown. You can also take a look at another example I wrote some time ago. It shows the limits of manual memory management and the advantages of RAII. I can not recommend enough to take a look at modern C++ idioms, it will greatly improve your code!
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Raphman

  • Newbie
  • *
  • Posts: 40
    • View Profile
    • Email
Re: Code Analysis tools
« Reply #4 on: April 05, 2013, 02:07:25 am »
Ah I see what you mean. Maybe I'll attempt to spend the  enormous amount of time to refactor my code to implement RAII  :P.
 I appreciate the input a lot, thank you.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Code Analysis tools
« Reply #5 on: April 05, 2013, 02:23:48 am »
Might be worth it... and you should definitely use this practice for new projects ;)

For the refactoring of T* to unique_ptr<T>, there are not that many modifications necessary. The following operations are still defined:
*ptr
ptr->member
if (ptr)
if (!ptr)
ptr = nullptr; // C++11, use nullptr instead of NULL or 0

New operations are mainly:
ptr = new T; // ->
ptr.reset(new T);

delete ptr; // ->
/* nothing */

T* passivePtr = ptr; // ->
T* passivePtr = ptr.get();

Note that you can still have other pointers to the unique pointer (as shown in the last line), it is just the unique_ptr which has ownership of the object. That is, the object is destroyed as soon as the unique_ptr is destroyed.

But smart pointers are not the only technique to avoid mistakes, other important classes are std::vector (instead of new[] and delete[]), std::array (instead of arrays) and std::string (instead of char*).
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Raphman

  • Newbie
  • *
  • Posts: 40
    • View Profile
    • Email
Re: Code Analysis tools
« Reply #6 on: April 05, 2013, 02:48:01 am »
Once again, I thank you for your input.