|
Hi all,
well allthough I'm new to C++ I've done my share of development in more user friendly environments, mainly C#.
Anyhow I want to learn C++ as mainly personal interest as my professional life will be focused primarly in .NET development whenever it is needed.
To learn the basics of the language I'm starting with a real easy Math class that will initially implement a Matrix (no templates). Nothing fancy but an easy place to start to at least start learning Object Oriented programming in C++ like any other.
Well the thing is that I'm completely stumped with an error I'm getting. I've created a simple Win32 console app that links my FastMath.dll and uses the FMatrix class defined inside. Test app code is as simple as it can get, it only instantiates one FMatrix and then deletes it, and something that simple is giving me a runtime error. Here's the code in the tesp app:
#include "stdafx.h"
#include "FMatrix.h"
using namespace FastMath;
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
FMatrix *a=new FMatrix(5);
cout << "A is a [" << a->GetRowCount() << "x" << a->GetColumnCount() << "] matrix." << endl;
delete a;
return 0;
}
Ok the error I'm getting is when executing "delete a". The error message is:
Windows has triggered a breakpoint in Tester.exe.
This may be due to a corruption of the heap, and indicates a bug in Tester.exe or any of the DLLs it has loaded.
The output window may have more diagnostic information
If I try to continue I get the same error once and then succesive Assertion Failed error messages: "Expression: _CrtIsValidHeapPointer(pUserData)" and if I ignore that I get a HEAP CORRUPTION DETECTED error message "CRT detected that the application wrote to memory after end of heap buffer."
I really do not understand what I'm doing wrong in my FMatrix file. I'm including header and source files below:
FMatrix.h
#ifdef FASTMATH_EXPORTS
#define FASTMATH_API __declspec(dllexport)
#else
#define FASTMATH_API __declspec(dllimport)
#endif
#pragma once
#include "StdAfx.h"
namespace FastMath
{
class FASTMATH_API FMatrix
{
private:
int rw,cl;
double *val;
FMatrix(void);
FMatrix(unsigned int,bool);
FMatrix(unsigned int,unsigned int,bool);
void initMatrix(unsigned int,unsigned int,bool);
void destroy(void);
public:
FMatrix(const FMatrix&);
FMatrix(unsigned int);
FMatrix(unsigned int,unsigned int);
FMatrix(unsigned int,unsigned int,double**);
~FMatrix(void);
static FMatrix* CreateIdentity(unsigned int);
static FMatrix* CreateFull(unsigned int,unsigned int,double);
static FMatrix* CreateRandomIntMatrix(unsigned int,unsigned int,int,int);
static FMatrix* CreateRandomDblMatrix(unsigned int,unsigned int,double,double);
double GetItem(unsigned int,unsigned int) const;
void SetItem(unsigned int,unsigned int,double);
int GetRowCount() const;
int GetColumnCount() const;
static bool AreSameSize(const FMatrix&,const FMatrix&);
FMatrix& operator =(const FMatrix&);
FMatrix& operator -() const;
FMatrix& operator +(const FMatrix&) const;
FMatrix& operator -(const FMatrix&) const;
FMatrix& operator *(double) const;
FMatrix& operator *(const FMatrix&) const;
FMatrix& operator !() const;
bool operator ==(const FMatrix&) const;
bool operator !=(const FMatrix&) const;
};
}
FMatrix.cpp (only relevant code)
#include "StdAfx.h"
#include "FMatrix.h"
#include "time.h"
#include <cstdlib>
#include <iostream>
using namespace FastMath;
using namespace std;
void FMatrix::initMatrix(unsigned int r,unsigned int c, bool initValues)
{
rw=r;
cl=c;
val=new double[rw*cl];
#ifdef _DEBUG
cout <<"++ Matrix created." << endl;
#endif
if (initValues)
{
for (int i=0;i<rw;i++)
{
for (int j=0;j<cl;j++)
*(val+i*cl+j)=0;
}
}
}
FMatrix::FMatrix(unsigned int dim)
{
initMatrix(dim,dim,true);
}
FMatrix::~FMatrix()
{
destroy();
}
void FMatrix::destroy()
{
delete[] val;
val=NULL;
#ifdef _DEBUG
cout << "-- Matrix destroyed." << endl;
#endif
}
1. Only pointer in my class is private member double *val;
2. Only pointer used in constructor is *val that is set with a new double[whatever], thus no non initialized pointers are left hanging once constructor is through instantiating my class.
3. Destructor calls private method destroy() where I delete [] val, set it to NULL and log to the console that the destructor was called.
I'm really confused because I dont know what I'm doing wrong. The thing is if I use in the Test app the static constructors (CreateRandomIntMatrix for example) and I dont have to manually delete them everything works great and the destructor of my two matrixes is called all by itself. But when I explecitly instantiate with the new operator in my test app and thus have to delete them myself in that same app I get the errors described above.
The worst thing is that I'm not getting an error anywhere that i can see when I step through with the debugger. Everything executes perfectly if I put a breakpoint on the exit bracket of my destroy() method. Once I hit the breakpoint and I press continue and the only thing it should do is step out of the Destroy() method and return to the the Test app I get the error. I dont know what's going on at all.
Can this be due to something else and not a coding problem? Some corrupted VS 2005 file or something? I am getting a constant message evey now and then when I build or start debuging my app that tells me "This project is out of date. FastMath - Debug Win32" ??? anyone know what this is and if it can be linked to my problem?
Any help plz?
-- modified at 23:18 Saturday 13th January, 2007
|
|
|
|
|
gumi_r@msn.com wrote: "CRT detected that the application wrote to memory after end of heap buffer."
That means that you allocated a block of memory, but wrote past the end of the block.
|
|
|
|
|
Hi Micheal,
thanks for the input. I guessed that much myselft but the problem is that I'll be damned if I see where I wrote past the allocated memory. Only the constructor and destructor are running in my test app and I don't see anywhere in the running code where I could be writing past any alocated memory.
I really dont understand what is wrong.
|
|
|
|
|
Hi,
Have you tried to override the delete operator?;)
Regards,
Eli
|
|
|
|
|
1. I am creating an instance of class-W from within class-A function-B.
Can the instance of class-W be referenced outside of class-A function-B? I suspect it cannot and don't want to push a rope.
2. I can create an instance of class-W in the header of class-A and reference it all through class-A. For example, the following creates an instance of class Ladder which is available to all functions in class-A:
Ladder LC;
Then I can then do the following all through class-A
LC.SomeLadderFunction()
Suppose I create a pointer to Ladder like this:
Ladder *XY
How can I assign LC to pointer XY so I can say
XY->SomeLadderFunction();
Thanks
|
|
|
|
|
|
I have placed two buttons in a cstatic. I have added this static as a child control in a status bar. The buttons highlight state is not udpdated even if the mouse pointer is taken out of the button after lbuttondown. How does the same work fine if the buttons are placed in a dialog?
Thanks
|
|
|
|
|
These are push button types? What highlight state are you referring to?
Mark
|
|
|
|
|
hi
i want to know if we can open and close the USB port and if we can how ?? and how we can get the data from any device such a mouse connected with the USB ???
ahmad al-omar
|
|
|
|
|
You can open the port using CreateFile() function -eg. CreateFile("usb0001") -. Then get the file handle returned by the function and deal with it as you would read/write data from/to a file.
Regards
|
|
|
|
|
To my fellow programmers, I come forth again with another issue.
For review, I am reading from an MS Access database into a console app. In the database, I have data all being read by the console app as strings. However, there are two pieces of data I wish to convert from string to integer. Here is the code:
<br />
CoInitialize(NULL);<br />
try<br />
{<br />
_RecordsetPtr pGetInfo("ADODB.Recordset");<br />
_bstr_t strMyDB("DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=FinalDB.mdb");<br />
pGetInfo->Open("SELECT * FROM [Video];", strMyDB, adOpenKeyset, adLockOptimistic, adCmdText);<br />
pGetInfo->MoveFirst();<br />
while (!pGetInfo->EndOfFile)<br />
{<br />
sID = (char*) ( (_bstr_t) pGetInfo->GetFields()->GetItem("Vid_ID")->GetValue() );<br />
sTitle = (char*) ( (_bstr_t) pGetInfo->GetFields()->GetItem("Vid_Title")->GetValue() );<br />
sFormat = (char*) ( (_bstr_t) pGetInfo->GetFields()->GetItem("Vid_Format")->GetValue() );<br />
pGetInfo->MoveNext();<br />
iIndex++;<br />
}<br />
pGetInfo->Close();<br />
}
Now, thanks for the answer to VuNic and CoffeeAddict(##), I am supposed to itoa() the (char*) as so:
<br />
iVidTotal = itoa(sAmount);
iAvailable= itoa(sAvailable);
However, eh... ... when I compile under MS's IDE 2003, I get that the itoa() doesn't take only 1 argument. When I checked the MSDB, it requires three arguments, each I have no certain idea what they do. Just remember, I have the header <stdlib.h> in my header file, just to let you guys know. So, yeah.... I thank you guys in advanced.
Like a friend said, "TOO GOOD!" ありがとうございます!
|
|
|
|
|
I think you're going the wrong way - maybe you want atoi()
|
|
|
|
|
Wait a minute... are you serious?
Eh, well... Thanks, guys! I thank you and HAND for supplying me with widsom. I was wondering why itoa wasn't working...
As for CoffeeAddict(##) and VuNic, I guess it was a matter of a slip-up, right? A typo?
It's ok - I thank you guys for your time and wisdom. One day, I might lend a hand and lend some wisdom of my own... but that's a long way off. Thanks!!!
Like a friend said, "TOO GOOD!" ありがとうございます!
|
|
|
|
|
You need atoi instead of itoa.
|
|
|
|
|
Hi,
I have a class which will be accessed by two or more threads and I wondered if what I have done is OK or if there is a better way. I have added a CCriticalSection class member which is initialised in the constructor. Then, each class method uses EnterCriticalSection and LeaveCriticalSection.
The problem is that there are 100 instances of this class and I wondered if having so many critical sections was a burden on the operating system. I thought of having a single CCriticalSection static class member but (rightly or wrongly) I figured that I would be unnecessaryly locking access to all instances and could end up with the code being locked most of the time.
I would really appreciate youe views on this. I understand the basics of multithreading but its still a bit of a mystery.
TIA
Tony
|
|
|
|
|
It sounds like you have it right to me.
Using one critical section for all instances could definitely impact performance with only
one thread able to access any object at one time. It's still a viable solution but only if
that's really what you want or need.
I would imagine the only burden on the system is more memory/handles being used for each sync
object. 100 is relatively few. The class instances have to be thread protected so there's not
much choice anyway
I thought the OS was THERE for us to burden
Mark
|
|
|
|
|
Hi Mark,
Thanks very much for your reply. The code appears to work well but its good to have someone else give a quick sanity check on what I have done.
Thanks again
Tony
|
|
|
|
|
i am using unicode
when displaying strings from the string table on a message box
AfxMessageBox(IDS_MSG_STRING, ...);
where IDS_MSG_STRING is an entry in the sting table, the following occure:
1- in debug mode: every thing foes nice.
2- in release mode: the message box pops up as usual but displaying the following string "an unnamed file".
why is that and how to avoid it?
thankx in advance
k_dehairy
|
|
|
|
|
If my project work in "Multithread" and during process in every thread will generate message box for ask user what the way user want to do.
How I check in main program, the message box that occur.
Which thread generate it?
|
|
|
|
|
Use the void pointer (LPVOID) which you pass while creating the thread.
<code>
int threadCount;
AfxBeginThread(YouThread,(LPVOID)threadCount);
UNIT YourThread(LPVOID lp)
{
int threadCount = (int)lp;
yourFunction(threadCount);
}
void yourFunction(int threadID)
{
do
{
itoa(threadID,ch,10)
MessageBox(..ch)
}while()
}
</code>
Code-Frog:So if this is Pumpkinhead. Time for him to run and hide. It's an interesting thought really.
|
|
|
|
|
Or the simpler way is you can simple have a GlobalThreadID, that gets incremented everytime you create a thread, and you assign it the local variable that you will have in the thread function.
Code-Frog:So if this is Pumpkinhead. Time for him to run and hide. It's an interesting thought really.
|
|
|
|
|
Guys,
I ran into this issue during migration of project from .net2003 to vs2005.
Have you guys encountered something similar and fixed the same?
error LNK2019: unresolved external symbol "public: __thiscall std::exception::exception(char const * const &)" (??0exception@std@@QAE@ABQBD@Z) referenced in function "public: __thiscall std::bad_alloc::bad_alloc(char const *)" (??0bad_alloc@std@@QAE@PBD@Z)
error LNK2001: unresolved external symbol "public: __thiscall std::exception::exception(char const * const &)" (??0exception@std@@QAE@ABQBD@Z)
error LNK2001: unresolved external symbol "public: __thiscall std::exception::exception(char const * const &)" (??0exception@std@@QAE@ABQBD@Z)
SharedMemory.obj : error LNK2001:
Vipin
|
|
|
|
|
|
You find your answer
|
|
|
|
|
I originally using Visual Basic 6.0 and .NET.
But, I realized vb is had limited.
so, I'm turning to VC++/MFC.
I knew VB's The general trend is transition to .NET
but, I don't know in VC++.
Is VC++'s trend 6.0? or trend is .NET?
I wandering this problem.
I really appreciate, If i had your reply.
thanks~
Aim High, life is soaring
but, Aim Low, life is boring!
|
|
|
|