<commit_before>//=============================================================================
//File Name: Kinect.cpp
//Description: Manages interface with a Microsoft Kinect connected to the USB
//             port of the computer
//Author: <NAME>
//=============================================================================

#include <cstring>
#include <cstdlib>
#include "Kinect.hpp"
#include <SFML/Graphics/Image.hpp>

#include <iostream> // TODO Remove me

#include <opencv2/core/core_c.h>
#include <opencv2/imgproc/imgproc_c.h>
#include <opencv2/highgui/highgui_c.h>

Kinect::Kinect() : m_vidImage( NULL ) , m_depthImage( NULL ) {
    m_kinect = knt_init();

    if ( m_kinect != NULL ) {
        m_kinect->rgb->newframe = newVideoFrame;
        m_kinect->rgb->callbackarg = this;

        m_kinect->depth->newframe = newDepthFrame;
        m_kinect->depth->callbackarg = this;
    }

    CvSize imageSize = { static_cast<int>(ImageVars::width) , static_cast<int>(ImageVars::height) };
    m_cvVidImage = cvCreateImage( imageSize , 8 , 4 );
    m_cvVidImage->imageData = static_cast<char*>( std::malloc( ImageVars::width * ImageVars::height * 4 ) );

    m_cvDepthImage = cvCreateImage( imageSize , 8 , 4 );
    m_cvDepthImage->imageData = static_cast<char*>( std::malloc( ImageVars::width * ImageVars::height * 4 ) );

    m_redPart = cvCreateImage( imageSize , 8 , 1 );
    m_greenPart = cvCreateImage( imageSize , 8 , 1 );
    m_bluePart = cvCreateImage( imageSize , 8 , 1 );

    m_channelAnd = cvCreateImage( imageSize , 8 , 1 );

    m_imageAnd = cvCreateImage( imageSize , 8 , 4 );

    m_enabledColors = 0x00;

    for ( unsigned int index = 0 ; index < ProcColor::Size ; index++ ) {
        m_calibImages.push_back( new IplImage );
        m_calibImages.at( m_calibImages.size() - 1 ) = cvCreateImage( imageSize , 8 , 4 );
    }
}

Kinect::~Kinect() {
    stopVideoStream();
    stopDepthStream();

    if ( m_kinect != NULL ) {
        knt_destroy( m_kinect );

        // wait for Kinect thread to close before destroying instance
        while ( m_kinect->threadrunning == 1 ) {
            Sleep( 100 );
        }

        m_kinect = NULL;
    }

    m_vidImageMutex.lock();
    std::free( m_cvVidImage->imageData );
    m_vidImageMutex.unlock();<commit_msg>Fixed memory leak in Kinect class destructor<commit_after>//=============================================================================
//File Name: Kinect.cpp
//Description: Manages interface with a Microsoft Kinect connected to the USB
//             port of the computer
//Author: <NAME>
//=============================================================================

#include <cstring>
#include <cstdlib>
#include "Kinect.hpp"
#include <SFML/Graphics/Image.hpp>

#include <iostream> // TODO Remove me

#include <opencv2/core/core_c.h>
#include <opencv2/imgproc/imgproc_c.h>
#include <opencv2/highgui/highgui_c.h>

Kinect::Kinect() : m_vidImage( NULL ) , m_depthImage( NULL ) {
    m_kinect = knt_init();

    if ( m_kinect != NULL ) {
        m_kinect->rgb->newframe = newVideoFrame;
        m_kinect->rgb->callbackarg = this;

        m_kinect->depth->newframe = newDepthFrame;
        m_kinect->depth->callbackarg = this;
    }

    CvSize imageSize = { static_cast<int>(ImageVars::width) , static_cast<int>(ImageVars::height) };
    m_cvVidImage = cvCreateImage( imageSize , 8 , 4 );
    m_cvVidImage->imageData = static_cast<char*>( std::malloc( ImageVars::width * ImageVars::height * 4 ) );

    m_cvDepthImage = cvCreateImage( imageSize , 8 , 4 );
    m_cvDepthImage->imageData = static_cast<char*>( std::malloc( ImageVars::width * ImageVars::height * 4 ) );

    m_redPart = cvCreateImage( imageSize , 8 , 1 );
    m_greenPart = cvCreateImage( imageSize , 8 , 1 );
    m_bluePart = cvCreateImage( imageSize , 8 , 1 );

    m_channelAnd = cvCreateImage( imageSize , 8 , 1 );

    m_imageAnd = cvCreateImage( imageSize , 8 , 4 );

    m_enabledColors = 0x00;

    for ( unsigned int index = 0 ; index < ProcColor::Size ; index++ ) {
        m_calibImages.push_back( new IplImage );
        m_calibImages.at( m_calibImages.size() - 1 ) = cvCreateImage( imageSize , 8 , 4 );
    }
}

Kinect::~Kinect() {
    stopVideoStream();
    stopDepthStream();

    if ( m_kinect != NULL ) {
        knt_destroy( m_kinect );

        // wait for Kinect thread to close before destroying instance
        while ( m_kinect->threadrunning == 1 ) {
            Sleep( 100 );
        }

        m_kinect = NULL;
    }

    if ( m_vidImage != NULL ) {
        DeleteObject( m_vidImage );
    }

    if ( m_depthImage != NULL ) {
        DeleteObject( m_depthImage );
    }

    m_vidImageMutex.lock();
    std::free( m_cvVidImage->imageData );
    m_vidImageMutex.unlock();
{'fraction_non_alphanumeric': 0.18875078997261427, 'fraction_numerical': 0.012639561828523278, 'mean_word_length': 3.31244323342416, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5850454', 'n_tokens_mistral': 1634, 'n_tokens_neox': 1553, 'n_words': 329}