Help. C++ newbie has problems :p
-
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" #include 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":
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 informationIf 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&); //copy constructor FMatrix(unsigned int); FMatrix(unsigned int,unsigned int); FMatrix(unsigned int,unsigned int,double*
gumi_r@msn.com wrote:
well allthough I'm new to C++ I've done my share of development in more user friendly environments, mainly C#.
This is the C++/CLI forum. IF you want to learn C++, you need the visual C++ forum. C++/CLI is .NET C++.
FMatrix *a=new FMatrix(5); cout << "A is a [" << a->GetRowCount() << "x" << a->GetColumnCount() << "] matrix." << endl; delete a;
Why create it as a pointer at all ? Your destructor is obviously causing the error. Set a breakpoint and step through to see what it is. I can't see the initMatrix method, but for a starter, any constructor should set all pointers to NULL. A pointer that was not set by initMatrix and was not set to NULL is a very likely cause of this error.Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
-
gumi_r@msn.com wrote:
well allthough I'm new to C++ I've done my share of development in more user friendly environments, mainly C#.
This is the C++/CLI forum. IF you want to learn C++, you need the visual C++ forum. C++/CLI is .NET C++.
FMatrix *a=new FMatrix(5); cout << "A is a [" << a->GetRowCount() << "x" << a->GetColumnCount() << "] matrix." << endl; delete a;
Why create it as a pointer at all ? Your destructor is obviously causing the error. Set a breakpoint and step through to see what it is. I can't see the initMatrix method, but for a starter, any constructor should set all pointers to NULL. A pointer that was not set by initMatrix and was not set to NULL is a very likely cause of this error.Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
The only pointer in my class is double* val that is a double array that stores all my matrix items. Destructor only executes delete [] val and sets the pointer to NULL on exit. No other pointers are set during the constructor. The 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 (Destructor calls this method, I implemented this way so I can log every call to my destroy method even if called from inside the class like in the assign operator where I have to delete *val before reassigning.) 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. 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]; 3. Destructor calls private method destroy() where I delete [] val and log to the console that the destructor was called. You can see the initMatrix method in my first post, but just for clarification, I'll show the involved methods here:
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 }
-
The only pointer in my class is double* val that is a double array that stores all my matrix items. Destructor only executes delete [] val and sets the pointer to NULL on exit. No other pointers are set during the constructor. The 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 (Destructor calls this method, I implemented this way so I can log every call to my destroy method even if called from inside the class like in the assign operator where I have to delete *val before reassigning.) 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. 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]; 3. Destructor calls private method destroy() where I delete [] val and log to the console that the destructor was called. You can see the initMatrix method in my first post, but just for clarification, I'll show the involved methods here:
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 }
Are r and c both > 0 when this is called ?
Christian Graus - C++ MVP 'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert
-
Are r and c both > 0 when this is called ?
Christian Graus - C++ MVP 'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert
Yes, I'm calling FMatrix::FMatrix(5); I should add that every once in while when I build for debugging, I get a message warning me that: "The project is out of date. FastMath - Debug Win 32". Could the problems I'm having be something other that a coding problem? Something that goes wrong when building or something?
-
Yes, I'm calling FMatrix::FMatrix(5); I should add that every once in while when I build for debugging, I get a message warning me that: "The project is out of date. FastMath - Debug Win 32". Could the problems I'm having be something other that a coding problem? Something that goes wrong when building or something?
OK, yeah, if this is in a dll, it's possible you're not building the dll, or the one that your main program is seeing, is out of date. Step into the code. Watch what the constructor and destructor do, assuming hte other methdos called are not changing state.
Christian Graus - C++ MVP 'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert
-
OK, yeah, if this is in a dll, it's possible you're not building the dll, or the one that your main program is seeing, is out of date. Step into the code. Watch what the constructor and destructor do, assuming hte other methdos called are not changing state.
Christian Graus - C++ MVP 'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert
No :( I suspected it could have something to do with me not linking well the two projects so I even built it all in one same project...included header and source files of my FMatrix class in the Test app and removed any linking to the dlls...just compiled it as one console app. I still got the same error and I still couldn't catch it with the debugger step by step. I'm giving up, at least today. Thanks for all the help.
-
No :( I suspected it could have something to do with me not linking well the two projects so I even built it all in one same project...included header and source files of my FMatrix class in the Test app and removed any linking to the dlls...just compiled it as one console app. I still got the same error and I still couldn't catch it with the debugger step by step. I'm giving up, at least today. Thanks for all the help.
If you email me the project (christian dot graus at gmail dot com ), I'll look at it for you. But, next time ask in the right forum :-)
Christian Graus - C++ MVP 'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert
-
If you email me the project (christian dot graus at gmail dot com ), I'll look at it for you. But, next time ask in the right forum :-)
Christian Graus - C++ MVP 'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert
Thanks! I really apreciate it. About the wrong sorry, yeah :s sorry about that. Didnt see the Visual C++ formun :(
-
Thanks! I really apreciate it. About the wrong sorry, yeah :s sorry about that. Didnt see the Visual C++ formun :(
Ok Christian, thanks for all the help but I managed to track down the error. Must of had some other bug I fixed before when I tried to compile everything under the same project because otherwise it should have worked. The thing is that in the FMatrix.h header file included in the Test project I wasn't including the private definitions. I thought that the header acted more like an interface than anything else and thus private members were not needed. Once I included the private definitios in the FMatrix.h header file inside the Test app the problem disappeared. Thanks for your help!
-
Ok Christian, thanks for all the help but I managed to track down the error. Must of had some other bug I fixed before when I tried to compile everything under the same project because otherwise it should have worked. The thing is that in the FMatrix.h header file included in the Test project I wasn't including the private definitions. I thought that the header acted more like an interface than anything else and thus private members were not needed. Once I included the private definitios in the FMatrix.h header file inside the Test app the problem disappeared. Thanks for your help!
No worries, glad to help ( and glad that people are still learning C++ :-) )
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog