|
In terms of data structures, I mean you can store file data into two data structures such as a character array, string, vector, link list and the like.
You used strcmp, but then direct the parameter to two files. I am not sure if strcmp can open two files on its owns.
Kuphryn
|
|
|
|
|
I have a socket server app that accepts a connection from our data feed (we send out financial data for various markets). This server has several threads, one for reading from the socket, and another for parsing the current chunk of data read in by the socket read thread. THe parse thread in turn adds a new instance of a data object (created by parsing the incoming data from the socket) to list. This list is protected by a critical section for both reads and writes. The two threads each have an event which is used to wake them up whenever new data is available.
All of this is set up as a DLL, that is then used as a library for some specific application.
When used in a console app this works fine, runs on single processor, or multi-processor machines like a charm - no mem leaks, no race cond, and stays up for weeks (the longest I kept is running was for 4 weeks - I shut it down to test something else).
Sadly, when the DLL is used in an MFC this is no longer the case. On single processor machines it seems to run fine. On our multi-processor Winnt4 box it crashes within an hour.
The gui app is a simple MFC dialog app, that uses a tray icon to remain invisible. When the icon is double click it displays a dialog with some list views that display various stats - like how many data packets have been recv, how many bytes, etc.
The libary works by creating a library object and then inside of a loop somewhere repeated calling a handleEvent() function. In the console program the code looks something like this:
GTIS::Application& app = GTIS::Application::getApp();
printf( "Starting application loop...\n" );
int count = 0;
while ( true ) {
app.handleEvents(5);
count ++;
Sleep(0);
}
In the MFC app I have a timer that fires off every 10 milliseconds
in the call back for the timer message I call the
handleEvents() method
the crash occurs when the library goes to delete one of the newly allocated data objects (that was created from parsing the incoming socket data). This crash NEVER occurs in a single processor box only multiprocessor - the call stack is deep in the destructor code - curiously enough in the STL basic_string code, since the data object has a number of string members.
I have been staring at the code for days and I can think of nothing other than perhaps now would be a good time to open that lawn care business...
Thanks in advance !
|
|
|
|
|
Are you using the MT version of MFC and C runtime lib (check the compiler options for the /MD /MT flag can't remember which). I found that it is usually set differently for MFC and Console apps, and as a result can have an effect when moving code over.
If nothing else works and its crashing for no reason some where in the STL, then how about trying another STL lib like STLPort?
Are there any static variables that are not declared as 'volatile' to make sure access to them is atomic?
These will not come into play on a single processor machine, but I've seen this on a dual machine, when a variable has been updated but is still in the other processors cache.
Finally are you using _beginthreadex or CreateThread?
|
|
|
|
|
Giles wrote:
Are you using the MT version of MFC and C runtime lib
Yeah, MultiThreaded DLL for all versions, the lib and the app's that link to it
Giles wrote:
Are there any static variables that are not declared as 'volatile' to make sure access to them is atomic?
I do have some static variables. They are NOT declared as volatile. If I understand you, you're saying that I should declare them as volatile ?
|
|
|
|
|
yup yup
volatile means the processor wont cache them onboard in a reg which means they will always be the value they should be as opposed to the value they might have been
"... and so i said to him ... if it don't dance (or code) and you can't eat it either f**k it or throw it away" sonork: 100.18128 8028finder.com
|
|
|
|
|
Jim Crafton wrote:
If I understand you, you're saying that I should declare them as volatile ?
Yep as Lauren pointed out.
Check out these functions in the kernel32. They make for interesting reading, that only come into play where you use 2 or more processors.
InterlockedExchangeAdd
InterlockedCompareExchange
InterlockedIncrement
InterlockedExchange
Basically the OS takes care that another thread will not access the given variable while you perform the given operation. There are a few others as well.
Good luck, and happy hunting.....
|
|
|
|
|
Ok. This is different from most questions about "what is my ip address". Some background: I'm developing an application that will record data UDP broadcast or multicast from a standalone device.
Technicians will take their laptops with them when on travel from their home/office and plug it into this device's network port with a crossover cable (private LAN) to facilitate recording of data. The device can be assigned a private IP address but I don't think this is the best choice since this would require the technician to modify his existing IP address, etc. in Windows, and cause headaches when he goes back to the office. This is just an example. In some situations some technicians and others will want to record data from their desktop PCs if the data is local to their facility. Thus, I would not know their local IP addresses at these types of facilities.
I would like to program the device to reply to a message broadcast from my application (laptop) and set it's ip address accordingly. Since, the laptop and standalone device could be on two different subnets (or even nets, I think) though physically hooked up, I think I would have to send a broadcast IP message telling this device what my IP address is so that he can set his address to one within the same subnet/net, right? Or, a second option would be to pick a pre-defined IP address for the device and have my application on the laptop send it a message with it's source IP address in the IP header and have the device dynamically change it's IP address to the same subnet/net.
The device is a UNIX flavored RTOS type. I think POSIX. Don't know if there is a way to dynamically change its IP address. If anyone knows a command, please let me know. BTW, we have application source code for the device that we developed.
Any comments or suggestions? This isn't totally off-topic here since I want to know what would be the best method for performing this under Windows and if there are any alternative ways. Of course, if anyone knows UNIX or POSIX that's helpful too.
|
|
|
|
|
I would like to program the device to reply to a message broadcast from my application (laptop) and set it's ip address accordingly.
This is essentially what DHCP or BOOTP does. Install a DHCP server on the laptop, and configure the device to get its net address by DHCP.
|
|
|
|
|
Thanks for the reply. You're right that would probably be the easiest thing to do but setting up a DHCP server on every laptop wouldn't be that easy.
|
|
|
|
|
I am trying to create a simple dialog box with scrolling horizontal text (marquee) in Visual C++. I feel like I've seen a really easy method somewhere but can't remember. Any help would be appreciated
Thank you
|
|
|
|
|
there isnt any easy function in mfc or c++ to do that but its not too hard to scroll text across a static control on a timer interval
there are a number of classes here to do that ... they might be useful if you decide to write your own
"... and so i said to him ... if it don't dance (or code) and you can't eat it either f**k it or throw it away" sonork: 100.18128 8028finder.com
|
|
|
|
|
Scrolling text my forte:
Many ways to skin a cat.
Using TextOut and SetBoundsRect and select clipping regions give optimal scrolling.
Also you can use ScrollWindow and paint the bits uncovered.
Normski. - Professional Windows Programmer
|
|
|
|
|
For sorting a listcontrol on header-click, I use the following code:
But the condition if (iIndex1 == iIndex2) in CompareItems is always true.
The callback func is always called to compare an item with the same item.
And this happens for each item in the list....
Anyone already seen this behaviour? Any solve for this ?
Greetings,
Niko
//Sorting
static int CALLBACK CompareFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
{
if(!lParamSort) return 0;
CListCtrlEx* pListCtrl = (CListCtrlEx*) lParamSort;
ASSERT_VALID(pListCtrl);
return pListCtrl->CompareItems( lParam1, lParam2);
}
void CListCtrlEx::SortOnCol(int iCol)
{
if(m_iSelColumn == iCol)
m_iSortDirection = -m_iSortDirection;
else
{
m_iSelColumn = iCol;
m_iSortDirection = 1;
}
//Set callback
SortItems( CompareFunc, (DWORD)this );
//Show first Item
int iItem = GetNextItem(-1, LVNI_SELECTED);
if (iItem > 0)
EnsureVisible(iItem,true);
}
int CListCtrlEx::CompareItems(int iIndex1, int iIndex2)
{
const int iMaxSortCol = 100;
char szBuf1[iMaxSortCol]; memset(szBuf1,0x00,sizeof(szBuf1));
char szBuf2[iMaxSortCol]; memset(szBuf2,0x00,sizeof(szBuf2));
if (iIndex1 == iIndex2)
return 0; //Shouldn't be possible
LV_FINDINFO FI;
FI.flags = LVFI_PARAM;
FI.lParam = iIndex1;
iIndex1 = FindItem( &FI);
if(iIndex1<0)
return 0;
GetItemText( iIndex1, m_iSelColumn, szBuf1, iMaxSortCol-1 );
FI.lParam = iIndex2;
iIndex2 = FindItem( &FI);
if(iIndex2<0)
return 0;
GetItemText( iIndex2, m_iSelColumn, szBuf2, iMaxSortCol-1 );
return m_iSortDirection*strcmp(szBuf1, szBuf2);
}
void CListCtrlEx::OnColumnclick(NMHDR* pNMHDR, LRESULT* pResult)
{
CWaitCursor Wait;
NM_LISTVIEW* pNM = (NM_LISTVIEW*)pNMHDR;
// TRACE("Clicked on column %d - Sorting: %s.\r\n", pNM->iSubItem, ((m_iSortDirection == m_iSortDirectionDsc)?"DSC":"ASC" ));
SortOnCol(pNM->iSubItem);
*pResult = 0;
}
|
|
|
|
|
How do you set the item data of a list item ?
We do not inherit the Earth from our ancestors. We borrow it from our children.
Antoine de Saint Exupéry (1900-1944)
|
|
|
|
|
The values you're passing into the CListCtrlEx::CompareItems() method are the item data values of the items to be compared, not their indexes (which you've obviously realised, because your code is using FindItem to search for the LPARAM - sorry for stating the obvious!). Are you sure that the item data values are set correctly and uniquely for each list item?
"We are the knights who say Ni" (The Knights Who Say Ni)
|
|
|
|
|
Thank you for your help,
That was 'obviously' the problem...
Greetings,
Niko
|
|
|
|
|
If you're after every single message, you might be interested in PreTranslateMessage() . It's called in the window that receives the message, which then calls PreTranslateMessage in its parent window, all the way up to the main frame window.
"We are the knights who say Ni" (The Knights Who Say Ni)
|
|
|
|
|
Seems to work, but not all messages.
For example, when I press Ctrl+4, or any other SysCommand, the Frame Window doesn't get any message.
What I basically want to do is monitor inter-mdichild messages (switching windows, closing a window, etc). It seems that the messages stop at the Client Window and doesn't travel up to the Frame Window.
Some other ideas
|
|
|
|
|
|
Thanks for the infos
|
|
|
|
|
|
Hello,
Still struggling with the linker. Many things have been solved, but still (at least) one left.
So, I have mylib.lib, which is written in C and I'm using it in my console program, that uses also MFC. I have the source code of that lib, but I can't change it. I use makefile for compiling and linking.
I have (among others) compile options /TP /MT, so it's C++ and multithreaded. With a linker option /NODEFAULTLIB:libc.lib, I avoid crashes with libcmt.lib.
So I compile my selfmade files and then link the .obj files and .libs and I get error LNK2001: unresolved external symbol _errno. The place where I get this error from is in one file in mylib. I checked, that it includes header errno.h.
If I remove linker option /NODEFAULTLIB:libc.lib, this error of _errno disappears, but I get lots of errors, because libc and libctm define same functions. Or if I make it single threaded, the problem is with _begingthreadex and _endthreadex.
I don't understand. Why doesn't the linker realize errno? Could it be because of messing with C and C++?
-Janetta
|
|
|
|
|
The way out of this dilemma is to have both componentes (the MFC program and the lib) compiled for the same version of the C run-time library. Go to the project settings for your MFC app and select (in the first tab) "Use MFC in a static library". Then go to the C/C++ tab, category "Code generation" and set "Use run-time library" to "Multithreaded". Good luck.
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
Janetta wrote:
Could it be because of messing with C and C++?
Yes it could. Try doing this:
extern "C"
{
#include "header_written_in_C_used_from_c_plus_plus"
}
This will inform the C++ compilers to use C calling conventions, not C++ ones which are by default.
Best regards,
Alexandru Savescu
|
|
|
|
|
I have a toolbar that under certain conditions i may need to diable an item.
how can this be done?
the only set function i can find is SetButtonInfo.
i tried to change the state of the toolbar button to point to an id with out a function. i noted if i do that as i am building the toolbar and did not yet attach a function to it that is shows diabled.
i tried the following
CToolBar* pBar = &m_wndToolBar;
if (pBar != NULL) {
pBar->SetButtonInfo(11,ID_NULL,TBBS_BUTTON,11);
}
this had no effect on the toolbar.
thank you for any help
|
|
|
|