So I started playing around with the SFML library and I get lots of memory leaks just by creating a simple program that draws a window. Any solution to this?
int main()
{
sf::RenderWindow window(sf::VideoMode(SCREEN_X, SCREEN_Y), APP_TITLE);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::MouseButtonPressed)
{
if (event.mouseButton.button == sf::Mouse::Left)
{
}
}
if (event.type == sf::Event::Closed)
window.close();
}
window.clear(sf::Color::Black);
window.display();
}
_CrtDumpMemoryLeaks();
return 0;
}
Okay, so it turns out that CRT falsely reports memory leaks. Actually there are no leaks whatsoever. I installed a plugin for VS and now everything is fine.
Related
I want to know your opinion on how I coded my QThread.
this part of my code is for handle a PWM for LED display.
The purpose of my QThread is to work as smoothly as possible while changing a value in the QThread else i can watch on LED display a very fast break during the value change, example when i use QMutex.
this is the reason why I chose to use the SIGNAL / SLOT way
When I need to change the brightness that depends on the time or value of a resistance, I use the emit SIGNAL_FUNCTION (qint32 new_value)
Is there a risk of crashing my application using my method?
is there another way more correct to handle this kind of procedure?
I enclose my code below.
#include "luminosite.h"
LUMINOSITE::LUMINOSITE(QObject *parent, qint32 P_PIN_LUMINOSITE) :
QThread(parent)
{
PIN_LUMINOSITE = P_PIN_LUMINOSITE;
VALEUR = 1;
}
void LUMINOSITE::run()
{
qint32 TICK = 0;
while(true)
{
if (TICK == 0)
{
digitalWrite(PIN_LUMINOSITE, LOW);
}
else if (TICK == VALEUR)
{
digitalWrite(PIN_LUMINOSITE, HIGH);
}
TICK++;
if (TICK == 33)
{
TICK = 0;
}
delayMicroseconds(1);
}
}
void LUMINOSITE::CHANGEMENT_VALEUR(qint32 P_VALEUR) // SLOT
{
VALEUR = P_VALEUR;
}
If your question is only "SIGNAL/SLOT are they thread safe ?", read that : http://doc.qt.io/qt-5/threads-qobject.html#signals-and-slots-across-threads
It only depend on the connection type you choose.
You need to choose between Qt::QueuedConnection and Qt::BlockingQueuedConnection
I keep getting this error due to a leak in my native code according to this thread:
ReferenceTable overflow (max=512) JNI
yet, it seems to me the the AttachCurrentThread leaks. I tried this code and it leaks
// this code LEAKS!
// C++:
void Engine::UpdateCamera(float x, float y, float z) {
JNIEnv *jni;
app_->activity->vm->AttachCurrentThread(&jni, NULL);
//Do nothing
app_->activity->vm->DetachCurrentThread();
return;
}
// Java
public void updateCamera(final float x, final float y, final float z) {
if (_label2 == null)
return;
StackTraceElement trace = new Exception().getStackTrace()[0];
Log.e(APP_TAG, "Called:" +
trace.getClassName() + "->" + trace.getMethodName() + ":" + trace.getLineNumber());
}
Then I simply commented out everything and the program stopped leaking and ran forever :(
// this code never leaks, but it does not do anything either
void Engine::UpdateCamera(float x, float y, float z) {
JNIEnv *jni;
//app_->activity->vm->AttachCurrentThread(&jni, NULL);
//app_->activity->vm->DetachCurrentThread();
return;
}
Has anyone experienced leaking issues with AttachCurrentThread?
thank you.
Are you connected to the debugger? If so, disconnect and you may find the weak reference table returns to a reasonable value.
I had this same problem; if I run with the debugger, this occurs.
The below attatchTestMemoryLeak() function has a native memory leak, and I still have not figured out the reason, but I did find another way to avoid the native memory leak; see function attatchTestOK();
//c++ code
void attatchTestMemoryLeak(){
for(int i=0; i<100000; i++){
JNIEnv *env= nullptr;
//native thread try to attach java environment;
int getEnvStat = g_VM->GetEnv((void **)&env,JNI_VERSION_1_4);
if (getEnvStat == JNI_EDETACHED) {
jint attachStat=g_VM->AttachCurrentThread(&env, NULL);
if (attachStat == JNI_OK) {
LOG_E("index=%d, attach ok",i);
}else{
LOG_E("index=%d, attach failed",i);
}
}
//do something, call java function;
//Detatched the native thread from java environment;
jint detachStat=g_VM->DetachCurrentThread();
if(detachStat==JNI_OK){
LOG_E("detach ok, index=%d, detachStat=%d",i,detachStat);
}else{
LOG_E("detach failed, index=%d,detachStat=%d",i,detachStat);
}
env = NULL;
}
}
The below function works fine, and the https://www.jianshu.com/p/1f17ab192940 gives the explanation.
static pthread_key_t detachKey=0;
void detachKeyDestructor(void* arg)
{
pthread_t thd = pthread_self();
JavaVM* jvm = (JavaVM*)arg;
LOG_E("detach thread, thd=%u",thd);
jvm->DetachCurrentThread();
}
void attachTestOK(){
for (int i = 0; i < 1000000; i++)
{
JNIEnv *env= nullptr;
int getEnvStat = g_VM->GetEnv((void **)&env,JNI_VERSION_1_4);
if (getEnvStat == JNI_EDETACHED) {
if (detachKey == 0){
LOG_E("index=%d,create thread key",i);
pthread_key_create(&detachKey, detachKeyDestructor);
}
jint attachStat=g_VM->AttachCurrentThread(&env, NULL);
pthread_setspecific(detachKey, g_VM_Test);
if (attachStat == JNI_OK) {
LOG_E("index=%d, attach ok",i);
}else{
LOG_E("index=%d, attach failed",i);
}
}
LOG_E("index=%d, getEnvStat=%d",i,getEnvStat);
//do something, call java function;
env = NULL;
}
}
I have the following code that I would like to update to be more portable and c++11 friendly. However, I'm stuck as how to replace the pthread calls. I can use std::this_thread::get_id() to get the thread id but can't tell if that thread is still alive.
pthread_t activeThread = 0;
pthread_t getCurrentThread() {
return pthread_self();
}
bool isActiveThreadAlive() {
if(activeThread == 0) {
return false;
}
return pthread_kill(activeThread, 0) != ESRCH;
}
Potential std::thread version...
std::thread::id activeThread = std::thread::id();
std::thread::id getCurrentThread() {
return std::this_thread::get_id();
}
bool isActiveThreadAlive() {
if(activeThread == std::thread::id()) {
return false;
}
return pthread_kill(activeThread, 0) != ESRCH;// <--- need replacement!!!
}
What the code really needs to do is know if the thread has died from an exception or some other error that caused it to terminate without releasing the object. As in the following...
std::unique_lock<std::mutex> uLock = getLock();
while (activeThread != 0) {
if (threadWait.wait_for(uLock, std::chrono::seconds(30)) == std::cv_status::timeout) {
if (!isActiveThreadAlive()) {
activeThread = 0;
}
}
}
activeThread = getCurrentThread();
uLock.unlock();
try {
// do stuff here.
}
catch (const std::exception&) {
}
uLock.lock();
activeThread = 0;
And before anyone asks I do not have a guarantee of control over when, where or how the threads are created. The threads that call the functions may be from anywhere.
in an parallel loop, there is a critical section. I try to execute an mfc dialog with DoModal in the critical section, however since main thread waits for parallel threads, there is no way for my dialog to show up and execute. In order to break this dependency, I create an executable and I run it as a process within my parallel loop. When the process shows dialog and gets the information. It returns and other threads keeps running.
However my team leader insist that there is a better way to do it which I couldn't figure out after doing hours of search :\
I tried a seperate thread in parallel for. It didn't worked.
I tried CWinThread (google say it is gui thread :\ which didn't helped)
I tired creating an exe and running it. That worked :)
int someCriticDialog()
{
#pragma omp critic (showCriticDlg)
{
CMyDialog ccc;
ccc.DoModal();
/* However the code below works
CreateProcess("someCriticDlg.exe", null, &hProcess);
WaitForSingeObject(hProcess, INFINITE);
*/
}
}
#pragma omp parallel
for (int i = 0; i < 5; i++)
someCriticDialog();
Let's say here is the problem:
void trouble_maker()
{
Sleep(10000);//application stops for 10 seconds
}
You can use PostMessage + PeekMessage + modal dialog to wait for it to finish through GUI window:
void PumpWaitingMessages()
{
MSG msg;
while (::PeekMessage(&msg, NULL, NULL, NULL, PM_NOREMOVE))
if (!AfxGetThread()->PumpMessage())
return;
}
BEGIN_MESSAGE_MAP(CMyDialog, CDialog)
ON_COMMAND(2000, OnDoSomething)
ON_COMMAND(IDCANCEL, OnCancel)
END_MESSAGE_MAP()
CMyDialog::CMyDialog(CWnd* par /*=NULL*/) : CDialog(IDD_DIALOG1, par)
{
working = false;
stop = false;
}
BOOL CMyDialog::OnInitDialog()
{
BOOL res = CDialog::OnInitDialog();
//call the function "OnDoSomething", but don't call it directly
PostMessage(WM_COMMAND, 2000, 0);
return res;
}
void CMyDialog::OnCancel()
{
if (working)
{
stop = true;
}
else
{
CDialog::OnCancel();
}
}
void CMyDialog::OnDoSomething()
{
HANDLE h = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)&trouble_maker, NULL, 0, NULL);
working = true;
for (;;)
{
if (WAIT_TIMEOUT != WaitForSingleObject(h, 100)) break;
PumpWaitingMessages();
//update progress bar or something...
if (stop)
{
//terminate if it's safe
//BOOL res = TerminateThread(h, 0);
//CloseHandle(h);
//CDialog::OnCancel();
//return;
}
}
working = false;
MessageBox("done");
}
I wonder how to solve this problem.
I notice that CreateThread() doesn't work well in this code:
DWORD threadFunc1(LPVOID lParam)
{
int cur = (int)lParam
while(1)
{
//Job1...
//Reason
break;
}
Start(cur + 1);
Sleep(100);
}
void Start(int startNum)
{
....
CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)threadFunc1, &startNum, 0, &dwThreadId);
...
}
void btnClicking()
{
Start(0);
}
In this code, there is a thread create by Start() and it calls Start() when the thread ends.
The second created thread does not work. I think the first thread disappeared and the second thread is destroyed.
What is the best way to solve this?
OS: Win 7 64bit Ultimate.
Tool: Visual Studio 2008.
It does not work because your code has bugs in it. The signature of your thread function is wrong, and you are passing the startNum value to the thread in the wrong way.
Try this instead:
DWORD WINAPI threadFunc1(LPVOID lParameter)
{
int cur = (int) lParameter;
while(1)
{
//Job1...
//Reason
break;
}
Start(cur + 1);
Sleep(100);
}
void Start(int startNum)
{
....
HANDLE hThread = CreateThread(NULL, NULL, &threadFunc1, (LPVOID) startNum, 0, &dwThreadId);
if (hThread != NULL)
{
// ... store it somewhere or close it, otherwise you are leaking it...
}
...
}
void btnClicking()
{
Start(0);
}