use gstreamer pipeline to init opencv's videocapture, has memory leak when Repeatedly reset videocapture - memory-leaks

environment:
Jetson Xavier NX
Jetpack 4.6
NVIDIA® Jetson™ Linux Driver Package (L4T) 32.6.1
gstreamer1.14.5
opencv4.1.1
code:
int CMyServer::open_camera() {
string zstr = "rtspsrc location=rtsp://........ ! rtph264depay ! h264parse ! nvv4l2decoder ! nvvidconv ! video/x-raw, width=(int)1920, height=(int)1080, format=(string)BGRx ! videoconvert ! appsink";
if (!video_cap_) {
try {
// video_cap_ = make_unique<VideoCapture>(zstr, cv::CAP_GSTREAMER);
video_cap_.reset(new VideoCapture(zstr, cv::CAP_GSTREAMER));
LOGF(INFO, "\n reset video_cap_");
} catch (...) {
cerr << "error in opening camera\n" << flush;
LOGF(WARNING, "\n error in reseting video_cap_ first time");
}
}
int wnum = 10;
while (!video_cap_->isOpened() && wnum >= 0) {
try {
wnum--;
video_cap_->open(zstr, cv::CAP_GSTREAMER);
} catch (...) {
cerr << "error in opening camera\n" << flush;
LOGF(WARNING, "\n error in reseting video_cap_ in while() and send camera err msg to client");
break;
}
boost::this_thread::sleep(boost::posix_time::millisec(500));
}
if(!video_cap_->isOpened()||!video_cap_){
return 1;
}
int w = static_cast<int>(video_cap_->get(CAP_PROP_FRAME_WIDTH));
int h = static_cast<int>(video_cap_->get(CAP_PROP_FRAME_HEIGHT));
Size videoSize(1920, 1080);
if (!video_writer_) {
video_writer_.reset(new VideoWriter("..//data//testgst-zhi-1920-30.avi",
VideoWriter::fourcc('M', 'J', 'P', 'G'), 30,
videoSize));
LOGF(INFO, "\n reset video_writer_");
}
if (!video_writer_->isOpened()) {
video_writer_.reset(new VideoWriter("..//data//testgst-zhi-1920-30.avi",
VideoWriter::fourcc('M', 'J', 'P', 'G'), 30,
videoSize));
LOGF(WARNING, "\n error in reseting video_writer_ first time");
}
if (!video_writer_ && !video_writer_->isOpened()) {
cerr << "!!!!!!error: cant open video writer\n" << flush;
LOGF(WARNING, "\n!!!!!!error: cant open video writer");
return 1;
}
if (video_cap_ && video_cap_->isOpened() && video_writer_ && video_writer_->isOpened()) {
cout << "------camera is working\n" << flush;
LOGF(INFO, "\n both video_cap_ and video_writer_ are working");
isCameraOpenedMutex_.lock();
isCameraOpened_ = true;
isCameraOpenedMutex_.unlock();
isCameraStoppedMutex_.lock();
isCameraStopped_ = false;
isCameraStoppedMutex_.unlock();
isCameraReadingStoppedMutex_.lock();
isCameraReadingStopped_ = false;
isCameraReadingStoppedMutex_.unlock();
isCameraWritingStoppedMutex_.lock();
isCameraWritingStopped_ = false;
isCameraWritingStoppedMutex_.unlock();
thread te(&CMyServer::camera_reading_and_enque, this);
te.detach();
LOGF(INFO, "\n starts camera_reading_and_enque() thread");
thread td(&CMyServer::camera_deque_and_writing, this);
td.detach();
LOGF(INFO, "\n starts camera_deque_and_writing() thread");
}
return 0;
}
int CMyServer::close_camera() {
if (video_cap_ && video_cap_->isOpened() && video_writer_ && video_writer_->isOpened()) {
isCameraStoppedMutex_.lock();
isCameraStopped_ = true;
isCameraStoppedMutex_.unlock();
LOGF(INFO, "\n wants to close camera");
bool read_stopped = false, write_stopped = false;
while (1) {
isCameraReadingStoppedMutex_.lock();
if (isCameraReadingStopped_)
read_stopped = true;
isCameraReadingStoppedMutex_.unlock();
isCameraWritingStoppedMutex_.lock();
if (isCameraWritingStopped_)
write_stopped = true;
isCameraWritingStoppedMutex_.unlock();
if (write_stopped && read_stopped) {
// if (read_stopped) {
// video_cap_->release();
// video_writer_->release();
isCameraOpenedMutex_.lock();
isCameraOpened_ = false;
isCameraOpenedMutex_.unlock();
LOGF(INFO, "\n both thread are closed, free video_cap_ and video_writer_ and queue");
cout << "------camera is closed\n" << flush;
break;
}
}
std::queue<Mat>().swap(video_frame_queue_);
video_cap_.reset();
video_writer_.reset();
}
return 0;
}
void MyCamera::camera_reading_and_enque() {
if (!(video_cap_ && video_cap_->isOpened() && video_writer_ && video_writer_->isOpened())) {
LOGF(WARNING, "\n!!!!!! video_cap_ and video_writer_ are not all available in camera_reading_and_enque()");
return;
}
bool isend = false;
Mat frame;
while (1) {
video_cap_->read(frame);
if (!frame.empty()) {
VideoFrameQueueMutex_.lock();
if (video_frame_queue_.size() < maxQueueSize_)
video_frame_queue_.push(frame);
else {
video_frame_queue_.pop();
video_frame_queue_.push(frame);
LOGF(WARNING, "\n video_frame_queue_.size() is > maxQueueSize_");
}
VideoFrameQueueMutex_.unlock();
}
isCameraStoppedMutex_.lock();
if (isCameraStopped_)
isend = true;
isCameraStoppedMutex_.unlock();
if (isend) {
isCameraReadingStoppedMutex_.lock();
isCameraReadingStopped_ = true;
isCameraReadingStoppedMutex_.unlock();
cout << "------end camera reading\n" << flush;
LOGF(INFO, "\n camera_reading_and_enque() thread is stopped");
break;
}
}
}
void MyCamera::camera_deque_and_writing() {
if (!(video_cap_ && video_cap_->isOpened() && video_writer_ && video_writer_->isOpened())) {
LOGF(WARNING, "\n!!!!!! video_cap_ and video_writer_ are not all available in camera_deque_and_writing()");
return;
}
bool isend = false;
Mat frame;
int fram_num = 0, pic_num = 0;
while (1) {
VideoFrameQueueMutex_.lock();
if (video_frame_queue_.size() > 0) {
frame = video_frame_queue_.front();
video_frame_queue_.pop();
} else {
frame.resize(0);
LOGF(WARNING, "\n video_frame_queue_.size() is <= 0");
}
VideoFrameQueueMutex_.unlock();
if (!frame.empty()) {
video_writer_->write(frame);
if (fram_num % 20 == 0) {
stringstream str;
str << "..//test_pic//camera//test//z" << pic_num << ".png";
// str << "..//test_pic//camera//test//z" << pic_num << ".jpg";
//str << "..//test_pic//camera//test//d" << pic_num << ".png";
// str << "..//test_pic//camera//test//d" << pic_num << ".jpg";
cout << str.str() << endl;
pic_num++;
imwrite(str.str(), frame);
LOGF(INFO, "\n write one frame to %s", str.str());
}
fram_num++;
}
isCameraStoppedMutex_.lock();
if (isCameraStopped_)
isend = true;
isCameraStoppedMutex_.unlock();
if (isend) {
fram_num = 0;
isCameraWritingStoppedMutex_.lock();
isCameraWritingStopped_ = true;
isCameraWritingStoppedMutex_.unlock();
cout << "------end camera writing\n" << flush;
LOGF(INFO, "\n------camera_deque_and_writing() thread is stopped");
break;
}
}
}
problem description:
use open_camera() to start save the rtsp steam and jpg pictures.
use close_camera() to stop it.
use two threads to read the frames, decode and save to jpg.
use this gstreamer pipeline for using NVDEC chip on NX to decode video frames.
when repeat this process:
call open_camera(), then call close_camera() after 3 secs,
My code is taking up more and more memory using top to show it.
use function release() of videocapture also has memory leaks.
it comes out that only when videocapture using gstreamer has memory leaks,and only when repeating process:"reset(pipeline),and then reset()" has memory leaks.
videocapture without gstreamer has no leaks.
valgrind shows all memory leaks are in the boost or gstreamer.
thanks for helping!!!
your help really matters!!!

Related

Incorrect checksum for freed object in c++/sfml application

I am trying to create a multiplayer game where players can use voice chat to communicate.
I am using C++ and SFML-3.0 for everything.
Here is the GitHub page for the whole project: https://github.com/iwannabespace/test
First of all, here are some of the classes that I am using:
AudioCapturer.hpp
class AudioCapturer : public sf::SoundStream
{
public:
AudioCapturer(Client& client);
void receiveLoop(sf::Packet& packet);
private:
bool onGetData(sf::SoundStream::Chunk& data) override;
void onSeek(sf::Time timeOffset) override;
private:
std::recursive_mutex mutex;
std::vector<std::int16_t> samples;
std::vector<std::int16_t> tempbuffer;
size_t offset;
Client& client;
};
AudioCapturer.cpp
AudioCapturer::AudioCapturer(Client& client)
: offset(0), client(client)
{
initialize(1, 44100);
}
bool AudioCapturer::onGetData(sf::SoundStream::Chunk& data)
{
while (offset >= samples.size())
sf::sleep(sf::milliseconds(10));
{
std::scoped_lock lock(mutex);
tempbuffer.assign(samples.begin() + static_cast<std::vector<std::int64_t>::difference_type>(offset),
samples.end());
std::cout << "Tempbuffer address: " << &tempbuffer << std::endl;
}
data.samples = tempbuffer.data();
data.sampleCount = tempbuffer.size();
offset += tempbuffer.size();
return true;
}
void AudioCapturer::onSeek(sf::Time timeOffset)
{
offset = static_cast<std::size_t>(timeOffset.asMilliseconds()) * getSampleRate() * getChannelCount() / 1000;
}
void AudioCapturer::receiveLoop(sf::Packet& packet)
{
while (true)
{
if (client.receivedAudio)
{
sf::Packet copy = packet;
std::uint8_t command;
if (copy >> command)
{
std::size_t sampleCount = (copy.getDataSize() - 1) / sizeof(std::int16_t);
{
std::scoped_lock lock(mutex);
std::size_t oldSize = samples.size();
samples.resize(oldSize + sampleCount);
std::cout << "Samples address: " << &samples << std::endl;
std::cout << "Last item address: " << &samples[oldSize] << std::endl;
std::memcpy(&(samples[oldSize]),
static_cast<const char*>(copy.getData()) + 1,
sampleCount * sizeof(std::int16_t));
}
client.receivedAudio = false;
}
}
}
}
Client.hpp
class Client
{
public:
Client(const sf::IpAddress& ip, int port);
~Client();
bool connect();
void disconnect();
bool send(sf::Packet& packet);
bool receivePacket(sf::Packet& packet, std::unordered_map<std::uint32_t, Player>& players, std::uint32_t& thisPlayer);
public:
bool receivedAudio;
bool receivedPosition;
private:
int port;
sf::IpAddress ip;
sf::TcpSocket socket;
};
Client.cpp
Client::Client(const sf::IpAddress& ip, int port)
: ip(ip), port(port), receivedAudio(false), receivedPosition(false)
{
}
Client::~Client()
{
}
bool Client::connect()
{
if (socket.connect(ip, port) != sf::Socket::Done)
return false;
return true;
}
void Client::disconnect()
{
socket.disconnect();
}
bool Client::send(sf::Packet& packet)
{
if (socket.send(packet) != sf::Socket::Done)
return false;
return true;
}
bool Client::receivePacket(sf::Packet& packet, std::unordered_map<std::uint32_t, Player>& players, std::uint32_t& thisPlayer)
{
sf::Packet recv;
sf::Packet copy;
while (true)
{
if (socket.receive(recv) != sf::Socket::Done)
return false;
if (receivedAudio)
std::cout << "Data is not used!" << std::endl;
std::uint8_t command;
std::uint32_t id;
copy = recv;
if (copy >> command)
{
switch (command)
{
case ServerCommand::ADD_PLAYER:
copy >> id;
if (thisPlayer == 0)
{
thisPlayer = id;
std::cout << "My id is " << thisPlayer << std::endl;
}
players[id] = Player(5.f, id);
std::cout << "Player joined!" << std::endl;
break;
case ServerCommand::ADD_PLAYERS:
while (copy >> id)
players[id] = Player(5.f, id);
break;
case ServerCommand::RECEIVE_AUDIO:
receivedAudio = true;
packet = recv;
break;
case ServerCommand::RECEIVE_POSITION:
receivedPosition = true;
packet = recv;
break;
default:
break;
}
}
}
return true;
}
AudioRecorder.hpp
class AudioRecorder : public sf::SoundRecorder
{
public:
AudioRecorder(Client& client);
~AudioRecorder() override;
void activeness();
void setActive(bool active);
bool isActive() const;
private:
bool onStart() override;
bool onProcessSamples(const std::int16_t* samples, std::size_t sampleCount) override;
void onStop() override;
private:
Client& client;
bool active;
std::mutex mutex;
};
AudioRecorder.cpp
AudioRecorder::AudioRecorder(Client& client)
: client(client), active(false)
{
}
AudioRecorder::~AudioRecorder()
{
stop();
}
void AudioRecorder::activeness()
{
while (true)
{
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space) && !active)
{
std::scoped_lock lock(mutex);
active = true;
if (start()) {}
}
if (!sf::Keyboard::isKeyPressed(sf::Keyboard::Space) && active)
{
std::scoped_lock lock(mutex);
stop();
active = false;
}
}
}
void AudioRecorder::setActive(bool active)
{
this->active = active;
}
bool AudioRecorder::isActive() const
{
return active;
}
bool AudioRecorder::onStart()
{
std::cout << "Started!" << std::endl;
return true;
}
bool AudioRecorder::onProcessSamples(const std::int16_t* samples, std::size_t sampleCount)
{
sf::Packet packet;
packet << ServerCommand::RECEIVE_AUDIO;
packet.append(samples, sampleCount * sizeof(std::int16_t));
return client.send(packet);
}
void AudioRecorder::onStop()
{
std::cout << "Stopped!" << std::endl;
}
main.cpp
int main()
{
std::srand(std::time(0));
Client client("127.0.0.1", 4242);
if (client.connect())
{
std::cout << "Connected to server!" << std::endl;
sf::RenderWindow window(sf::VideoMode({ 1280, 720 }), "Multiplayer Game", sf::Style::Default);
window.setVerticalSyncEnabled(true);
sf::View view;
view.setCenter({ 640, 360 });
view.setSize(sf::Vector2f(window.getSize()));
sf::Packet packet;
std::unordered_map<std::uint32_t, Player> players;
std::uint32_t thisPlayer = 0;
StartScreen startscreen(window, sf::Color(172, 192, 146));
AudioRecorder recorder(client);
AudioCapturer capturer(client);
capturer.play();
std::thread receivePacket;
std::thread receiverloop;
std::thread activeness;
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
switch (event.type)
{
case sf::Event::Closed:
window.close();
break;
case sf::Event::Resized:
if (startscreen)
{
view.setCenter({
static_cast<float>(window.getSize().x) / 2,
static_cast<float>(window.getSize().y) / 2
});
view.setSize(sf::Vector2f(window.getSize()));
startscreen.updateOnResize();
}
break;
}
}
if (!receivePacket.joinable())
receivePacket = std::thread(&Client::receivePacket, std::ref(client), std::ref(packet), std::ref(players), std::ref(thisPlayer));
if (!receiverloop.joinable())
receiverloop = std::thread(&AudioCapturer::receiveLoop, std::ref(capturer), std::ref(packet));
if (!activeness.joinable())
activeness = std::thread(&AudioRecorder::activeness, std::ref(recorder));
if (client.receivedPosition)
{
sf::Packet copy = packet;
std::uint8_t command;
if (copy >> command)
{
float x, y;
std::uint32_t id;
copy >> id >> x >> y;
for (auto& player : players)
if (player.first == id)
player.second.setPosition({ x, y });
client.receivedPosition = false;
}
}
if (window.hasFocus())
players[thisPlayer].move(client);
window.clear(sf::Color::White);
window.setView(view);
for (const auto& player : players)
window.draw(player.second);
window.display();
}
client.disconnect();
std::terminate();
}
return 0;
}
I know it's a lot of code and I am sorry that I have to share too much.
I believe the problem is with the AudioCapturer class. For some reason when sending audio data, after a while client that receives it stops the execution and prompts this problem with different addresses:
a.out(60911,0x16b47b000) malloc: Incorrect checksum for freed object 0x12a80a400: probably modified after being freed.
Corrupt value: 0xe7026101a000bcff
By the way I am kind of using the SFML's VoIP example for the AudioCapturer class.
This is the Github page of it: https://github.com/SFML/SFML/blob/master/examples/voip/Server.cpp
I'd appreciate it If someone can help me with that problem.

getting response only on the first time with socket programming

I'm trying to create an HTTP server using C++ 98.
The issue is that, every time I launch my server, I get the response, sending again the request from the same browser tab, and the browser keeps loading.
In my kqueue, it seems like the block for checking the read event is not being executed on the second time.
This is my code:
void webserv::Server::lunch(){
this->kq.create_event(this->sock.getSocket(), EVFILT_READ);
while(1)
this->_lunch_worker();
}
void webserv::Server::_lunch_worker(void)
{
int n_ev;
int accept_sock;
int address_size;
char buff[1000];
webserv::Header header;
// register the events in ev_list
std::cout << GREEN << "---- WAITING FOR CONNECTION ----" << RESET << std::endl;
n_ev = this->kq.get_event();
for (int i = 0; i < n_ev; i++)
{
if (this->kq.get_fd(i) < 0)
continue;
if (this->kq.get_fd(i) == this->sock.getSocket())
{
std::cout << "--- RECEIVED NEW CONNECTION ---" << std::endl;
address_size = sizeof(this->sock.getAddress());
accept_sock = accept(
this->sock.getSocket(),
(struct sockaddr*)&this->sock.getAddress(),
(socklen_t *)&address_size
);
this->sock.test_error(accept_sock);
this->kq.create_event(accept_sock, EVFILT_READ);
int flags;
if ((flags = fcntl(accept_sock, F_GETFL, 0)) < 0) {
perror("fcntl");
close(accept_sock);
close(this->sock.getSocket());
}
if (fcntl(accept_sock, F_SETFL, flags | O_NONBLOCK) < 0) {
perror("fcntl");
close(accept_sock);
close(this->sock.getSocket());
}
this->kq.create_event(accept_sock, EVFILT_WRITE, EV_ADD | EV_ONESHOT);
}
else if (this->kq.is_read_available(i))
{
int bytes_read;
std::cout << "START: is_read_available" << std::endl;
if ((bytes_read = recv(this->kq.get_fd(i), buff, 999, 0)) > 0)
{
}
}
else if (this->kq.is_write_available(i))
{
std::string hello = "HTTP/1.1 200 OK\nContent-Type: text/plain\nContent-Length: 12\n\nHello world!";
if (send(this->kq.get_fd(i), hello.c_str(), hello.length(), 0) != 0)
{
std::cout << "TEST2" << std::endl;
this->kq.set_event(this->kq.get_fd(i), EVFILT_WRITE, EV_DELETE);
this->kq.get_event_list()[i].ident = -1;
close(this->kq.get_fd(i));
}
std::cout << "END: is_write_available" << std::endl;
}
}
}
And this is the kqueue class:
/************************ CONSTRUCTORS/DESTRUCTOR ************************/
webserv::Kqueue::Kqueue()
{
this->_kq = kqueue();
std::cout << "KQUEUE CREATED" << std::endl;
this->test_error(this->_kq, "Creating Kqueue :");
this->_n_ev = 0;
}
webserv::Kqueue::~Kqueue()
{
close(this->_kq);
}
/************************ MEMBER FUNCTIONS ************************/
void webserv::Kqueue::set_event(int fd, int filter, int flags, void *udata)
{
EV_SET(&this->_ev_set, fd, filter, flags, 0, 0, udata);
}
void webserv::Kqueue::add_event(void)
{
int ret;
ret = kevent(this->_kq, &this->_ev_set, 1, NULL, 0, NULL);
this->test_error(ret, "Kqueue/add_even functions");
}
int webserv::Kqueue::get_event(void)
{
this->_n_ev = kevent(this->_kq, NULL, 0, this->_ev_list, __EV_LIST_SIZE__, NULL);
this->test_error(this->_n_ev, "Kqueue/get_event function:");
return (this->_n_ev);
}
void webserv::Kqueue::create_event(int fd, int filter, int flags, void *udata)
{
this->set_event(fd, filter, flags, udata);
this->add_event();
}
bool webserv::Kqueue::isEOF(int index)
{
if (this->_n_ev <= index)
this->test_error(-1, "Kqueue/isEOF function:");
return (this->_ev_list[index].flags & EV_EOF);
}
bool webserv::Kqueue::is_read_available(int index)
{
if (this->_n_ev <= index)
this->test_error(-1, "Kqueue/is_read_available function:");
return (this->_ev_list[index].filter == EVFILT_READ);
}
bool webserv::Kqueue::is_write_available(int index)
{
if (this->_n_ev <= index)
this->test_error(-1, "Kqueue/is_write_available function:");
return (this->_ev_list[index].filter == EVFILT_WRITE);
}
void webserv::Kqueue::test_error(int fd, const std::string &str)
{
if (fd < 0)
{
std::cerr << RED << str << " ";
perror("The following error occured: ");
std::cerr << RESET;
exit(EXIT_FAILURE);
}
}
/************************ GETTERS/SETTERS ************************/
struct kevent *webserv::Kqueue::get_event_list()
{
return (this->_ev_list);
}
int webserv::Kqueue::get_fd(int index)
{
if (this->_n_ev <= index)
this->test_error(-1, "Kqueue/get_ev_list function:");
return (this->_ev_list[index].ident);
}
void webserv::Kqueue::set_kqueue(int fd)
{
this->_kq = fd;
}

ZeroMQ : a thread stops exactly after 2 hours in a while(1)-loop. Why & how to resolve this?

It does not crash so there is no core to analyse. It simply stops.
I have experimented multiple times. It stops exactly after 2 hours.
OS : qnx6.5.0
LIBs : ZeroMQ and Protobuf
I just have a single thread, it looks something like this :
dummyFrontEnd::dummyFrontEnd():context(1),socket(context,ZMQ_PUB) {
}
void dummyFrontEnd::Init()
{
socket.connect("tcp://127.0.0.1:5555");
cout << "Connecting .... " << endl;
}
void dummyFrontEnd::SendCANalyserTable(const std::string& filename)
{
...
zmq::message_t create_values( protoTable.ByteSizeLong()
+ sizeof(uint16_t)
);
*((uint16_t*)create_values.data()) = TABLEMSG_ID; // ID
protoTable.SerializeToArray( create_values.data()
+ sizeof(uint16_t),
protoTable.ByteSizeLong()
);
try {
socket.send(create_values,ZMQ_NOBLOCK);
}
catch (int e){
std::cout << "SPD exception e : "
<< e
<< std::endl;
}
protoTable.clear_columnvalues();
usleep(1);
}
}
void main(){
...
...
while(1) {
if (arguments.canalyser_filename != "") {
dmyFntEnd.SendCANalyserTable(arguments.canalyser_filename);
if (arguments.verbose) {
cout << "DummyFrontEnd"
<< "completed sending CANalyser table"
<< endl;
}
}
}
...
...
}

Bluetooth thread 'Bad file descriptor' in recv funktion

I am writing a server/client program using linux sockets. I want that both server and client can read and write at the same time with threads.
(The full code I followed can be downloaded at http://www.praxisbuch.net/embedded-linux/downloads.html).
When I start() my thread it throws a infinite loop of 'Bad file descriptor'.
My client.cpp is as follows:
class receiveThread : public Thread {
public:
receiveThread(RawMessage& rawMsg) : data_control(rawMsg) {}
virtual void run() {
cout << "starting recveive thread ..." << endl;
while(1) {
int recvBytes = bt_socket.recieveMessage(&temp_data_control);
try {
cout << "Received new Target Position: " << temp_data_control.data.value1 << endl;
data_control = temp_data_control;
}
catch (SocketException) {
cout << "Received exception " << endl;
sleep(1);
}
}
}
~receiveThread() {
bt_socket.disconnect();
}
private:
BtSocket bt_socket;
RawMessage temp_data_control;
RawMessage& data_control;
};
int main() {
BtSocket BtClient(PORTNUM);
// connect to the server.
BtClient.connectToServer();
RawMessage raw_mes;
receiveThread recv_thread(raw_mes);
recv_thread.start(); //the error occurs
while (1)
{
//do something
}
return 0;
}
thread.cpp is as follows:
void Thread::start() throw(Thread::Exception) {
if (pthread_create(&thread_id, 0, thread_starter, (void *) this) != 0) {
throw Exception("Thread::start: Cannot create thread");
}
}
void* Thread::thread_starter(void* thread_obj) {
Thread* self = reinterpret_cast<Thread*> (thread_obj);
self->setRunning(true);
self->run();
self->setRunning(false);
return 0;
}
In socket.cpp the exception is thrown:
int BtSocket::recieveMessage(AbstractDatagram *abstractDgm)
{
int numBytes;
if ((numBytes = recv(s, abstractDgm->getData(), abstractDgm->getSize(), 0)) < 0)
{
cout << strerror(errno) << endl;
}
return numBytes;
}
I really don't know what the problem is.
Any help would be extremely appreciated!
EDIT: (also from socket.cpp)
Socket::Socket(int pNumber)
{
portNumber = pNumber;
blocking = 1;
if ((s = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM)) == -1)
{
throw SocketException("socket failed (socket())", true);
}
locAddr.rc_family = AF_BLUETOOTH;
str2ba(dest, &locAddr.rc_bdaddr);
locAddr.rc_channel = portNumber;
}
void BtSocket::connectToServer()
{
struct sockaddr_rc serverAddr;
serverAddr.rc_family = AF_BLUETOOTH;
serverAddr.rc_channel = 1;
str2ba(dest, &serverAddr.rc_bdaddr);
if (connect(s, (struct sockaddr *)&serverAddr, sizeof(serverAddr)) == -1)
{
throw SocketException("Connect failed (connect())", true);
}
}
I think I opend the socket correctly!?
Could it have been closed somehow?

How to prepare eclipse for opencl programming (Intel OpenCL SDK Installed) in Linux

I installed Intel OpenCL SDK on OpenSUSE GNU/Linux 13.1. How do I prepare the Eclipse CDT IDE that comes with my distribution for OpenCL programming?
Edit:
After adding include path /opt/intel/opencl-xxx/inlcude to project and linking /opt/intel/opencl-xxx/lib64/libOpenCL.so to /usr/lib64/libOpenCL.so, Eclipse will find the CL/cl.h but GCC return this error:
Building file: ../src/HelloWorld.cpp
Invoking: GCC C++ Compiler
g++ -I/opt/intel/opencl-1.2-3.2.1.16712/include/ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/HelloWorld.d" -MT"src/HelloWorld.d" -o "src/HelloWorld.o" "../src/HelloWorld.cpp"
../src/HelloWorld.cpp:11:16: fatal error: cl.h: No such file or directory
#include <cl.h>
^
compilation terminated.
make: *** [src/HelloWorld.o] Error 1
Edit 2:
HelloWorld.cpp :
#include <iostream>
#include <fstream>
#include <sstream>
#include <CL\cl.h>
const int ARRAY_SIZE = 1000;
cl_context CreateContext() {
cl_int errNum;
cl_uint numPlatforms;
cl_platform_id firstPlatformId;
cl_context context = NULL;
errNum = clGetPlatformIDs(1, &firstPlatformId, &numPlatforms);
if (errNum != CL_SUCCESS || numPlatforms <= 0) {
std::cerr << "Failed to find any OpenCL platforms." << std::endl;
return NULL;
}
cl_context_properties contextProperties[] = {
CL_CONTEXT_PLATFORM,
(cl_context_properties)firstPlatformId,
0
};
context = clCreateContextFromType(contextProperties, CL_DEVICE_TYPE_GPU,
NULL, NULL, &errNum);
if (errNum != CL_SUCCESS) {
std::cout << "Could not create GPU context, trying CPU..." << std::endl;
context = clCreateContextFromType(contextProperties, CL_DEVICE_TYPE_CPU, NULL, NULL, &errNum);
if (errNum != CL_SUCCESS) {
std::cerr << "Failed to create an OpenCL GPU or CPU context." << std::endl;
return NULL;
}
}
return context;
}
cl_command_queue CreateCommandQueue(cl_context context, cl_device_id *device)
{
cl_int errNum;
cl_device_id *devices;
cl_command_queue commandQueue = NULL;
size_t deviceBufferSize = -1;
errNum = clGetContextInfo(context, CL_CONTEXT_DEVICES, 0, NULL, &deviceBufferSize);
if (errNum != CL_SUCCESS) {
std::cerr << "Failed call to clGetContextInfo(...,GL_CONTEXT_DEVICES,...)";
return NULL;
}
if (deviceBufferSize <= 0) {
std::cerr << "No devices available.";
return NULL;
}
devices = new cl_device_id[deviceBufferSize / sizeof(cl_device_id)];
errNum = clGetContextInfo(context, CL_CONTEXT_DEVICES, deviceBufferSize, devices,NULL);
if (errNum != CL_SUCCESS) {
delete [] devices;
std::cerr << "Failed to get device IDs";
return NULL;
}
commandQueue = clCreateCommandQueue(context, devices[0], 0, NULL);
if (commandQueue == NULL) {
delete [] devices;
std::cerr << "Failed to create commandQueue for device 0";
return NULL;
}
*device = devices[0];
delete [] devices;
return commandQueue;
}
cl_program CreateProgram(cl_context context, cl_device_id device, const char* fileName)
{
cl_int errNum;
cl_program program;
std::ifstream kernelFile(fileName, std::ios::in);
if (!kernelFile.is_open())
{
std::cerr << "Failed to open file for reading: " << fileName << std::endl;
return NULL;
}
std::ostringstream oss;
oss << kernelFile.rdbuf();
std::string srcStdStr = oss.str();
const char *srcStr = srcStdStr.c_str();
program = clCreateProgramWithSource(context, 1,
(const char**)&srcStr,
NULL, NULL);
if (program == NULL)
{
std::cerr << "Failed to create CL program from source." << std::endl;
return NULL;
}
errNum = clBuildProgram(program, 0, NULL, NULL, NULL, NULL);
if (errNum != CL_SUCCESS)
{
char buildLog[16384];
clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG,
sizeof(buildLog), buildLog, NULL);
std::cerr << "Error in kernel: " << std::endl;
std::cerr << buildLog;
clReleaseProgram(program);
return NULL;
}
return program;
}
bool CreateMemObjects(cl_context context, cl_mem memObjects[3],
float *a, float *b)
{
memObjects[0] = clCreateBuffer(context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR,
sizeof(float) * ARRAY_SIZE, a, NULL);
memObjects[1] = clCreateBuffer(context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR,
sizeof(float) * ARRAY_SIZE, b, NULL);
memObjects[2] = clCreateBuffer(context, CL_MEM_READ_WRITE,
sizeof(float) * ARRAY_SIZE, NULL, NULL);
if (memObjects[0] == NULL || memObjects[1] == NULL || memObjects[2] == NULL)
{
std::cerr << "Error creating memory objects." << std::endl;
return false;
}
return true;
}
void Cleanup(cl_context context, cl_command_queue commandQueue,
cl_program program, cl_kernel kernel, cl_mem memObjects[3])
{
for (int i = 0; i < 3; i++)
{
if (memObjects[i] != 0)
clReleaseMemObject(memObjects[i]);
}
if (commandQueue != 0)
clReleaseCommandQueue(commandQueue);
if (kernel != 0)
clReleaseKernel(kernel);
if (program != 0)
clReleaseProgram(program);
if (context != 0)
clReleaseContext(context);
}
int main(int argc, char** argv)
{
cl_context context = 0;
cl_command_queue commandQueue = 0;
cl_program program = 0;
cl_device_id device = 0;
cl_kernel kernel = 0;
cl_mem memObjects[3] = { 0, 0, 0 };
cl_int errNum;
// Create an OpenCL context on first available platform
context = CreateContext();
if (context == NULL)
{
std::cerr << "Failed to create OpenCL context." << std::endl;
return 1;
}
// Create a command-queue on the first device available
// on the created context
commandQueue = CreateCommandQueue(context, &device);
if (commandQueue == NULL)
{
Cleanup(context, commandQueue, program, kernel, memObjects);
return 1;
}
// Create OpenCL program from HelloWorld.cl kernel source
program = CreateProgram(context, device, "HelloWorld.cl");
if (program == NULL)
{
Cleanup(context, commandQueue, program, kernel, memObjects);
return 1;
}
// Create OpenCL kernel
kernel = clCreateKernel(program, "hello_kernel", NULL);
if (kernel == NULL)
{
std::cerr << "Failed to create kernel" << std::endl;
Cleanup(context, commandQueue, program, kernel, memObjects);
return 1;
}
// Create memory objects that will be used as arguments to
// kernel. First create host memory arrays that will be
// used to store the arguments to the kernel
float result[ARRAY_SIZE];
float a[ARRAY_SIZE];
float b[ARRAY_SIZE];
for (int i = 0; i < ARRAY_SIZE; i++)
{
a[i] = (float)i;
b[i] = (float)(i * 2);
}
if (!CreateMemObjects(context, memObjects, a, b))
{
Cleanup(context, commandQueue, program, kernel, memObjects);
return 1;
}
// Set the kernel arguments (result, a, b)
errNum = clSetKernelArg(kernel, 0, sizeof(cl_mem), &memObjects[0]);
errNum |= clSetKernelArg(kernel, 1, sizeof(cl_mem), &memObjects[1]);
errNum |= clSetKernelArg(kernel, 2, sizeof(cl_mem), &memObjects[2]);
if (errNum != CL_SUCCESS)
{
std::cerr << "Error setting kernel arguments." << std::endl;
Cleanup(context, commandQueue, program, kernel, memObjects);
return 1;
}
size_t globalWorkSize[1] = { ARRAY_SIZE };
size_t localWorkSize[1] = { 1 };
// Queue the kernel up for execution across the array
errNum = clEnqueueNDRangeKernel(commandQueue, kernel, 1, NULL,
globalWorkSize, localWorkSize,
0, NULL, NULL);
if (errNum != CL_SUCCESS)
{
std::cerr << "Error queuing kernel for execution." << std::endl;
Cleanup(context, commandQueue, program, kernel, memObjects);
return 1;
}
// Read the output buffer back to the Host
errNum = clEnqueueReadBuffer(commandQueue, memObjects[2], CL_TRUE,
0, ARRAY_SIZE * sizeof(float), result,
0, NULL, NULL);
if (errNum != CL_SUCCESS)
{
std::cerr << "Error reading result buffer." << std::endl;
Cleanup(context, commandQueue, program, kernel, memObjects);
return 1;
}
// Output the result buffer
for (int i = 0; i < ARRAY_SIZE; i++)
{
std::cout << result[i] << " ";
}
std::cout << std::endl;
std::cout << "Executed program succesfully." << std::endl;
Cleanup(context, commandQueue, program, kernel, memObjects);
return 0;
}
HelloWorld.cl :
__kernel void hello_kernel(
__global const float *a,
__global const float *b,
__global float *result)
{
int gid = get_global_id(0);
result[gid] = a[gid] + b[gid];
}
You just need to add one OpenCL include directory to the project and link to one libOpenCL. In my case (Ubuntu 13.04) I need to add /opt/intel/opencl-1.xxxx/include to the include paths of the project, which allow for the use of #include <CL/cl.h>.
Also add /opt/intel/opencl-1.xxxx/lib64/ to the linker search paths and add OpenCL to the link-libraries.
That should do it.

Resources