|
if we use packed structure in c thne there is no byte padding...can we such technique in VC++
birajendu
CyberG India
Delhi
India
|
|
|
|
|
Yes, you can use pragma pack directive for that. Look at the bottom of this page[^] for an example
But you still didn't answer my question: what is the purpose of doing that this way ?
Cédric Moonen
Software developer
Charting control
|
|
|
|
|
if we use packed structure in c thne there is no byte padding...can we such technique in VC++
birajendu
CyberG India
Delhi
India
|
|
|
|
|
Two approaches often used:
1) open the file and read the entire content into a buffer.
2) create a file-mapping and map the file content to your memory buffer.
Don't load the file content to a sturcture directly. Load it to a buffer (string) first and then do whatever you like with it.
Best,
Jun
|
|
|
|
|
Hi Gurus!
I have an MFC activeX component which creates a memory leak in the application that uses it. The leak is when exiting program. I have created a small example to show you what happens.
In my activeX component I have this method;
Code:
LONG CThursdayActiveXCtrl::getSomething(VARIANT* pVar)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
// A safe array of float
CComSafeArray <float> saVars;
// Insert elements into the CComSafeArray
saVars.Add(1); //THIS LINE GIVES A LEAK!!! SEE BELOW
saVars.Add(32);
saVars.Add(43);
// A CComVariant to return to the client
CComVariant varReturn (saVars.Detach ());
varReturn.vt=VT_R4;
return varReturn.Detach (pVar);
}
I have added my activex control to a dialog based MFC application. I have a button, when that button is pushed this method is called:
Code:
void Ctest1Dlg::OnBnClickedButton1()
{
CComVariant varArray;
// Get the array from the server
test.getSomething(&varArray);
// Attach the safe array
CComSafeArray <float> saFromVariant;
saFromVariant.Attach (varArray.parray);
a=saFromVariant.GetAt(0);
b=saFromVariant.GetAt(1);
c=saFromVariant.GetAt(2);
UpdateData(false);
}
My debugging tool, which is DevPartner gives me the following memory leak:
Memory Leak Exiting Program: Address 0x024101A0 (40) allocated by SafeArrayCreate.
The call stack gives me this lines of code causing the leak;
atlsafe.h 534
atlsafe.h 521
atlsafe.h 373
getSomething 195 (which is the line I have marked in the code above).
Can anyone help me out here? This leak is 40 bytes big, and that will become quite a large amount when the method in my real control is invoked approx. 5 times a second!!
Thanks for any help!
-- modified at 14:12 Thursday 29th June, 2006
|
|
|
|
|
pierre_ribery wrote: varReturn.vt=VT_R4;
This is part of your problem. It should be the following:
varReturn.vt = VT_R4 | VT_ARRAY;
Otherwise, when the "smart" classes call ::ClearVariant, it won't be performing the proper cleanup procedure.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
Thanks for your reply!
You say part of my problem, is there more that could be corrected?
And, btw, the leaks are still there after I made the change you suggested! I am really lost of how to fix this!
Pierre.
|
|
|
|
|
I said "part of" because I believe you don't need the Detach/Attach methods as well (just not sure on that ... its been a little while since I dealt with Microsoft's COM wrappers).
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
I think one of the problems is this assignment:
varReturn.vt = VT_R4;
I suppose this is unneeded, since varReturn.vt already contains a value, which is VT_ARRAY | VT_R4 .
What happens when you comment this line?
Next, the constructor at
CComVariant varReturn(saVars.Detach());
creates a copy of passed SAFEARRAY object, therefore the source detached object is not deleted and causes memory leaks. I think you should first try this:
CComVariant varReturn((LPSAFEARRAY)saVars);
If it works, you can reconsider your getSomething function and return a SAFEARRAY pointer directly, detached from saVars . In the other function, attach this pointer to a CComSafeArray variable. It will be deleted automatically.
I hope it helps.
-- modified at 10:22 Thursday 29th June, 2006
|
|
|
|
|
Thanks mate!
There is no rating system here right? (well you got it anyway somewhere!)
|
|
|
|
|
Hello,
I need a timer in a user class which calls a member function every 2 seconds. I have tried some timers which have a callback function. But it not possible to call a member function from a callback function.
How can I solve this problem?
Thx for help.
|
|
|
|
|
jsumm wrote: I have tried some timers which have a callback function
Which ones ? Could you post some more info about that ?
Basically if you can supply data (through a parameter) that is passed to your callback function, then you can simply pass the this parameter and convert it back to a class pointer in your callback function. Then you are able to call any public function of your class.
Cédric Moonen
Software developer
Charting control
|
|
|
|
|
Thank you for your help.
I've found a timer tutorial an I'm trying to develop a Multimedia Timer.
Here the source in my user class.
void CALLBACK TimerFunction(UINT wTimerID, UINT msg, DWORD dwUser, DWORD dw1, DWORD dw2)
{
// This is used only to call MMTimerHandler
// Typically, this function is static member of CTimersDlg
CMyClass* obj = (CMyClass*) dwUser;
obj->MMTimerHandler(wTimerID);
}
//----------------------------------------------------------------------------------------
void CMyClass::StartTimer()
{
m_nTimeout = 2000;
TIMECAPS tc;
timeGetDevCaps(&tc, sizeof(TIMECAPS));
DWORD resolution = min(max(tc.wPeriodMin, 0), tc.wPeriodMax);
timeBeginPeriod(resolution);
// create the timer
m_idEvent = timeSetEvent(
m_nTimeout,
resolution,
::TimerFunction,
(DWORD)this,
TIME_PERIODIC);
}
//----------------------------------------------------------------------------------------
void CMyClass::StopTimer()
{
// destroy the timer
timeKillEvent(m_idEvent);
// reset the timer
timeEndPeriod (m_nTimeout);
}
//----------------------------------------------------------------------------------------
void CMyClass::MMTimerHandler(UINT nIDEvent) // called every elTime milliseconds
{
// do what you want to do, but quickly
static int a = 0;
CString str;
str.Format("Timer-Event %d", a++);
m_pTargetDlg->SetState(str);
}
//----------------------------------------------------------------------------------------
It's done in the way you expained, I think. But when the last line of code is done in the MMTimerHandler function (it calls the following Methode in a Dialog class)
void CTestDlg::SetState(CString strState)
{
m_strStatus = strState;
UpdateData(FALSE);
}
it crashes with a Debug Assertion Failed!- Error.
File wincore.cpp
Line 879
Any idea why?
Juergen
|
|
|
|
|
It is better to send a user defined message to your dialog class. And in the event handler of this message, update the data.
Cédric Moonen
Software developer
Charting control
|
|
|
|
|
If I do it in that way
m_pTargetDlg->SetWindowText(str);
it works.
But this is only for testing.
I am trying now to call a member function, which tries a cyclic reconnect to a telnet server after the connection has been closed by the server side.
The connect method works, but if I start it in the timer callback it crashes.
The timer only runs ones. After the first callback I stop the timer and call the connect method, but it crashes.
Hard work, I don't know why...
|
|
|
|
|
Did you read my previous post ? Did you implement it ?
Cédric Moonen
Software developer
Charting control
|
|
|
|
|
I am trying to write an OPC server, opcfoundation.org is no help as one need to be a member of the orgaization. I tried some ActiveX, but with limited flexibility.
Can anyone help?
Sanjay
|
|
|
|
|
Hello
mouse_event(MOUSEEVENTF_RIGHTUP,x,y,0,GetMessageExtraInfo());
it works!
But with MOUSEEVENTF_LEFTUP and MOUSEEVENTF_ABSOLUTE nothing happens!
I think that the fanction has a problem . I write my program in Borland C++Builder version 6. can you help me?
thanks
|
|
|
|
|
Did you do MOUSEEVENTF_LEFTDOWN before MOUSEEVENTF_LEFTUP?
|
|
|
|
|
No , but I think that isn't important. because when I use MOUSEEVENTF_RIGHTUP I dont use MOUSEEVENTF_RIGHTDOWN before it but it works .
thanks
|
|
|
|
|
That would depend on whatever application that is the receipient of the mouse clicks, and I would imagine that a programmer won't think of a situation in which the mouse button can go up without going down first. Just because it "works" in certain cases doesn't mean it's correct. If you had tried simulating keyboard input with keys going up without coming down first you get fantastic results.
Have your taskbar be on the bottom of your primary monitor and visible and run this snippet. Does the start menu pop up? Works for me...
mouse_event(MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE, 0, 65535, 0, GetMessageExtraInfo());
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0 ,0,GetMessageExtraInfo());
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0 ,0,GetMessageExtraInfo());
|
|
|
|
|
|
I am a beginner, trying to make an application in Borland C++ builder that can monitor the chat communication(openned port as file using windows API) going on serial port, I read different funcitons but couldn't understand. it will be so kind of you to guide me how to create a hook to sniff the in and out traffic on serial port thanks
Akram Mahmood
|
|
|
|
|
See if this article helps.
"The largest fire starts but with the smallest spark." - David Crow
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
Hi,
I am coding an application in vc++6, mfc. I have some sort of bar graph in which when I click on one of the bars a small dialog pops up giving information about that bar. I want the dialog to close automatically when I click anywhere outside it and not when an OK button is pressed. I tried with OnKillFocus and EndDialog but it is not working.
void CChartInfoDisplay::OnKillFocus(CWnd* pNewWnd)
{
EndDialog(0);
CDialog::OnKillFocus(pNewWnd);
}
Please can you tell me how to make a dialog box close when the user click anywhere outside it?
Thanks,
Tara
Fortitudine Vinsinues!
|
|
|
|