RAII(Resource Acquisition Is Initialization)


Resource acquisition is initialization (RAII) is a programming idiom used in several object-oriented languages to describe a particular language behavior.

RAII (Resource Acquisition Is Initialization) is one of the famous design patterns.
The technique was developed for exception-safe resource management in C++ during 198489, primarily by Bjarne Stroustrup and Andrew Koenig.

RAII patterns are important techniques for preventing leakage in languages such as C++ that require developers to directly manage resources.


The following C++11 example demonstrates usage of RAII for file access and mutex locking:

#include <fstream>
#include <iostream>
#include <mutex>
#include <stdexcept>
#include <string>
void WriteToFile(const std::string& message) {
  // |mutex| is to protect access to |file| (which is shared across threads).
  static std::mutex mutex;
// Lock |mutex| before accessing |file|.
  std::lock_guard<std::mutex> lock(mutex);
// Try to open file.
  std::ofstream file("example.txt");
  if (!file.is_open()) {
    throw std::runtime_error("unable to open file");
  }
// Write |message| to |file|.
  file << message << std::endl;
// |file| will be closed first when leaving scope (regardless of exception)
  // mutex will be unlocked second (from lock destructor) when leaving scope
  // (regardless of exception).
}



<Ref : https://en.wikipedia.org/wiki/Resource_acquisition_is_initialization>

#RAII #Resource Acquisition Is Initialization #C++ #mutex #Memory leakage #std::auto_ptr

How to install docker in Ubuntu linux system


How to install docker in Ubuntu linux system

Install Docker
$ curl -fsSL https://get.docker.com/ | sudo sh

Install Docker Compose

Docker Compose Permission
$ sudo chmod 777 /usr/local/bin/docker-compose


Command (  ${xxxx} <- input your info )

Root permission
$ sudo usermod -aG docker <NAME>


Docker Image Command

Download Image
docker pull ${IMAGE:Version}
$ docker pull ubuntu:latest

Run Docker Image
docker run ${IMAGE:Version}
$ docker run ubuntu:latest

Build Docker Image
$ docker build

Show Downloaded Docker Image
$ docker images

Remove Docker Image
$ docker rmi ${IMAGE:Version}

Remove All Docker Image
$ docker rmi ${docker images -q}

Show Running Docker Image(Container) List
$ docker ps

Restart Docker image
$ docker restart




Container Command

Stop Running Container
docker stop ${CONTAINER ID}
$ docker stop ${CONTAINER ID}

Remove Container
$ docker rm <CONTAINER ID>

Remove All Container
$ docker rm $(docker ps -a -q)

Show Detailed Container Infomation
$ docker container inspect <CONTAINER ID>

Show Container volume
$ docker volume ls




#docker #linux docker #install docker

Signal Handling Example (linux C/C++)


Signal Handling Example (linux C/C++)

#include <iostream>
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <fstream>

class TestClass{
public:
    static void handle_signal(int signal);
};
void TestClass::handle_signal(int signal) {
    //char *signalName;
    std::string signalName = "";
    sigset_t pending;
    switch(signal) {
        case SIGHUP:
            signalName = "SIGHUP";
            break;
        case SIGINT:
            signalName = "SIGINT";
            break;
        case SIGTERM:
            signalName = "SIGTERM";
            break;
        case SIGQUIT:
            signalName = "SIGQUIT";
            break;
        case SIGKILL:
            signalName = "SIGKILL";
            break;
        default:
            printf("Caught unknown signal: %d\n", signal);
            return;
    }
    //실행하고 싶은 함수        
    std::ofstream outFile("./output.txt"std::ios::app);
    printf("Linux signal occurred. signal[%d] : name[%s]\n", signal, signalName.c_str());
    std::string str(std::string("Linux signal occurred. signal[") + std::to_string(signal) + "] signal Name["std::string(signalName) + "]");
    outFile << str << std::endl;
    outFile.close();
    //exit(0);
}
int main(int argcchar* arv[]) {
    TestClass *A = new TestClass();
    struct sigaction signalhandler;
    //stack_t sigstk; //for seg.fault
    //if ((sigstk.ss_sp = (char *)//malloc(SIGSTKSZ*2))==NULL) {
    //fprintf(stderr, "can't alloc alt stack\n");
    //}
    //sigstk.ss_size = SIGSTKSZ*2;
    //sigstk.ss_flags = 0;
    //if(sigaltstack(&sigstk, (stack_t *) 0) < 0) {
    //perror("sigaltstack");
    //}
    signalhandler.sa_handler = &TestClass::handle_signal;
    sigemptyset(&signalhandler.sa_mask);
    signalhandler.sa_flags = SA_RESETHAND | SA_ONSTACK;
    sigaction(SIGSEGV, &signalhandler,NULL);
    sigaction(SIGHUP, &signalhandler,NULL);
    sigaction(SIGINT, &signalhandler,NULL);
    sigaction(SIGTERM, &signalhandler,NULL);
    sigaction(SIGQUIT, &signalhandler,NULL);
    sigaction(SIGKILL, &signalhandler,NULL);

    while(1) {
    }

    return 1;
}



#Signal #Signal Handling #Signal Handler #C #C++ #Linux #Embedded

java.net.SocketException: socket failed: EACCES (Permission denied)

App.manifests.Androidmanifest.xml

error:
java.net.SocketException: socket failed: EACCES (Permission denied)

solve:
add : Manifest.xml file

<uses-permissionandroid:name="android.permission.INTERNET"


example:

<?xmlversion="1.0"encoding="utf-8"?>
<manifestxmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.androidclient">

<uses-permissionandroid:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activityandroid:name=".MainActivity">
<intent-filter>
<actionandroid:name="android.intent.action.MAIN"/>

<categoryandroid:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>

</manifest>



#Android #Java #Permission denied

pure virtual function call (C/C++)

armv7 toolchain Error

error message:
pure virtual function call

solve:
1. Occurs when calling a virtual function from a constructor or destructor.
2. Compiler option did not write -march==armv7-a.

example :
arm-linux-gnueabi-g++ -fno-omit-frame-pointer -march=armv7-a -funwind-tables

#toolchain #embedded #C #C++

Terminate called out an active extension / Aborted (C/C++)


You can build it, but when you run it,
Execute and die when an error occurs.

Error Message:
Terminate called out an active extension
Aborted


Problem
Occurs when coding using "std::thread"

Cause / Solution
When coding using "std::thread", it is caused by not inserting "join"


Example Code:
class TEST {
TEST();
~TEST();

void init();
void end();
std::shared_ptr<std::thread> th = nullptr;

void exec(){}
}

void TEST::init() {
    th = std::make_shared<std::thread>(&TEST::exec , this);
}
void TEST::end() {
if(th != nullptr) {
th->join();
th = nullptr;
}
}

int main() {
Test *t = new TEST();
t->init();

if( t != nullptr ) {
delete t;
t = nullptr;
}
// If this program ends here, an error will occur.

//add code
t->end();
if( t != nullptr ) {
delete t;
t = nullptr;
}
//solved code !
}




#linux #embedded #C #C++ #debug #error #thread #join

wstring to string / string to wstring / utf16 to utf8 / utf8 to utf16(C/C++)

wstring to string / string to wstring

for utf16 parsing error


#include <locale>
#include <codecvt>
#include <string>

std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
std::string Str_UTF8 = converter.to_bytes(utf16String);
std::wstring wideString_UTF16 = converter.from_bytes(utf8String);

For use, you should modify the bold writing.

#utf16 #utf8 #wstring #string #C #C++ #Embedded #Linux

Apartment Buying Guide 2025: Shocking Red Flags You Should NEVER Ignore!

 🏙️ Apartment Buying Guide 2025: Shocking Red Flags You Should NEVER Ignore! 🚨 Are you thinking about buying an apartment in 2025? 🏢  It’...