|
I've had a quick look, and IMHO the code is severely lacking. Most importantly, the classes are often passed by value instead of by reference. Therefore, every time a hard copy is created. Apart from potential issues regarding performance and memory for more complex object instances, this also means that any code attempting to modify an object using these calls will not work!
Example: the destructor code passes the object that is to be destructed by value!? This will allow you to clean up resources such as pointers to allocated secondary objects, but if you're managing something more complex, such as a database connection or file handle, you're going to have severe issues, if you can properly clean this up at all!
Similarly, the sound() functions create local hard copies of the object that in this case is correctly being passed by reference. Why the author did this is beyond me - it generates needless overhead and ensures that you can not modify the original object. Most importantly, modifying the local copy will not change the behavior of the referenced object that was passed to the function, i. e. the effect of these functions is zero!
Also the example main function doesn't show inheritance at all, because it always directly calls the sound() function on the class interface it is created as (e. g. calling dog.sound() ). The only way to prove there is something like virtual inheritance at play is to store the cat and dog objects as animal references, and call the sound() function via the animal class interface. Without actually trying, I predict however, it won't work, because the "derived" sound() functions won't be called, and, therefore, the super class won't adjust it's behaviour.
Always referencing class objects through pointers would go a long way to fix the code and get it towards what the author originally intended. But that still wouldn't introduce polymorphism or encapsulation.
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)
|
|
|
|
|
Hi Lukeer !
Returning a struct is indeed valid C. It's even given as example in section 6.8.6.4 of the C11 standard, which deals with the return statement.
The return is done with a dumb bit by bit copy. This works very well for struct composed of fundamental types. But as soon as you have pointers in the struct, you need to be extremely careful about who owns an object and shall invoke (manually) the destructor: there is a high risk of nasty memory deallocation errors here !
The more efficient approach would be to return pointers as you suggested. But then, you'll have a high risk of memory leakage, in case the caller is not interested in the returned value (in other functions than the constructor).
The safer way would be to design the API to encourage the caller to provide a pointer to the struture to be used, that he is responsible of (whether it's mallocated or local):
class_animal a,*pa;
newClassAnimal(&a);
pa = newClassAnimal (NULL);
I used very long time ago these kind of structures. The function pointers prove to be very efficient way to implement polymorphism in a non object language like C. However the management of the object has its drawback: no automatic destruction, and hence high risk of leakage for more complex execution flows, and especially if you use setjmp/longjmp.
|
|
|
|
|
I wrote a VC++ 6.0 application few years back that we still use. Its basically a Dialog based application with a parent dialog and few child dialogs. Most of the child dialogs have something to do with drawing upon requests from parent dialog. The application itself is multi- threaded. Most of the drawing on some dialogs is done using gdiplus and for a couple using just MFC drawing functions. The only issue is have with this is that it flickers a bit and that flicker rate is different on different systems.
We are now thinking of redeveloping the application on VS2010 as support for XP which supports VC++ 6.0 has ended and its getting harder and harder to get PCs with XP on them or even to install XP. I am looking at some best options to choose from to implement in VS2010 for real-time drawing without flicker issues while the core structure will be the same for most part. We will be sticking with dialog based application again as we don't see any real use for SDI or MDI based application in our case. The core of the program is dependent on frames captured from a scientific camera and do some analysis on that frame and do the following
Draw the captured frame on a dialog and draw some graphics (squares/lines) on the same frame based on analysis
4 more dialogs (in which one will have plots in it) will draw/refresh with new analysis data for that frame. I am looking at few options like
OpenCV (which we already use in other applications)
Direct2D (my application doesn't involve any 3D graphics)
GdiPlus (not sure if this works better on VS2010)
Any suggestions?
thanks
PKNT
|
|
|
|
|
GDI+ works. I suppose flickering is an application problem (it is not a library fault).
If you already know OpenCv then using it is an option (as far as I know is rather optimized).
I would use Direct2D only if performance really matters, because (I suppose) the learning step could be heavy.
On a completely different approach, I would also consider using C# instead of C++ with MFC .
|
|
|
|
|
Thanks for your reply Pallini, I need to interface external hardware in this application that come with C++ api than C#, so I am a bit stuckw ith C++/MFC combo I guess.
Basically I get a 512x512 8bit pixel data from a framegrabber which I need to display in realtime and even draw some graphics on this image as needed (mostly squares, dots and lines) based on a colorbar.
PKNT
|
|
|
|
|
You need to double buffer the output. Create a bitmap in memory; copy from your frame to that bitmap, then draw your extra graphics on that bitmap. Then when it's done draw it to the screen in one blit. That's how you get rid of the flicker. Gdi+ will do it fine.
|
|
|
|
|
Hi
I have the following code
INT16 asid; // has a value of 1
CString buffer = ",Asid="
buffer.Format("%s,%x",buffer,asid);
the result is ",Asid,1";
My question is why the trailing comma
Thanks
|
|
|
|
|
What trailng comma? This is what you have (and the use of <pre> tags make it so much easier to read):
INT16 asid = 1;
CString buffer = ",Asid=";
buffer.Format("%s,%x",buffer,asid);
|
|
|
|
|
I meant the comma following the asid,
Jochen explained my mistake
|
|
|
|
|
You put it there in your format string.
|
|
|
|
|
|
You are passing the object itself as parameter to the Format function. This will lead to unpredictable results.
See CStringT::Format[^]:
Quote: The call will fail if the string object itself is offered as a parameter to Format.
|
|
|
|
|
So if I did
CString buffer = ",Asid="
buffer.format("%2x",asid);
Would I get ",Asid=01"
Which is what I am looking for
|
|
|
|
|
ForNow wrote: Would I get ",Asid=01" I don't believe so, I think you'd get "01" as the format() would overwrite your initial value.
How about trying.
CString buffer;
buffer.Format(",Asid=%2x", asid);
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment
"Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst
"I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle
|
|
|
|
|
Thanks so much I got to get into the habit of using CString
|
|
|
|
|
Try making a small test project and play around with the format syntax and the specifiers. Knowing it will help you with other (non-CString) areas as well.
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment
"Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst
"I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle
|
|
|
|
|
Hi,
I'm developing a client-server C language in Windows.
The client connects to server and every 5 seconds send a message. The communication is OK and server receives messages.
I try to develop multi-client (.bat file starts client.exe) - server but I have problem: A segmentation fault appears after n messages.
This is my code:
main
if (WSAStartup(MAKEWORD(2,2),&wsa) != 0){
printf("Failed. Error Code : %d",WSAGetLastError());
return 1;
}
if((s = socket(AF_INET , SOCK_STREAM , 0 )) == INVALID_SOCKET){
printf("Could not create socket : %d" , WSAGetLastError());
}
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons( 8888 );
if( bind(s ,(struct sockaddr *)&server , sizeof(server)) == SOCKET_ERROR){
printf("Bind failed with error code : %d" , WSAGetLastError());
}
listen(s , 10000);
puts("Waiting for incoming connections...");
while( TRUE ) {
new_socket = accept(s , (struct sockaddr *)&client, &c);
if (new_socket == INVALID_SOCKET){
printf("accept failed with error code : %d" , WSAGetLastError());
}else{
clientInfo[cont].clientAddr = client ;
clientInfo[cont].hClientSocket = new_socket;
hClientThread[cont] = CreateThread(NULL,0,( LPTHREAD_START_ROUTINE ) cliTh1,( LPVOID ) &clientInfo[cont],0,&dwThreadId ) ;
if ( hClientThread[cont] == NULL ){
printf("Unable to create client thread");
} else {
printf("thread OK\n");
CloseHandle( hClientThread[cont] ) ;
cont++;
}
}
}
Thread function (cliTh1)
DWORD WINAPI cliTh1( LPVOID lpData ){
struct CLIENT_INFO *pClientInfo;
char szClientMsg[250];
static int j = 0;
int i = 0;
char *message;
pClientInfo = (struct CLIENT_INFO *)lpData ;
printf("SOCKET:%d - THREAD_ID:%ld\n", pClientInfo->hClientSocket, GetCurrentThreadId());
while ( 1 ){
if(WSAGetLastError()){
printf("CURRENT:%ld\n", GetCurrentThreadId());
closesocket(pClientInfo->hClientSocket);
ExitThread(GetCurrentThreadId());
}
if(recv( pClientInfo -> hClientSocket, szClientMsg, sizeof( szClientMsg ), 0 ) > 0){
if(j>5000){j=0;};
strcpy(bufferRx[j].packet, szClientMsg);
for(i=0; i < sizeof( szClientMsg ); i++){
szClientMsg[i] = 0;
}
printf("RECEIVE %d: %s",j, bufferRx[j].packet);
j++;
}
}
return(TRUE);
}
I read on Internet, but I don't know where is Error.
Thanks.
|
|
|
|
|
You are accepting an unlimited number of connections but did not check if cont reaches the limit of your clientInfo and hClientThread array sizes. This results in segmentation faults when cont is equal to the number of array items.
|
|
|
|
|
Thanks for reply.
I declared:
HANDLE hClientThread[65000]
struct CLIENT_INFO clientInfo[10000]
I launch 20 client process.
|
|
|
|
|
I find my error!!!! thanks a lot. You advised me to control array size....
|
|
|
|
|
So that is not the error source. But why you are using different sizes?
Another possible source is in these lines in the thread function:
if(j>5000){j=0;};
strcpy(bufferRx[j].packet, szClientMsg);
This requires that the packet contains a null terminated string. You are also using a constant value for the bufferRx size without showing the definition or allocation. It would be better to use a variable when it is allocated or sizeof() when using a fixed size.
|
|
|
|
|
Thanks for your advices!!!! I will correct code source.
|
|
|
|
|
I picked up a function RecursiveDelete on the internet, as I needed to erase all files and folders on an SD card (as part of a larger project). I was having problems insofar as the function would fail on removing the folders after CFileDialog DoModal - there were two nested folders, the deepest failing with code 32 ("in use by another proicess") and the shallowest with 145 ("folder not empty" - expected !)
However, if I call the function BEFORE the DoModal, then everything works fine.
I have extracted the relevent code into a simpler project and it still fails
Perhaps some kind soul can throw some light on why this is so !!
CFileDialog dlgFileBrowse(true);
UINT uiFileBrowseDlgRC;
RecursiveDelete("F:");
uiFileBrowseDlgRC = dlgFileBrowse.DoModal();
void CDeleteFolderTestDlg::RecursiveDelete(CString szPath)
{
CFileFind ff;
BOOL bResult;
CString path = szPath;
if(path.Right(1) != '\\')
path += '\\';
path += "*.*";
bResult = ff.FindFile(path);
BOOL bItemDelete;
DWORD dwLastError;
CString szFilePath;
while(bResult)
{
bResult = ff.FindNextFile();
if (!ff.IsDots() && !ff.IsDirectory())
{
szFilePath = ff.GetFilePath();
bItemDelete = DeleteFile(szFilePath);
if(!bItemDelete)
dwLastError = GetLastError();
}
else if (ff.IsDirectory() && !ff.IsDots())
{
path = ff.GetFilePath();
RecursiveDelete(path);
bItemDelete = RemoveDirectory(path);
if(!bItemDelete)
dwLastError = GetLastError();
}
}
}
Doug
modified 16-Jun-15 3:39am.
|
|
|
|
|
I assume that the CFileDialog object contains a handle to the directory once it's opened and that the handle is freed when the dialog object is destroyed. You could try the following:
CFileDialog * dialog = new CFileDialog(true);
dialog->DoModal();
delete dialog;
RecursiveDelete("F:");
Alternatively you could try:
{
CFileDialog dialog(true);
dialog.DoModal();
}
RecursiveDelete("F:");
The good thing about pessimism is, that you are always either right or pleasently surprised.
|
|
|
|
|
Snap.
You beat me by seconds with the solution.
|
|
|
|
|