|
The code you have given is in C platform but is acceptable in C++. No more stories dear...
Open Visual C++ from your machine.
Create new source file and save it using appropriate name.
Type in the following code:
#include<iostream>
using namespace std;
void main()
{cout<<"Hello\n";
}
amets is a young talented and enthusiastic upcoming software developer in one of the kenyan public universities.Computing problems are no more with stema. Try him and you will never regret.
|
|
|
|
|
I'm trying to get my code to create a thread pool class that fires off 5 threads and prints the instantiation sequence of the threads right before terminating by raising an event from the spawned thread itself, but I'm getting the following linker errors when compiling with VC8:
1>EventHandler.obj : error LNK2019: unresolved external symbol "private: static class CSource * CThreadPool::source" (?source@CThreadPool@@0PAVCSource@@A) referenced in function "private: static unsigned int __stdcall CThreadPool::DoWork(void *)" (?DoWork@CThreadPool@@CGIPAX@Z)
1>EventHandler.obj : error LNK2019: unresolved external symbol "private: static class CReceiver * CThreadPool::receiver" (?receiver@CThreadPool@@0PAVCReceiver@@A) referenced in function "public: __thiscall CThreadPool::CThreadPool(class CSource *,class CReceiver *)" (??0CThreadPool@@QAE@PAVCSource@@PAVCReceiver@@@Z)
The below is my source code:
#include <stdio.h>
#include <process.h>
#include <windows.h>
[event_source(native)]
class CSource
{
public:
__event void MyEvent(int nValue);
};
[event_receiver(native)]
class CReceiver
{
public:
void MyHandler(int nValue)
{
printf("Thread %d terminating...\n", nValue);
}
void hookEvent(CSource* pSource)
{
__hook(&CSource::MyEvent, pSource, &CReceiver::MyHandler);
}
void unhookEvent(CSource* pSource)
{
__unhook(&CSource::MyEvent, pSource, &CReceiver::MyHandler);
}
};
class CThreadPool
{
public:
CThreadPool(CSource* source, CReceiver* receiver)
{
CThreadPool::source = source;
CThreadPool::receiver = receiver;
receiver->hookEvent(source);
}
~CThreadPool()
{
receiver->unhookEvent(source);
}
void Initialize()
{
SpawnThreads();
}
private:
static CSource* source;
static CReceiver* receiver;
void SpawnThreads()
{
HANDLE hThread[5];
unsigned threadID[5];
for (int i = 0; i < 5; i++)
{
hThread[i] = (HANDLE)_beginthreadex(0, 0, &CThreadPool::DoWork, (void*)i, 0, &threadID[i]);
}
};
static unsigned int __stdcall DoWork(void* pArgs)
{
unsigned int x = 0;
for (int i = 0; i < 1000000; i++)
{
x++;
}
__raise CThreadPool::source->MyEvent(*(int*)pArgs);
return x;
}
};
int main() {
CSource source;
CReceiver receiver;
CThreadPool* threadPool = new CThreadPool(&source, &receiver);
threadPool->Initialize();
}
|
|
|
|
|
You have to initialize static members outside of the class declaration. So in a .cpp file you need something like:
CSource* CThreadPool::source = null;
or whatever it should be and of course the other static member as well.
led mike
|
|
|
|
|
It seems like the problem was that I didn't separate my .cpp and my .h when I should have, making things more confusing.
Thanks.
|
|
|
|
|
Hello,
Here is the function:
void CAssignment04View::OnPrint(CDC* pDC, CPrintInfo* pInfo)<br />
{<br />
int iRowCount = 1;
CString cNumeric;
CPen oPen;
CBrush oBrush;
CFont oTextFont;
CFont oHeadFont;
CFont oColFont;
LOGFONT lfFont;
CSize oFontSize;
COLORREF clrRef;
int iRowPos = 120;
int iTextHeight = 0;
CRect oDrawRect;
int iRecNumPos;
COleCurrency Salary;<br />
<br />
int iEmployeeNumPos; <br />
int iFirstNamePos; <br />
int iLastNamePos;<br />
int iAgePos;<br />
int iDepartmentPos;<br />
int iSalaryPos;<br />
<br />
BOOL EndOfRowset = false;
HRESULT hr;
<br />
<br />
oDrawRect = pInfo->m_rectDraw;<br />
clrRef = 0x00000000;<br />
<br />
oPen.CreatePen(PS_SOLID, 2, clrRef);<br />
pDC->SelectObject(&oPen);<br />
clrRef = 0x00C0C0C0;<br />
oBrush.CreateSolidBrush(clrRef);<br />
pDC->SelectObject(&oBrush);<br />
oHeadFont.CreatePointFont(240, "Arial", pDC);<br />
pDC->SelectObject(&oHeadFont);<br />
oFontSize = pDC->GetOutputTextExtent("HR DataBase");<br />
pDC->Ellipse(500,<br />
iRowPos - (oFontSize.cy / 2) - 10,<br />
oDrawRect.Width() - 500,<br />
iRowPos + (oFontSize.cy / 2) + 10);<br />
pDC->SetBkMode(TRANSPARENT);<br />
pDC->TextOut((oDrawRect.Width() - oFontSize.cx) / 2,<br />
iRowPos - (oFontSize.cy / 2) - 10,<br />
"HR DataBase");<br />
pDC->SetBkMode(OPAQUE);<br />
oHeadFont.GetLogFont(&lfFont);<br />
iRowPos = abs(lfFont.lfHeight) + 175;<br />
oTextFont.CreatePointFont(120, "Arial", pDC);<br />
oTextFont.GetLogFont(&lfFont);<br />
iTextHeight = abs(lfFont.lfHeight) + 10;<br />
lfFont.lfWeight = 700;
oColFont.CreateFontIndirect(&lfFont);<br />
pDC->SelectObject(&oColFont);<br />
<br />
iRecNumPos = int(oDrawRect.Width() / 17);<br />
iFirstNamePos = iRecNumPos + 50 + pDC->GetOutputTextExtent("##").cx;<br />
iLastNamePos = iFirstNamePos + 50 + pDC->GetOutputTextExtent("###########").cx;<br />
iAgePos = iLastNamePos + 100 + pDC->GetOutputTextExtent("###########").cx;<br />
iDepartmentPos = iAgePos + 150 + pDC->GetOutputTextExtent("##").cx;<br />
iSalaryPos = iDepartmentPos + 150 + pDC->GetOutputTextExtent("##########################").cx;<br />
<br />
<br />
pDC->TextOut(iRecNumPos, iRowPos, "#");<br />
pDC->TextOut(iFirstNamePos, iRowPos, "First Name");<br />
pDC->TextOut(iLastNamePos, iRowPos, "Last Name");<br />
pDC->TextOut(iAgePos, iRowPos, "Age");<br />
pDC->TextOut(iDepartmentPos, iRowPos, "Department");<br />
pDC->TextOut(iSalaryPos, iRowPos, "Salary");<br />
<br />
iRowPos += iTextHeight;<br />
pDC->MoveTo(iRecNumPos, iRowPos);<br />
pDC->LineTo(oDrawRect.Width() - iRecNumPos, iRowPos);<br />
iRowPos += 30;<br />
pDC->SelectObject(&oTextFont);<br />
iTextHeight = 20 + pDC->GetOutputTextExtent("Xy").cy;<br />
m_pSet->MoveFirst();<br />
while (!EndOfRowset)<br />
<br />
{<br />
itoa(iRowCount, cNumeric.GetBuffer(10), 10);<br />
cNumeric.ReleaseBuffer(-1);<br />
pDC->TextOut(iRecNumPos, iRowPos, cNumeric);<br />
pDC->TextOut(iFirstNamePos, iRowPos, CString(m_pSet->m_Employee_Name_First));<br />
pDC->TextOut(iLastNamePos, iRowPos, CString(m_pSet->m_Employee_Name_Last));<br />
<br />
itoa(m_pSet->m_Employee_Age, cNumeric.GetBuffer(10), 10);<br />
cNumeric.ReleaseBuffer(-1);<br />
pDC->TextOut(iAgePos, iRowPos, cNumeric);<br />
<br />
pDC->TextOut(iDepartmentPos, iRowPos, CString(m_pSet->m_Department));<br />
<br />
<br />
iRowPos += iTextHeight;<br />
iRowCount ++;<br />
<br />
m_pSet->MoveNext();<br />
if (m_pSet->IsEOF())<br />
{<br />
<br />
EndOfRowset = true;<br />
break;<br />
}<br />
<br />
<br />
}<br />
<br />
CRecordView::OnPrint(pDC, pInfo);<br />
}
Now the problem:
When you start the application you can navigate the records fine. You can then click print preview and it displays what you might expect. Now if you close the print preview window the recordset has navigated to the end. When you try to click either the << or < button it displays the following error message:
operation failed, no current record
Any suggestions what might be wrong. If you don't click the print preview button then you can navigate the records without problem.
Many Thanks.
|
|
|
|
|
For starters, here's the relevant code...
void CAssignment04View::OnPrint(CDC* pDC, CPrintInfo* pInfo)
{
BOOL EndOfRowset = false; // End of the rowset?
while (!EndOfRowset) {
// See if this is the last row.
m_pSet->MoveNext();
if (m_pSet->IsEOF()) {
EndOfRowset = true;
break;
}
}
CRecordView::OnPrint(pDC, pInfo);
}
/*
From MSDN:
If the last record is the current record when you call MoveNext, IsEOF will subsequently return nonzero. If IsEOF returns nonzero and you call MoveNext, an error occurs.
If IsEOF returns nonzero, the current record is undefined, and any action that requires a current record will result in an error.
*/
|
|
|
|
|
Thanks the advice. I don't suppose the MSDN page provided a solution to this ?
I got the code working when using OLE as my connection method using this code:
iRowPos += iTextHeight;<br />
iRowCount ++;<br />
hr = m_pSet->MoveNext();<br />
if (hr == DB_S_ENDOFROWSET)<br />
{<br />
EndOfRowset = true;<br />
break;<br />
}
However when this code is run using ODBC I get this error which I cannot fix:
error C2440: '=' : cannot convert from 'void' to 'HRESULT'
|
|
|
|
|
Lewis01 wrote: I don't suppose the MSDN page provided a solution to this ?
Did you try resetting the position to the first record before the break or something like that to set the position to a valid record before the function exits?
CRecordset::SetAbsolutePosition
Lewis01 wrote: I got the code working when using OLE as my connection method using this code:
Honestly, that code snippet makes no sense to me. What exactly is m_pSet in the context of the code snippet you just provided. I was assuming you were using a CRecordset but HRESULT is usually used with COM so I'm a little lost.
|
|
|
|
|
Lewis01 wrote: However when this code is run using ODBC I get this error which I cannot fix:
It's a compiler error, not a runtime error.
Lewis01 wrote: error C2440: '=' : cannot convert from 'void' to 'HRESULT'
Because the MoveNext() method does not return anything.
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
Hello,
I have previousy built a ODBC database application within VS2003 and was able to use the following code in order to sort the records:
m_pSet->m_<br />
m_pSet->Close();<br />
m_pSet->m_strSort = "[Col1]";<br />
m_pSet->Open();<br />
UpdateData(FALSE);
However I know have to use OLEDB solution and the method m_strSort is no longer available. Is it a matter of just a header file to get this functionality or can someone talk me through a new way of sorting records ?
Many Thanks.
|
|
|
|
|
Could you, please help me! I'm developing with VC++ 6 (WinXP SP2) and now I'm trying to find memory leaks. Internal memory leak finder doesn't provide me with sufficient information about codelines etc. that is why I tried a lot of tools. No one helps me. Here you can see an example with VLD (http://www.codeproject.com/tools/visualleakdetector.asp) tool used. Here is the output in Debug window:
<br />
........<br />
Visual Leak Detector Version 1.0 installed (multithreaded static).<br />
Loaded 'G:\WINDOWS\system32\uxtheme.dll', no matching symbolic information found.<br />
Loaded 'G:\WINDOWS\system32\riched32.dll', no matching symbolic information found.<br />
........<br />
The thread 0x8D0 has exited with code 0 (0x0).<br />
The thread 0x17C has exited with code 0 (0x0).<br />
The thread 0xD2C has exited with code 0 (0x0).<br />
The thread 0x824 has exited with code 0 (0x0).<br />
The thread 0xCFC has exited with code 0 (0x0).<br />
The thread 0x8DC has exited with code 0 (0x0).<br />
The thread 0x5F8 has exited with code 561 (0x231).<br />
The thread 0x528 has exited with code 0 (0x0).<br />
The thread 0x294 has exited with code 2 (0x2).<br />
No memory leaks detected.<br />
Visual Leak Detector is now exiting.<br />
The thread 0x470 has exited with code 0 (0x0).<br />
No memory leaks detected.<br />
Visual Leak Detector is now exiting.<br />
Detected memory leaks!<br />
Dumping objects -><br />
{60} normal block at 0x02F71790, 12 bytes long.<br />
Data: < > 90 17 F7 02 90 17 F7 02 CD CD CD CD <br />
{59} normal block at 0x02F717D0, 44 bytes long.<br />
Data: <|h a a > 7C 68 F2 02 80 61 F2 02 88 61 F2 02 10 1B F7 02 <br />
Object dump complete.<br />
The thread 0x928 has exited with code 0 (0x0).<br />
The program 'E:\Devs\Camel\Debug\Camel.exe' has exited with code 0 (0x0).<br />
As you can see, VLD is saying that there are no memory leaks (1) then internal leaks finder is saying the same (2), but then it finds two leaks. How it can be and what is the problem? This problem is regardless tool used. I just placed VLD as an example. Tools like Insure++ find nothing at all of just lines with calls to rand() or time() , which have no attitude to the problem. What can it be?
|
|
|
|
|
dum wrote: What can it be?
Disclaimer: I have no experience with other leak detector tools except what VC++ alerts me to.
In my experience, typical leaks occur when I use BSTR without a wrapper, failure to release a device context(ReleaseDC) after using GetDC(), and the obvious new/delete mismatched pairs.
You should search for the obvious sources in your code first. Any usage of a raw BSTR should be considered suspect (Not necessarily bad but at least scrutinize who "owns" the BSTR) as well as GetDC() and all memory allocation.
The execution paths should be scrutinized as posted sample code usually makes many assumptions about the success of operations to keep the snippet short and can mislead proper robust deallocation schemes. For instance, allocating and deallocating within the same scope can be dangerous if there are potential execution paths that can skip over the deallocation. Try/Catch is one of the likely places to forget alternate execution paths when allocation/deallocation is involved.
I Hope that helps.
|
|
|
|
|
The fact is that I'm developing this code for... 5 years. There have been several official releases etc. All the time I could find leaks manully or using some tools. There are no BSTR's at all and no new resources occupations. I do not know, what can it be, because I don't undestand, how memory leaks can be detected after I was reported that "No memory leaks detected"? On what stage of application's execution it caould be in this case? May be one can recomend me some outstanding tool for leaks looking up (I have tryed 5 today and no one have helped)?
|
|
|
|
|
If it helps any, I took a mulithreaded app and placed some intentional leaks in one of three of the threads. Everything appears fine until the app exits and the main thread is being shut down. That is where I get my notification of a leak.
Again, I'm not familiar with 3rd party leak detection tools and how they report. I usually incrementally scrutinize any changes so I know what I just changed is likely causes any reported leaks.
What was the last thing modified in the code?
|
|
|
|
|
It seems to me that I have missed the exact biuld, when it appears exactly. So, now I need some tool or information to find out, how it could be so. As you can imagine, in my oppinion there are no fresh reasons for leaks
So, does someone can help?
|
|
|
|
|
Lets reformulate the question a bit: I know the address, that leaks. I need a tool, which can tell me (after the execution of application), which line allocates at this address. Who knows such thing?
|
|
|
|
|
I haven't used VLD, but i expect that i creates a global instance of an class that checks for leaks in it's destructor.
I would also expect that there hasn't been any effort to control at what point the global object is created.
I would guess that there is a global object that is being created before VLD is initialized.
When when VLD exits all memory allocated since it was initialized has been freed, it knows nothing about memory allocated before it was initialized (so it says no leaks).
Your internal is likely the same.
So, depending on what you are calling that actually generates the leak message, you may have a leak in a global that is initialized early, or you may not have a leak at all just mis-configured leak detection code.
...cmk
Save the whales - collect the whole set
|
|
|
|
|
Thanx, but I do not think so.
1) Even without VLD I'm gettint "No leaks" and then a report about leaks from internal finder
2) I do not regulate the moment, whan VLD is created. It is kind of "p'n'p" solution: I just include it, no changes to code.
Does anyone have more ideas?
|
|
|
|
|
I don't know about third-party leak detectors.
I do use the CRT heap debugging features in my debug builds.
This goes on top (under the (#include lines) of all my source files:
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
For leaks that still don't give file/line number info...
In the debug output for the leaks, if the number in the curly braces is the same every time
you can add a breakpoint to break when those allocations occur.
See How to: Set Breakpoints on a Memory Allocation Number[^]
Mark
"Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder
|
|
|
|
|
Thank you for the information, it is very interesting, but the problem again. All the time I'm getting problem on allocations 59 and 60.
{60} normal block at 0x02721790, 12 bytes long.<br />
Data: < r r > 90 17 72 02 90 17 72 02 CD CD CD CD <br />
{59} normal block at 0x027217D0, 44 bytes long.<br />
Data: <|hm am am r > 7C 68 6D 02 80 61 6D 02 88 61 6D 02 10 1B 72 02 <br />
Object dump complete.<br />
but _CrtSetBreakAlloc(60); results in no break. Why can it be? I'm using VC++ 6, but there are no reasons to assume that it doesn't support this function.
|
|
|
|
|
I want to download the latest platform SDK for visual c++ based development. If I use google to locate it, I am being taken to a page at Microsoft which says Windows 2003 server sdk. If I search MS for SDK, there are plenty of results and I am confused. Can someone give me a link where I could download the latest platform SDK? Thanks.
Sorry if it is posted twice. There was error, when I attempted previously. I will delete one, if it is posted twice.
|
|
|
|
|
Windows Server 2003 R2 sdk
that is to the best of my knowneldge is the latest sdk for vc++ developers
p.s its name is a bit miss leading but thats the one
|
|
|
|
|
If you're using VS05, the latest & greatest SDK is now called the Windows SDK[^].
|
|
|
|
|
looks like i need to upgrade my SDK
thanks
|
|
|
|
|
Ive looked around on getting inputs from the LPT port on windows xp, apparantly I need a dll called inpout32.dll. I have that, however I wonder how I should "include" the file? So that i can use the code described on http://www.ctv.es/pckits/tutorial.html#ppro and its functions
Ive looked around alot on codeproject, and google around 50 times, but not found how to get it to work...
any ideas?
Im open to other methods too, but nothing too advanced.
PS, im in win32console with mfc on winxp, i will only be using lpt inputs, two of them.
thanks!
//Johannes
|
|
|
|
|