|
Hi All.
I call the following when I need to save a report in my MFC base program:
m_pEndThread = AfxBeginThread(Save2CWGCoilRep, &m_CoilRep[iCRepPos], THREAD_PRIORITY_NORMAL,0,0);
UINT Save2CWGCoilRep(LPVOID pParam)
{
I use CStdioFile to them save data to file
}
Then before I save another report, I will call
if(m_pEndCoilThread != NULL)
::WaitForSingleObject(m_pEndCoilThread->m_hThread, INFINITE);
to see if the thread is finished before calling again to save another report. And if not just wait. It seems in Win7 now, this call will not exit so that the program will just freeze. It does not freeze all the time, seems random. In WinXP, the same program seems not to have this problem. I am not sure what I am missing.
Any help or suggestion will be appreciated. Thanks!
Stan the man
|
|
|
|
|
You may ran into a race condition. A common way to check if a thread has been terminated (ensure that STILL_ACTIVE is not returned by your thread):
if (m_pEndCoilThread != NULL &&
m_pEndCoilThread->m_hThread != INVALID_HANDLE_VALUE)
{
DWORD dwExitCode = 0;
::GetExitCodeThread(m_pEndCoilThread->m_hThread, &dwExitCode);
if (dwExitCode == STILL_ACTIVE)
::WaitForSingleObject(m_pEndCoilThread->m_hThread, INFINITE);
m_pEndCoilThread = NULL;
}
|
|
|
|
|
Hi Jochen.
Thanks. I will try it out....
Stan
|
|
|
|
|
From the code example I take it that you create the Save2CWGCoilRep thread from the UI thread, i.e. from a menu or button handler function. My guess then is that you have a deadlock:
1. The background thread sends messages to the UI thread. Maybe it tries to display a message box, but it could be anything really.
2. The UI thread is suspended in WaitForSingleObject and can't process the message.
It's risky to suspend the UI thread as you don't know when or why messages are posted to it. I would instead protect the critical parts of Save2CWGCoilRep with a critical section and take away the WaitForSingleObject call in the UI thread.
|
|
|
|
|
Where does m_pEndCoilThread come from? DOnt you mean m_pEndThread?
==============================
Nothing to say.
|
|
|
|
|
class Base
{
char * ptr;
public:
Base(){}
Base(char * str)
{
ptr = new char[strlen(str)];
strcpy(ptr,str);
}
};
class Derived : public Base
{
char * ptr_s;
public:
Derived(char * str1,char * str2):Base(str2)
{
ptr_s = new char[strlen(str1)];
strcpy(ptr_s,str1);
}
Derived(const Derived & sec)//:Base(sec.ptr)
{
this->ptr_s = new char[strlen(sec.ptr_s)];
strcpy(this->ptr_s,sec.ptr_s);
}
};
int _tmain(int argc, _TCHAR* argv[])
{
Derived Obj1("sunil","singh");
Derived Obj2 = Obj1;
return 0;
}
Obj1 is a derived class object where base class char pointer is initialized with "singh" and
derived class char pointer is initilized with "sunil". I want to create Obj2 out of Obj1. Separate memory should be created for
Obj2 char pointer (base part and derived part as well) and that should be initialized with the strings contained in Obj1.
Here the problem is: Derived class part can be initialized with copy constructor. How to initialize the base class char poniter of Obj2 with the base class part of Obj1. char pointers in
both the classes are private.
I tried using initializer list but could not succeed.
Is there some proper way to do this?
Thanks for any help or suggestion in advance.
|
|
|
|
|
What's wrong in having a copy c'tor in base class, too. That would take care of this.
|
|
|
|
|
Does this do what you want?
I:
1) Added +1 to all of the strlen statements used for memory allocation (strlen doesn't account for the NULL terminator - which you need to allow for when storing the string, as opposed to simply displaying it)
2) Made the data members protected
3) Uncommented the call to the Base constructor in the 2nd constructor for Derived
Code:
#include <string.h>
#include <stdio.h>
class Base
{
public:
Base(){}
Base(char * str)
{
ptr = new char[strlen(str)+1];
strcpy(ptr,str);
}
protected:
char * ptr;
};
class Derived : public Base
{
public:
Derived(char * str1,char * str2):Base(str2)
{
ptr_s = new char[strlen(str1)+1];
strcpy(ptr_s,str1);
}
Derived(const Derived & sec):Base(sec.ptr)
{
printf("Derived(const Derived &sec)\n");
this->ptr_s = new char[strlen(sec.ptr_s)+1];
strcpy(this->ptr_s,sec.ptr_s);
}
void showName()
{
printf("%s %s\n", ptr_s, ptr);
}
protected:
char * ptr_s;
};
int main(int argc, char* argv[])
{
Derived Obj1("sunil","singh");
Derived Obj2 = Obj1;
Obj1.showName();
Obj2.showName();
return 0;
}
Result:
Derived(const Derived &sec)
sunil singh
sunil singh
Process returned 0 (0x0) execution time : 0.055 s
Press any key to continue.
|
|
|
|
|
Thanks for your help.
Here in my scenario char * ptr; is private in base class.
So the Derived copy constructor statement will fail
Derived(const Derived & sec):Base(sec.ptr)
We will not be allowed to access the base class data using "sec.ptr".
So if my base class data is private, what should I do to initialize the base class data.
Thanks,
Sunil
|
|
|
|
|
How about providing a copy constructor in the base class as others have suggested?
Something like so (the displayed output is identical to my previous post):
#include <string.h>
#include <stdio.h>
class Base
{
public:
Base(){}
Base(char * str)
{
ptr = new char[strlen(str)+1];
strcpy(ptr,str);
}
Base(const Base &src)
{
this->ptr = new char[strlen(src.ptr)+1];
strcpy(this->ptr,src.ptr);
}
virtual void showName()
{
printf("%s", ptr);
}
private:
char * ptr;
};
class Derived : public Base
{
public:
Derived(char * str1,char * str2):Base(str2)
{
ptr_s = new char[strlen(str1)+1];
strcpy(ptr_s,str1);
}
Derived(const Derived & sec):Base(sec)
{
printf("Derived(const Derived &sec)\n");
this->ptr_s = new char[strlen(sec.ptr_s)+1];
strcpy(this->ptr_s,sec.ptr_s);
}
void showName()
{
printf("%s", ptr_s);
}
void showFullName()
{
showName();
printf(" ");
Base::showName();
printf("\n");
}
private:
char * ptr_s;
};
int main(int argc, char* argv[])
{
Derived Obj1("sunil","singh");
Derived Obj2 = Obj1;
Obj1.showFullName();
Obj2.showFullName();
return 0;
}
|
|
|
|
|
Thanks
It cleared my confusion.
|
|
|
|
|
Dear All,
Does anybody know how to convert OpenCV image (iplimage) to Halcon image?
Thanks 4 ur help guys.
Lee
|
|
|
|
|
Did you try this[^]?
Unrequited desire is character building. OriginalGriff
I'm sitting here giving you a standing ovation - Len Goodman
|
|
|
|
|
HH.exe is the Microsoft Windows XP 's Primary Help Application.
If you Start MSDN directly, you will see a hh.exe process listed in taskmgr.exe
But, when you start help from VCpp or UltraEdit or InternetExplorer, hh.exe is NOT listed in the taskmgr.exe.
How can I create new a process from my program, and HIDE it from taskmgr.exe? I mean NOT only hh.exe, but any program other than hh.exe!
The C++ code clip would be great!
|
|
|
|
|
cyysoft wrote: How can I create new a process from my program, and HIDE it from taskmgr.exe?
That's not possible. And if hh.exe is not being listed in task manager if viewing help from VS, then it is NOT loading hh.exe at all. That's why it's not shown in the task manager.
Also, why do you need to hide a process?
"Real men drive manual transmission" - Rajesh.
|
|
|
|
|
Rajesh R Subramanian wrote: why do you need to hide a process?
To show off his 133t skillz.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
|
he might ask, how to hide task manager process
|
|
|
|
|
He'll get the same answer.
Bastard Programmer from Hell
|
|
|
|
|
Hello!
I want to compile MuPDF as a dynamic library (dll) instead of a static library.
With the static library, everthing works fine. However, the dll version is not working. When I call, e.g.
int f = _wopen(...);
read(f, ...);
in the main app, everything works fine.
However, if
read(f, ...);
is called inside the dll, I get an assertion.
It seems to me that the file f is not known within the dll - therefore, the call to read(...) fails.
Is there any way to share the files across dll boundaries?
Alex
|
|
|
|
|
LionAM wrote: Is there any way to share the files across dll boundaries?
if your app and the DLL both are set to use the CRT in a shared library (not statically linked to the CRT), and are built to use the same version of the CRT (same compiler, CRT version, etc), it should work.
|
|
|
|
|
LionAM wrote: I get an assertion.
Which one?
LionAM wrote: the file f is not known within the dll
Where is it defined in the DLL code?
Unrequited desire is character building. OriginalGriff
I'm sitting here giving you a standing ovation - Len Goodman
|
|
|
|
|
Thank you for the answer.
In the MuPDF sample, the file is opened with
int fd = _wopen(filename, _O_BINARY | _O_RDONLY, 0666);
The file descriptor fd is passed to a function defined within libmupdf. However, when this function tries to read the file (inside libmupdf):
read(fd, buf, bufsize)
the function fails with the assertion
---------------------------
Microsoft Visual C++ Debug Library
---------------------------
Debug Assertion Failed!
Program: p:\OLE_PDF\Debug\OLE_PDF_Test.exe
File: f:\dd\vctools\crt_bld\self_x86\crt\src\read.c
Line: 86
Expression: (_osfile(fh) & FOPEN)
I think the proble is that the libmupdf.dll "lives" in a different address space - you cannot simply assume that resources are valid inside the dll and the exe. Therefore,
I changed the MuPDF sample so that the file is opened inside the library. Now the file is read as expected.
However, there are still other (perhaps similar problems). For example, there are global variables inside libmupdf:
static fz_colorspace k_device_bgr = { -1, "DeviceRGB", 3, bgr_to_rgb, rgb_to_bgr };
fz_colorspace *fz_device_bgr = &k_device_bgr;
In the library header (which is included in the MuPDF sample), it is declared as
extern fz_colorspace *fz_device_bgr;
Inside the MuPDF sample, there is a local variable
fz_colorspace *colorspace = fz_device_bgr;
It is passed to the library. However, the function fails with an access violation when the dll tries to change a member of the colorspace struct. The problem is that fz_device_bgr has different values inside the dll and inside the sample (where it is undefinded?).
Alex
|
|
|
|
|
LionAM wrote: It is passed to the library. However, the function fails with an access violation when the dll tries to change a member of the colorspace struct. The problem is that fz_device_bgr has different values inside the dll and inside the sample (where it is undefinded?).
You can't access the main application's globals from a dll like that. They would have to be passed using a regular function call (perhaps using Get() and Set() calls).
|
|
|
|
|
Thank you for the answer.
After correcting this issue (by calling a dll function which returns the dll "version" of colorspace variable), the program does not crash any more.
However, it is still not working as expected...
I have to search further for similar issues, which becomes more difficult as I don't know where to look for the problem. I think I will debug both the statically and the dynamically linked versions simultaneously and look where they behave different...
Alex
|
|
|
|