Click here to Skip to main content
15,886,110 members

Comments by mbue (Top 200 by date)

mbue 16-Aug-22 10:32am View    
At here:
if(!m_buffer_size || m_buffer_index>=m_buffer_capacity) {
your function will run into an infinite loop, if the returned m_buffer_size is lower than the m_buffer_capacity size!
if(!m_buffer_size || (m_buffer_index>=m_buffer_size))
is the right way.
The function should use a while loop (not recursively calls) to fill the target buffer - so you can terminate easily if the underlying stream has no more data.
mbue 2-Aug-14 5:46am View    
Yes indeed your statement IntToStr is the reason is irrelevant, cause its impossible. There must be other code the OP did not posted here. Use your debugger and see that any kind of that function cannot change parameters in a different way.
regards
mbue 1-Aug-14 17:22pm View    
sorry to intercept. but nothing is proven!
what do you think the function IntToStr should look alike?


#pragma once
#include <stdio.h>
#include <tchar.h>
#include <malloc.h>

static TCHAR __intBuff[64];
#define IntToStr1(i) (const TCHAR*)__IntToStr1(i,__intBuff,sizeof(__intBuff)/sizeof(__intBuff[0]))
#define IntToStr2(i) (const TCHAR*)__IntToStr2(i,__intBuff,sizeof(__intBuff)/sizeof(__intBuff[0]))

const TCHAR* __IntToStr1(int& i,TCHAR* buff,const unsigned int blen)
{
_itot_s(i,buff,blen,10);
i = 0;// <--- parameter 'i' may be lost or nulled by malfunction
return buff;
}

const TCHAR* __IntToStr2(int& i,TCHAR* buff,const unsigned int blen)
{
i = 0;// <--- parameter 'i' may be lost or nulled by malfunction
_itot_s(i,buff,blen,10);
return buff;
}

int _tmain(int argc, _TCHAR* argv[])
{
int counter = 0;
int dest_pos = 20;

_tprintf(__T("loop IntToStr1\r\n"));
for (int z = 0; z < 1000; z++)
{
/***2***/
int y = z;
/***3***/

if(counter++ == dest_pos)
{
/***1***/
_tprintf(__T("y = %s (OP says here comes z unchanged)\r\n"),IntToStr1(y));
_tprintf(__T("z = %s (OP says here comes 0)\r\n"),IntToStr1(z));
//if reach dest position - than would set new value for dest_pos
dest_pos = 56;

}
}

_tprintf(__T("loop IntToStr2\r\n"));

counter = 0;
dest_pos = 20;
for (int z = 0; z < 1000; z++)
{
/***2***/
int y = z;
/***3***/

if(counter++ == dest_pos)
{
/***1***/
_tprintf(__T("y = %s (OP says here comes z unchanged)\r\n"),IntToStr2(y));
_tprintf(__T("z = %s (OP says here comes 0)\r\n"),IntToStr2(z));
//if reach dest position - than would set new value for dest_pos
dest_pos = 56;

}
}
_tprintf(__T("<key> ")); _gettch();
return 0;
}





the result:

loop IntToStr1
y = 20 (OP says here comes z unchanged)
z = 20 (OP says here comes 0)
y = 36 (OP says here comes z unchanged)
z = 36 (OP says here comes 0)
loop IntToStr2
y = 0 (OP says here comes z unchanged)
z = 0 (OP says here comes 0)
y = 0 (OP says here comes z unchanged)
z = 0 (OP says here comes 0)


conclusion:
the only place the value of z can be changed to 0 is between '/***3***/' and '/***1***/'.
how is this possible with the code we've seen?

regards.
mbue 25-Sep-13 16:44pm View    
glad to help you.
mbue 23-Sep-13 17:08pm View    
My suggestion to terminate application:


#define WM_QUIT_ON_THREAD_OK ( WM_APP + 3 )

////////////////////////////
// main window proc
case WM_CLOSE:
if(IsWindow(hDlg))
{
PostMessage(hDlg,WM_QUIT_ON_THREAD_OK);
}
else
{
DestroyWindow(hWnd);
}
return 0;

////////////////////////////
// dialog window proc
case WM_QUIT_ON_THREAD_OK:

if(IsThreadRunning())
{
QuitOnThreadOk = 1;
ReflectQuit(hwnd);
}
else
{
DestroyWindow(hwnd);
PostMessage(hWndFrame,WM_CLOSE,0,0);
}

return 0;

case WM_THREAD_OK: // thread sent this

if( IsThreadRunning() ) // just wait until it fully exits
{
PostMessage(hwnd,WM_THREAD_OK,0,0); // thread is already running post the message again
// alternative: use a timer to check the threads state
}
else
{
ThreadFinished(hwnd);
if( QuitOnThreadOk )
{
DestroyWindow(hwnd);
PostMessage(hWndFrame,WM_CLOSE,0,0);
}
}

return TRUE;

////////////////////////////
// helper function
void ReflectQuit(HWND hDlg)
{
HWND hbtn = GetDlgItem( hDlg, IDC_BUTTON1 );

// show the user whats going on
SetWindowText(hbtn,__TEXT("application will close on finish thread"));
}

Regards.