|
Right, I think it's because my copy-constructors have not been defined correctly !
Thanks for the help.
Paul
|
|
|
|
|
No, you're not o the right path.
You code leaks memory. Let me expaly why:
void test()
{ foo(); }
literally means this:
- a space wide enough to store a MyClass is leaved on the stack (That's what
Myclass foo() means to the compiler) - the actual code address is pushed on stack, than
- a jump to the beginning of
foo is executed. at thet point
- a space wide enough to store a pointer is leaved on stack. (That's what
MyClass* p requires)
- new MyClass() is executed, that means:
- a bunch of bytes enouh to store MyClass are taken from the heap
- The MyClass::Myclass() constructor is called
- The address of the allocated memory is copied to p
- now, we are at
return *p that means: - a copy of the value pointed by p (that's what
*p means) is copied into the space previously left on the stack by calling MyClass(const MyClass&). That's what return If you did not declare it that's not important, the compiler generates it implicitly.
Now there are two distinct copy of MyClass: one taken from the heap whose address is stored in p and one that is stored temporarily on the stack containing a copy of it.
After the return,
- the p pointer is destroyed by unrolling the stack. The value it store (the address of the heap containing the first Myclass) is lost forever, but the object is still there (hence the leak)
- a jump is executed to the address stored in the stack, thus causing the execution to return after the call.
Now we are back to test() .
- No actions are required on the return value, hence it is destroyed and the stack unrolled
Now you have destroyed the temporary copy. The original object is still there.
In general every time you call new, you've to guess where and when there is a corresponding delete that will be called.
if you want to prperly debug all this, yu have to explicitly declare all what the compiler will elsewhere do implicitly: that is
class MyClass
{
public:
MyClass() {}
Now set a breakpoint at all of those functions, and try to debug, and you'll see funny things.
2 bugs found.
> recompile ...
65534 bugs found.
|
|
|
|
|
paolosh wrote: The intention is to create a MyClass instance in foo, and return it without onvoking the constructor on exit.
instead of
return *p;
call
return p;
(of course, you need to declare foo to return a pointer)
and than in the caller do something like:
std::auto_ptr<MyClass> p(foo());
This way the object will be automatically destroyed on exit of your test, and you won't have a memory leak.
|
|
|
|
|
Hi all,
I'm trying to animate something using GDI+. At the moment, to prevent smearing when my picture moves on the screen, I clear the screen before redrawing each picture with drawimage(hdc).
Of course, that causes the screen to flicker.
Is there a way to stop that using GDI+? With GDI, I was able to stop the flickering by BITBLTing the pictures in a buffer HDC before BITBLTing in the window HDC. However, it doesnt look like BITBLT works with GDI+ (unless I'm doing something wrong).
I tried using drawimage to draw to a buffer HDC and then using BITBLT to copy the buffer HDC to the window HDC, but that doesn't work.
Does anyone know an alternative way? Or a way to draw in a buffer hdc and then copying that buffer to the window hdc using GDI+, not GDI.
|
|
|
|
|
The only way I know how (and the way I use) is to use GDI+ to draw on a memory HDC (in xp at least, GDI+ is not accelerated, so you're not losing much), then Blt that using classic GDI to the main screen.
So...
void CMyView::OnDraw (CDC *dc)
{
CMemDC MemDC (dc);
Graphics gfx (MemDC);
....
}
See Flicker Free Drawing In MFC[^] for the article I refer to. I use it in 83% of my drawing code.
Iain.
I have now moved to Sweden for love (awwww).
If you're in Scandinavia and want an MVP on the payroll (or happy with a remote worker), or need cotract work done, give me a job! http://cv.imcsoft.co.uk/[ ^]
|
|
|
|
|
I recommend doing all of your drawing into a memory Bitmap and then use DrawImage. Something like this:
void YourClass::OnPaint()
{
CPaintDC dc(this);
Graphics g(dc.m_hDC);
CRect r;
GetClientRect(r);
Bitmap memBitmap(r.Width(), r.Height());
Graphics memGraphics(&memBitmap);
g.DrawImage(&memBitmap, 0, 0);
}
How To Draw on a Memory Bitmap in GDI+[^]
I also highly recommend using the CachedBitmap Class[^]. Draw your background/pictures a single time and store them in a CachedBitmap object.
Best Wishes,
-David Delaune
|
|
|
|
|
I have a two very simple client server applications that work well. Client can communicate with Server and Server too can communicate with Client.
My goal is to access the applications of servers from client like TeamViewer. For that purpose, I have created a client/server application. Now what next I should do?
|
|
|
|
|
What is 'TeamViewer'? Could you elaborate on your requirements or technical C++ problems?
|
|
|
|
|
It sounds like you are attempting to create a remote desktop viewer which is not a trivial task. If so I suggest that you look at something like TightVNC.
|
|
|
|
|
YES. U are right. I have studied the VNC source code and could not grasp it completely. I would apprecaite if you guide me step by step.
eg , as I have already developed the client/server application. so what I should do next. How to capture the remote machine applications and then how to transmitt using client/server application.
Even I am not sure , either I need the client/server application or not?
I can guees that might be I capture the remote machine desktop by using printscreen function and then transmitt this image to viewer application (client) but then the question arises , how I may perform functions over an image, like mouse click to start/close the application.
THANKS for earlier help.
|
|
|
|
|
Madhu_Rani wrote: I have studied the VNC source code and could not grasp it completely
I would go back and study it a bit more. There is a wealth of information available in open source remote desktop apps and not just in the source code. Review the change logs and the SVN history of files responsible for the functionality that you are particularly interested in. Seeing the problems they have had to deal with can give you an idea of what you might encounter during the development process. You can even go back to very early versions which might be a bit primitive but will be easier to understand.
Madhu_Rani wrote: I would apprecaite if you guide me step by step.
At a very basic level most of what you are trying to accomplish is fairly straight forward. You might approach this by having your remote desktop server capture the desktop as an image, prepare the image data and then transmit it to the client. On the client you would then simply process the incoming image data and display it in a window. Without compression however you will be transmitting a significant amount of data for each screen update.
|
|
|
|
|
THANKS for the detailed reply.
I am trying to capture the desktop as an image and then I would send the captured image to the client and if delay is on higher side, then I would do compression by using some built-in-API..
I am little confused, how a transmitted image ( from server) would responds to mouse clicks. eg If I have a click over START button of transmitted image, how server would open the START menu ... as it is an image, so how clicks would trigger the corresponding events...
I can capture these events on client machine but how these events would be mapped to server sides...
THANKS.
|
|
|
|
|
Madhu_Rani wrote: If I have a click over START button of transmitted image, how server would open the START menu
Well this is pretty straight forward as well. If you click on the image representing the remote desktop you will receive a button message (e.g. WM_LBUTTONDOWN) which includes the mouse coordinates. The message and it's associated parameters (wParam/lParam) are simply transmitted to the remote desktop server which simulates the event. Since the mouse coordinates supplied are relative to the upper left hand corner of the window client area (on the remote client side) you shouldn't have to do any conversion. To simulate mouse and keyboard events on the server side you can make a call to SendInput().
|
|
|
|
|
This was my biggest fear and now I got the idea. Any how today I gained knowledge about DC and how to capture desktop and now I am integrating all these pieces of code with client server application. Then after integration I would do the final stuff that you mentioned in your reply. THANKS
|
|
|
|
|
Hi
I tried to delete the one menu from the main menu bar. I did following:
CMenu MainMenu = ((CMainFrame*)AfxGetMainWnd())->GetMenu();
CMenu toolmenu = MainMenu ->GetSubMenu(1);
while( toolmenu->GetMenuItemCount() > 0 )
{
ret = toolmenu->DeleteMenu(0,MF_BYPOSITION);
}
I checked the "ret" value. it is "1".
But the "Tool" menu is still there. Why I can not delete it?
Best regards,
|
|
|
|
|
The code should not even compile.
GetMenu[^] and GetSubMenu[^] returns CMenu pointers.
So the declaration must be CMenu* MainMenu and CMenu* toolmenu .
The DeleteMenu[^] that you are calling will delete the first menu item in the second sub menu and not an entire menu unless there is a popup menu as the menu item. For example, in Visual Studio this menu item is the Undo item in the Edit submenu.
«_Superman_»
I love work. It gives me something to do between weekends.
|
|
|
|
|
I typed in code here. It might have some typo. It compiles.
My "DeleteMenu" return "1". From MSDN, it means "DeleteMenu" executes successfully.
But I still saw that menu over there.
Thanks,
|
|
|
|
|
hi all,
i need to get the table names from .mdb file in MFC.Can any on e help in this regard.
Thanks in advance.
|
|
|
|
|
Hi,
Did you search CodeProject or Google?
It is a crappy thing, but it's life -^ Carlo Pallini
|
|
|
|
|
I searched from both.but i didn't get the suitable one.So that only i posted this question.
|
|
|
|
|
suthakar56 wrote: I searched from both.but i didn't get the suitable one.So that only i posted this question.
Strange! CP search engine returned the following article as the first result while I searched for "MFC access":
Easy way to mix the MFC Document/View and MS Access database applications.[^]
I sure did get more relevant results when I modified the search terms. Take a look at the CDatabase[^] class as well for your need.
It is a crappy thing, but it's life -^ Carlo Pallini
|
|
|
|
|
try following, I didn't tried it my self just found on googling
SELECT [Name] from msysobjects where ([type] =1 and Flags = 0) or [type] = 6
|
|
|
|
|
Hi all
How can i convert CString to long.I am try this
CString Content="000995688787";
int newintval=atoi(Content);
result is
newintval=225212472
and
__int64 n64Tmp=_atoi64(Content);
result is
n64Tmp=225212472
|
|
|
|
|
You must have used atol (or rather _ttol ) instead of atoi . See CString::Format() member if you're already using CString .
It is a crappy thing, but it's life -^ Carlo Pallini
|
|
|
|
|
Hello, I am prashant workign with one company and doing the Graph Designing into my application. now the issue is that i create the function for populating the graph data but it takes to much time to populate data.
Can any body tell me how to reduce the time for populating data.
the following is my function from which i draw the graph data from one table.
void D_ScripGraphWnd::OnDraw(CDC *pDC)
{
CRect crect;
BOOL b;
CPen CenterPen, AxisPen, SlotPen, DrawPenRise, DrawPenFall;
CBrush brush;
CFont font;
CPen* OldPen;
CFont *OldFont;
int left, right, top, bottom, Height, Width ;
b = CenterPen.CreatePen(PS_DASH, 0, m_GraphColor_ula[REFERENCE_DATA]);
GetClientRect(crect);
Height = m_YSlots_l * m_SlotWidthY_l + 2*m_MarginY_l ;
Width = m_XSlots_l * m_SlotWidthX_l + 2*m_MarginX_l ;
crect.left = 0;
SetScaleToFitSize;
if(crect.right < Width)
crect.right = Width;
crect.top = 0;
if(crect.bottom < Height)
crect.bottom = Height;
pDC->FillRect(crect, &brush);
pDC->SetBkMode(TRANSPARENT);
left = m_MarginX_l ;
right = Width - m_MarginX_l;
top = m_MarginY_l ;
bottom = Height - m_MarginY_l;
OldPen = pDC->SelectObject(&SlotPen);
OldFont = pDC->SelectObject(&font);
char l_StartTime_ca[TIME_STAMP_LEN] = "";
char l_CurrTime_ca[TIME_STAMP_LEN] = "";
CTimeSpan l_TimeSpan_cl(0,0,30,0);
CTime l_SeriesStartTime_cl, l_SeriesCurrTime_cl;
CTime l_IndexStartTime_cl, l_IndexCurrTime_cl;
CTime l_StartTime_cl, l_CurrTime_cl;
l_SeriesStartTime_cl = l_SeriesCurrTime_cl = NULL;
l_IndexStartTime_cl = l_IndexCurrTime_cl = NULL;
ScripGraphInfo_T* l_ScripGraphInfo_stp;
l_TimeSpan_cl = CTimeSpan(0,1,00,0);
int l_i_l;
int minutes;
if(m_ScripGraphTable_clp->m_maxEntries_l != 0 && m_ScripGraphTable_clp->m_IndexQuerySuccess_b == TRUE )
{
//To find out the starting time on X-Axis.
l_ScripGraphInfo_stp = (ScripGraphInfo_T *)m_ScripGraphTable_clp->GetAt(0);
if(l_ScripGraphInfo_stp == NULL)
return;
if(l_ScripGraphInfo_stp->TimeStamp_uca[MM] >= 30)
minutes = 30;
if(l_ScripGraphInfo_stp->TimeStamp_uca[MM] < 30)
minutes = 0;
l_IndexStartTime_cl = CTime(l_ScripGraphInfo_stp->TimeStamp_uca[CC]*100 + l_ScripGraphInfo_stp->TimeStamp_uca[YY],
l_ScripGraphInfo_stp->TimeStamp_uca[MO],
l_ScripGraphInfo_stp->TimeStamp_uca[DD],
l_ScripGraphInfo_stp->TimeStamp_uca[HH],
minutes,
0);
l_ScripGraphInfo_stp = (ScripGraphInfo_T *)m_ScripGraphTable_clp->GetAt(m_ScripGraphTable_clp->m_maxEntries_l-1);
if(l_ScripGraphInfo_stp == NULL)
return;
l_IndexCurrTime_cl = CTime(l_ScripGraphInfo_stp->TimeStamp_uca[CC]*100 + l_ScripGraphInfo_stp->TimeStamp_uca[YY],
l_ScripGraphInfo_stp->TimeStamp_uca[MO],
l_ScripGraphInfo_stp->TimeStamp_uca[DD],
l_ScripGraphInfo_stp->TimeStamp_uca[HH],
l_ScripGraphInfo_stp->TimeStamp_uca[MM],
0);
}
strcpy(l_StartTime_ca, l_IndexStartTime_cl.Format("%H:%M"));
l_StartTime_cl = l_IndexStartTime_cl;
strcpy(l_CurrTime_ca, l_IndexCurrTime_cl.Format("%H:%M"));
l_CurrTime_cl = l_IndexCurrTime_cl;
pDC->SetTextColor(m_GraphColor_ula[AXIS_LABELS]);
for(l_i_l = 0; l_i_l <= m_XSlots_l; l_i_l++)
{
pDC->MoveTo(left, top);
pDC->LineTo(left, bottom);
if(m_DisplayXScale_b)
{
if(m_InitTime_cl != NULL && (l_i_l %2 == 0))
{
CTimeSpan l_Span_cl = l_StartTime_cl - l_CurrTime_cl;
if(l_Span_cl <= l_TimeSpan_cl)
pDC->TextOut(left-10 , bottom+5, l_StartTime_ca);
l_StartTime_cl += l_TimeSpan_cl;
strcpy(l_StartTime_ca, l_StartTime_cl.Format("%H:%M"));
}
}
left += m_SlotWidthX_l;
}
char l_buff_ca[20];
int j = m_Origin_l + m_YSlots_l*SlotWidthY;
left = m_MarginX_l;
for(l_i_l=0; l_i_l<=m_YSlots_l; l_i_l++)
{
pDC->MoveTo(left, top);
pDC->LineTo(right, top);
sprintf(l_buff_ca, "%d", j);
if(m_DisplayYScale_b)
{
if ((strcmp(g_SystemInfo_st.BrokerCode_ca, "") != 0) &&
(strcmp(g_SystemInfo_st.TraderCode_ca, "") != 0))
{
if(m_ScripGraphTable_clp->m_maxEntries_l > 0 )
pDC->TextOut(left - m_MarginX_l, top-2 , l_buff_ca);
}
}
top += SlotWidthY;
j -= SlotWidthY;
}
top = m_MarginY_l ;
pDC->SelectObject(&AxisPen);
pDC->MoveTo(left, top);
pDC->LineTo(left, bottom);
pDC->MoveTo(left, bottom);
pDC->LineTo(right, bottom);
pDC->SelectObject(&CenterPen);
if ((strcmp(g_SystemInfo_st.BrokerCode_ca, "") != 0) &&
(strcmp(g_SystemInfo_st.TraderCode_ca, "") != 0))
{
PrevCloseY = int(l_ScripDayOpen1)- m_Origin_l;
PrevCloseY = bottom-PrevCloseY;
pDC->MoveTo(left, PrevCloseY);
pDC->LineTo(right,PrevCloseY);
}
CString key;
char temp[20];
int x,y;
m_tooltip.Activate(TRUE);
for(l_i_l = 0; (l_i_l < m_ScripGraphTable_clp->m_maxEntries_l && m_InitTime_cl != NULL); l_i_l++)
// for( pos = g_IndexMap_cl.GetStartPosition(); pos != NULL; )
{
if(m_MinY_l == -1 || m_MaxY_l == -1)
return;
l_ScripGraphInfo_stp = (ScripGraphInfo_T* )m_ScripGraphTable_clp->GetAt(l_i_l);
if(l_ScripGraphInfo_stp == NULL)
return;
strcpy(l_CurrTime_ca, "");
if(l_ScripGraphInfo_stp->TimeStamp_uca[HH] < 10)
strcpy(l_CurrTime_ca, "0");
sprintf(temp, "%d",l_ScripGraphInfo_stp->TimeStamp_uca[HH]);
strcat(l_CurrTime_ca, temp);
if(l_ScripGraphInfo_stp->TimeStamp_uca[MM] < 10)
strcat(l_CurrTime_ca, "0");
sprintf(temp, "%d",l_ScripGraphInfo_stp->TimeStamp_uca[MM]);
strcat(l_CurrTime_ca, temp);
l_CurrTime_cl = CTime(l_ScripGraphInfo_stp->TimeStamp_uca[CC]*100 + l_ScripGraphInfo_stp->TimeStamp_uca[YY],
l_ScripGraphInfo_stp->TimeStamp_uca[MO],
l_ScripGraphInfo_stp->TimeStamp_uca[DD],
l_ScripGraphInfo_stp->TimeStamp_uca[HH],
l_ScripGraphInfo_stp->TimeStamp_uca[MM],
0/*l_ScripGraphInfo_stp->TimeStamp_uca[SS]*/);
CString l_inittime_cl = m_InitTime_cl.Format("%H, %M, %S");
CString l_currtime_cl = l_CurrTime_cl.Format("%H, %M, %S");
l_TimeSpan_cl = l_CurrTime_cl - m_InitTime_cl;
CString l_timespan_cl = l_TimeSpan_cl.Format("%H, %M, %S");
x = l_TimeSpan_cl.GetTotalMinutes();
if(x < 0)
{
IF_ERROR1(1, "Invalid time rcvd.");
return;
}
y= (int)l_ScripGraphInfo_stp->ScripValue_l/10000 ;/// 100;
if(y == 0)
continue;
AdjustSlots(x, y);
y -= m_Origin_l;
if(l_i_l == 0)
{
pDC->MoveTo(left+x, bottom-y);
if(l_ScripGraphInfo_stp->ScripValue_l/10000< l_ScripDayOpen1)
pDC->SetPixel(left+x, bottom-y, m_GraphColor_ula[INDEX_FALLING]);
else
pDC->SetPixel(left+x, bottom-y, m_GraphColor_ula[INDEX_RISING]);
}
CPoint l_CurrPt_cl = pDC->GetCurrentPosition();
if(l_ScripGraphInfo_stp->ScripValue_l/10000 < l_ScripDayOpen1)//Prev Close
// if((long)indexval < m_ScripGraphTable_clp->l_ScripDayOpen1)
pDC->SelectObject(&DrawPenFall);
else
pDC->SelectObject(&DrawPenRise);
switch(m_GraphStyle_s)
{
case LINESTYLE :
//Consider origin as (left,top) of the window, so graph grows down.
if(l_CurrPt_cl.y < PrevCloseY && (bottom-y) >= PrevCloseY)
{
pDC->SelectObject(&DrawPenRise);
pDC->LineTo(left+x, PrevCloseY);
pDC->SelectObject(&DrawPenFall);
pDC->LineTo(left+x, bottom-y);
}
if(l_CurrPt_cl.y > PrevCloseY && (bottom-y) <= PrevCloseY)
{
pDC->SelectObject(&DrawPenFall);
pDC->LineTo(left+x, PrevCloseY);
pDC->SelectObject(&DrawPenRise);
pDC->LineTo(left+x, bottom-y);
}
pDC->LineTo(left+x, bottom-y);
break;
case BARSTYLE :
pDC->MoveTo(x+left, PrevCloseY);
pDC->LineTo(x+left, bottom-y);
break;
default :
if(l_ScripGraphInfo_stp->ScripValue_l/10000 < l_ScripDayOpen1)
pDC->SetPixel(x+MarginX, bottom-y, m_GraphColor_ula[INDEX_FALLING]);
else
pDC->SetPixel(x+MarginX, bottom-y, m_GraphColor_ula[INDEX_RISING]); break;
}
}
pDC->SelectObject(OldFont);
pDC->SelectObject(OldPen);
CenterPen.DeleteObject();
AxisPen.DeleteObject();
SlotPen.DeleteObject();
DrawPenRise.DeleteObject();
DrawPenFall.DeleteObject();
brush.DeleteObject();
font.DeleteObject();
return;
}
Can any body tell me how to reduce time to populating data.
|
|
|
|
|