Category Archives: Programming languages

Numeric promotions and conversions in C++

In the following C++ code the values of ‘z’ and ‘n’ are undefined, because they are the result of an operation with signed integer arithmetic overflow (‘x’ and ‘y’ are first implicitly converted to signed int). The value of ‘w’ is implementation defined, because it is the result of a conversion:

#include <iostream>
#include <bitset>

int main(int argc, char *argv[])
{
    unsigned short x = 65535, y = x;
    unsigned short z = x * y;
    unsigned int n = x * y;
    std::cerr << "z = " << std::bitset<16>(z) << ", n = " << std::bitset<32>(n) << ", sizeof(int) = " << sizeof(int) << std::endl;

    short w = 0x80000000;
    
    return 0;
}

see Numeric conversions section of Implicit conversions article.

Comparison of std::mutex and std::atomic performance

The following C++ code compares the performance of std::atomic and std::mutex:

#include <atomic>
#include <mutex>
#include <iostream>
#include <chrono>
#include <thread>

const size_t size = 100000000;
std::mutex mutex;
bool var = false;

typedef std::chrono::high_resolution_clock Clock;

void testA()
{
    std::atomic<bool> sync(true);
    const auto start_time = Clock::now();
    for (size_t counter = 0; counter < size; counter++)
    {
        var = sync.load();
        //sync.store(true);
        //sync.exchange(true);
    }
    const auto end_time = Clock::now();
    std::cout << 1e-6*std::chrono::duration_cast<std::chrono::microseconds>(end_time - start_time).count() << " s\n";
}

(more…)

Detecting memory leaks of C++ application in Ubuntu

First, I tried Valgrind tool using the following command:

valgrind --tool=memcheck --leak-check=yes ./app

With some large QT application started for some short period I got the following output:

==7090== HEAP SUMMARY:
==7090==     in use at exit: 5,623,365 bytes in 36,268 blocks
==7090==   total heap usage: 32,454,680 allocs, 32,418,412 frees, 12,822,939,874 bytes allocated
................................
==7090== LEAK SUMMARY:
==7090== definitely lost: 20,163 bytes in 74 blocks
==7090== indirectly lost: 60,053 bytes in 1,273 blocks
==7090== possibly lost: 396,167 bytes in 2,169 blocks
==7090== still reachable: 4,834,822 bytes in 31,576 blocks
==7090== suppressed: 0 bytes in 0 blocks
==7090== Reachable blocks (those to which a pointer was found) are not shown.
==7090== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==7090==
==7090== For counts of detected and suppressed errors, rerun with: -v
==7090== Use --track-origins=yes to see where uninitialised values come from
==7090== ERROR SUMMARY: 20905 errors from 1583 contexts (suppressed: 15 from 2)

When I left this app running for the night (approximately 16 hours), I got the following summary:

==3816== HEAP SUMMARY:
==3816==     in use at exit: 7,746,701 bytes in 36,248 blocks
==3816==   total heap usage: 827,800,342 allocs, 827,764,094 frees, 105,404,761,516 bytes allocated
..................................
==3816== LEAK SUMMARY:
==3816== definitely lost: 19,835 bytes in 38 blocks
==3816== indirectly lost: 59,805 bytes in 1,237 blocks
==3816== possibly lost: 396,167 bytes in 2,169 blocks
==3816== still reachable: 6,958,734 bytes in 31,628 blocks
==3816== suppressed: 0 bytes in 0 blocks
==3816== Reachable blocks (those to which a pointer was found) are not shown.
==3816== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==3816==
==3816== For counts of detected and suppressed errors, rerun with: -v
==3816== Use --track-origins=yes to see where uninitialised values come from
==3816== ERROR SUMMARY: 13022 errors from 1574 contexts (suppressed: 10 from 2)

It means that the app leaks about 0.126 MB/hour ( (6958734 – 4834822) / 16 / 1024.0 / 1024.0) and totally for the night 2.02 MB, and probably it is not a leak, because the app has pointers to the allocated memory (it is reachable), but it does not clean exit.

To figure out how it works I built the following trivial program:

int main()
{
  int *a = new int[100];
}

with debug information and started the tool:

g++ -g -o ex ex.cpp
valgrind --tool=memcheck --leak-check=yes ./ex

In the output the tool shows line number 3 where memory leak is occurred:

==22042== Memcheck, a memory error detector
==22042== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==22042== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info
==22042== Command: ./ex
==22042==
==22042==
==22042== HEAP SUMMARY:
==22042==     in use at exit: 73,104 bytes in 2 blocks
==22042==   total heap usage: 2 allocs, 0 frees, 73,104 bytes allocated
==22042==
==22042== 400 bytes in 1 blocks are definitely lost in loss record 1 of 2
==22042==    at 0x4C2B800: operator new[](unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==22042==    by 0x40061B: main (ex.cpp:3)
==22042==
==22042== LEAK SUMMARY:
==22042==    definitely lost: 400 bytes in 1 blocks
==22042==    indirectly lost: 0 bytes in 0 blocks
==22042==      possibly lost: 0 bytes in 0 blocks
==22042==    still reachable: 72,704 bytes in 1 blocks
==22042==         suppressed: 0 bytes in 0 blocks
==22042== Reachable blocks (those to which a pointer was found) are not shown.
==22042== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==22042==
==22042== For counts of detected and suppressed errors, rerun with: -v
==22042== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

Links:

  1. How I can detect memory leaks of C++ application in Linux (Ubuntu OS)?
  2. How to check for memory leaks in a large scale c++ Linux application?
  3. Still Reachable Leak detected by Valgrind
  4. With Memcheck’s memory leak detector, what’s the difference between “definitely lost”, “indirectly lost”, “possibly lost”, “still reachable”, and “suppressed”?
  5. Valgrind: can possibly lost be treated as definitely lost?

Another alternative is calling mallinfo() function in C++ code. The following code gets something like total heap size of the process:

int usedmem = mallinfo().uordblks;

Using this function I wrote a QT widget that shows memory usage in application status bar:

#include <QLabel>
#include <malloc.h>

class MemoryStatusWidget : public QLabel
{
public:
    MemoryStatusWidget(QWidget *parent = nullptr);

public:
    void timerEvent(QTimerEvent *) override;
};

MemoryStatusWidget::MemoryStatusWidget(QWidget* parent)
    : QLabel(parent)

{
    startTimer(1000);
}

void MemoryStatusWidget::timerEvent(QTimerEvent *)
{
    int usedmem = mallinfo().uordblks;

    setText(tr("Memory Usage:%1 MB").arg(QString::number((double)usedmem / (1024 * 1024), 'f', 3)));
}

Detecting leaks on Windows

Just in case if you need to detect leaks on Windows (where Valgrind doesn’t work and will never work by design), give Deleaker a try. By the way it can be integrated with Qt Creator, see video on YouTube.

QML DropShadow is very slow

QML DropShadow is an interesting effect that acts in a very simple way. It works fine in my application and produces the following result:

QML DropShadow is very slow

The only disadvantage of DropShadow effect is that is slows down my application from 60 FPS to 30 FPS on Android Phone. The following code demonstrates how I use it with StackView:

(more…)

Lines 3D application structure (Windows Store version)

Lines 3D game is a UWP application based on “XAML App for OpenGL ES (Universal Windows)VS2015 project template (written in C++/VS2015 using OpenGL ES 2.0 and elements of OpenGL 3.0). You can install Lines 3D  from Windows Store and play for free, or at least see the game screenshots.

Main components

Game logic and OpenGL rendering engine in Lines 3D are cross-platform. Their code uses STL, OpenGL and abstract C++ interfaces for doing the following tasks:

  • Loading sounds from wav files and playing them with different speed and volume.
  • Loading textures from PNG images (this code uses Windows API, but probably it can be made cross-platform).
  • Logging game events, such as “game over” to the Windows Store. They used to collect statistics on what game levels the users play and what score they get. The possible application crashes (unhandled exceptions and memory failures) and internal errors (like file not found, etc.) are also logged to the Windows Store.
  • Accessing application installation path and application data path in the file system.

All the graphic controls, including the main windows, application bar (main menu), dialogs, message boxes and advertising are written using XAML and Windows-specific code.

(more…)

Drawing a transparent image with OpenGL ES in a UWP XAML app

In my previous post Testing XAML App for OpenGL ES on Windows 10 Mobile Device I described the changes I made to UWP application based on “XAML App for OpenGL ES (Universal Windows)” template to demonstrate some strange effect related to the transparency of the image drawn with OpenGL ES in SwapChainPanel. But I did yet another experiment with this application and got some beautiful pictures that demonstrate what happens if I make the scene completely transparent with the following code:

void SimpleRenderer::Draw()
{
    glEnable(GL_DEPTH_TEST);
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    ...
}

(more…)

Testing XAML App for OpenGL ES on Windows 10 Mobile Device

Today I played a bit with a UWP application based on “XAML App for OpenGL ES (Universal Windows)” template and realized that there is some specific bug probably related to the interaction between SwapChainPanel and OpenGL surface. First, I made the cube transparent by changing two lines of code in the vertex shader:

const std::string vs = STRING
(
    uniform mat4 uModelMatrix;
    uniform mat4 uViewMatrix;
    uniform mat4 uProjMatrix;
    attribute vec4 aPosition;
    attribute vec3 aColor;
    varying vec4 vColor;
    void main()
    {
        gl_Position = uProjMatrix * uViewMatrix * uModelMatrix * aPosition;
        vColor = vec4(aColor, 0.1);
    }
);

(more…)

How to compile QT with VS2015 and GCC

Below I provided a simple step by step instruction on how to compile QT 5.7 with VS2015 assuming you already have VS2015 and Git client installed on your Windows machine.

Install Perl, Python and Ruby.

To get QT 5.7 sources open Git Bash and run the following command (the repository has some submodules, so “recursive” option is required), see the list of possible clone here at the bottom of the page:

git clone --recursive https://github.com/qtproject/qt5.git --branch 5.7

Create a bat file called configureqt.bat with the following content:

set PATH=%PATH%;"C:\Program Files (x86)\Portable\ruby-2.3.0-i386-mingw32\bin";C:\Perl\bin;C:\Python27
D:
cd D:\Repos\qt5\
set _ROOT=D:\Repos\qt5
set PATH=%_ROOT%\qtbase\bin;%_ROOT%\gnuwin32\bin;%PATH%
set QMAKESPEC=win32-msvc2015
set _ROOT=
configure -debug -nomake examples -opensource

(more…)

Using OpenGL 3.0 with MSOpenTech ANGLE

Typically ANGLE library is used with OpenGL 2.0, but I successfully tried to enable OpenGL 3.0:

const EGLint contextAttributes[] = 
{ 
    EGL_CONTEXT_CLIENT_VERSION, 3, 
    EGL_NONE
};

and used some OpenGL 3.0 features in my Universal Windows App. But today I tried to compile my application with the new version of ANGLE library and got EGL_BAD_CONFIG error while creating the OpenGL context. The source code that returns this error checks some EGL_OPENGL_ES3_BIT_KHR that is not set in the new version:

if (clientMajorVersion == 3 && !(configuration->conformant & EGL_OPENGL_ES3_BIT_KHR))
{
    return Error(EGL_BAD_CONFIG);
}

(more…)

Debugging a C++ application on an Android device with VS2015 on Windows 10

VS2015 has an exciting ability to debug a C++ application on Android Emulator, but in this article I will talk about no less exciting and more time expensive ability to debug a C++ application on a real Android device. The first thing we need to spend the time with is figuring out how to enable USB debugging mode on our Android device. On my ASUS Zenfone I need to go to Settings->About->Software Information and tap on Build Number 7 times, after that I have USB debugging check box in Settings->Developer Options that I should tap as well:

enabling USB debugging mode on Android device USB debugging mode on Android device

(more…)