Click here to Skip to main content
15,892,737 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: How to put a dll to remote XP and run it? Pin
#realJSOP13-Jan-07 23:44
mve#realJSOP13-Jan-07 23:44 
GeneralRe: How to put a dll to remote XP and run it? Pin
liur1714-Jan-07 0:35
liur1714-Jan-07 0:35 
GeneralRe: How to put a dll to remote XP and run it? Pin
mark novak14-Jan-07 9:16
mark novak14-Jan-07 9:16 
QuestionProblem passing a Reference through multiple functions Pin
jblau13-Jan-07 19:17
jblau13-Jan-07 19:17 
AnswerRe: Problem passing a Reference through multiple functions Pin
#realJSOP13-Jan-07 23:42
mve#realJSOP13-Jan-07 23:42 
QuestionHow to connect toan ORACLE DB Using VC++6? Pin
Patriot8113-Jan-07 18:57
Patriot8113-Jan-07 18:57 
AnswerRe: How to connect toan ORACLE DB Using VC++6? [modified] Pin
liur1713-Jan-07 20:03
liur1713-Jan-07 20:03 
Questionwierd delete [ ] myClass exception. Need Help PLZ! [modified] Pin
gumi_r@msn.com13-Jan-07 17:12
gumi_r@msn.com13-Jan-07 17:12 
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&); //copy constructor
		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); //for debug purposes
		static FMatrix* CreateRandomDblMatrix(unsigned int,unsigned int,double,double); //for debug purposes
		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&);

		//operators
		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;

//-------------------------------------------------------------------------------------------------------------
//CONSTRUCTORS AND DESTRUCTOR----------------------------------------------------------------------------------
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
AnswerRe: wierd delete [ ] myClass exception. Need Help PLZ! Pin
Michael Dunn13-Jan-07 18:27
sitebuilderMichael Dunn13-Jan-07 18:27 
GeneralRe: wierd delete [ ] myClass exception. Need Help PLZ! Pin
gumi_r@msn.com14-Jan-07 2:10
gumi_r@msn.com14-Jan-07 2:10 
AnswerRe: wierd delete [ ] myClass exception. Need Help PLZ! Pin
eli1502197914-Jan-07 3:48
eli1502197914-Jan-07 3:48 
QuestionPointers to classes Pin
Oliver12313-Jan-07 16:52
Oliver12313-Jan-07 16:52 
AnswerRe: Pointers to classes Pin
bob1697213-Jan-07 17:00
bob1697213-Jan-07 17:00 
Questionchange the highlight for a button Pin
PrabhuDev13-Jan-07 9:56
PrabhuDev13-Jan-07 9:56 
AnswerRe: change the highlight for a button Pin
Mark Salsbery13-Jan-07 10:20
Mark Salsbery13-Jan-07 10:20 
QuestionUSB port controlling in vc++ Pin
ahmad al-omar13-Jan-07 9:11
ahmad al-omar13-Jan-07 9:11 
AnswerRe: USB port controlling in vc++ Pin
Nader Elshehabi13-Jan-07 11:16
Nader Elshehabi13-Jan-07 11:16 
Questionitoa() arguments problem Pin
CAgent00713-Jan-07 7:50
CAgent00713-Jan-07 7:50 
AnswerRe: itoa() arguments problem Pin
Mark Salsbery13-Jan-07 9:11
Mark Salsbery13-Jan-07 9:11 
GeneralDoh!! Pin
CAgent00713-Jan-07 13:17
CAgent00713-Jan-07 13:17 
AnswerRe: itoa() arguments problem Pin
Dennis Gourjii13-Jan-07 9:12
Dennis Gourjii13-Jan-07 9:12 
QuestionMultithreaded appication design question Pin
softwaremonkey13-Jan-07 5:25
softwaremonkey13-Jan-07 5:25 
AnswerRe: Multithreaded appication design question Pin
Mark Salsbery13-Jan-07 6:43
Mark Salsbery13-Jan-07 6:43 
GeneralRe: Multithreaded appication design question Pin
softwaremonkey13-Jan-07 7:32
softwaremonkey13-Jan-07 7:32 
QuestionString table and "an unnamed file" Pin
kdehairy13-Jan-07 5:16
kdehairy13-Jan-07 5:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.