memory error after returning a class

Hi,
I have two different classes each in a separate header file and a .cpp file.
In the main function I create an instance of each class and then call a test function in one of the classes which should return an instance of the other class. The information in the returned class is stored using a custom operator= function.
When the program runs (with no build errors) I get an error from the console, "assertion failed" ... "line 52" ... "_BLOCK_TYPE_IS_VALID(phead->nblockuse)"
I don't know which line "line 52 refers to".

A google search makes me think the error is from trying to access memory which has already been deleted but I'm not actually sure which bit of the program is doing this
When line 17, "delete [] data;", from the fArray.h file is removed the error goes away.

Obviously I don't want to leave this out because I'd end up with a lot of useless memory, does anyone know what to do?

I can also comment out all the non-constructor/destructor functions in the fArray.h file and get the same result.

Thanks, Henry.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
//.cpp////////////////////////////
#define _USE_MATH_DEFINES

#include <iostream>
//#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include <cstdlib> 
//#include<ctime>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;

#include "Vec2.h"
#include "Circle.h"
#include "fArray.h"

typedef unsigned short int usi;
typedef signed short int ssi;
typedef unsigned long int uli;
typedef signed long int sli;

int main(){
	Circle<int,int> test(2,1,1);
	fArray<int> test3;
	test3 = test.testFnc();
	//Circle<int,int> test2 = test3.testFnc();
	//circum = test.genCircum<long double>(8);
}
/////////////////////////////////////////////////////


relevent function is last
//circle.h///////////////////////////////////////////
#pragma once

#include "fArray.h"
#include "Vec2.h"
typedef unsigned short int usi;
typedef signed short int ssi;
typedef unsigned long int uli;
typedef signed long int sli;


template <class cType,class rType>
class Circle
{
	Vec2<cType> center;
	rType rad;
public:
	Circle(void){
		center.setV(0,0);
		rad = 0;
	}
	Circle(Vec2<cType> ncenter,rType nrad){
		center = ncenter;
		rad = nrad;
	}
	Circle(cType ncx,cType ncy,rType nrad){
		center.setV(ncx,ncy);
		rad = nrad;
	}
	void operator+= (Vec2<cType> pc){
		center += pc;
	}
	void operator+= (rType pr){
		rad += pr;
	}
	void operator-= (Vec2<cType> pc){
		center -= pc;
	}
	void operator-= (rType pr){
		rad -= pr;
	}
	void operator= (Vec2<cType> nc){
		center = nc;
	}
	void operator= (Circle<cType,rType> nc){
		center = nc.getCenter();
		rad = nc.getRad();
	}
	Vec2<cType> getCenter(){
		return center;
	}
	rType getRad(){
		return rad;
	}
	template <class nType>
	void convertC(){
		center.convert<nType>();
	}
	template <class nType>
	void convertR(){
		rad = (nType) rad;
	}
	template <class nType,class nType2>
	void convertCR(){
		convertC<nType1>();
		convertR<nType2>();
	}
	void coutC(){
		cout<<"Circle center: x = "<<center.getX()<<"\ty = "<<center.getY()<<"\tradius "<<rad<<endl;
	}
	template <class vType>
	fArray<Vec2<vType>> genCircum(usi split){
		fArray<Vec2<vType>> tempA;
		for(usi i = 0;i < split;i++){
			Vec2<vType> temp;
			//Vec2<cType> add;
			temp.setD(rad,360*i/split);
			//add = center;
			//add.convert<vType>();
			//temp += add;
			tempA.addEnd(temp);
		}
		return tempA;
	}
	fArray<int> testFnc(){
		fArray<int> temp;
		return temp;
	}
};
/////////////////////////////////////////////////////////////

//fArray.h////////////////////////////////////////////////
#pragma once

typedef signed long int sli;

template <class mType>
class fArray
{
	sli length;
	mType *tempData;
	mType *data;
public:
	fArray(void){
		length = 0;
		data = new mType[length];
	}
	~fArray(void){
		delete [] data;
	}
	mType getValue(sli i){
		return data[i];
	}
	void add(mType value,sli index){
		length++;
		tempData = new mType[length];
		for(sli i=0;i<index;i++){
			tempData[i] = data[i];
		}
		for(sli i=index;i<length-1;i++){
			tempData[i+1] = data[i];
		}
		tempData[index] = value;
		delete [] data;
		data = new mType[length];
		for(sli i = 0;i < length;i++){
			data[i] = tempData[i];
		}
		delete [] tempData;
	}
	void addEnd(mType value){
		add(value,length);
	}
	sli getLength(){
		return length;
	}
	void operator= (fArray<mType> na){
		delete [] data;
		length = na.getLength();
		data = new mType[length];
		for(sli i =0;i<length-10;i++){
			data[i] = na.getValue(i);
		}
	}
};
///////////////////////////////////////////////////////////


closed account (3hM2Nwbp)
I don't see a copy constructor in your fArray class. That very may well be your issue, since your class isn't POD.

You'll notice that often assignment operators and copy constructors go hand in hand. You'll also want to note that these operators don't meet the strong guarantee.

1
2
3
4
5
6
7
8
fArray(const fArray& rhs)
{
  delete[] data;
  length = rhs.getLength();
  data = new mType[length]; // <- If this throws, *this is now in a bad state.
  for(int i = 0; i < length - 10; i++) // Why length - 10?
    data[i] = rhs.getValue(i);
}
Last edited on
Thankyou very much, I didn't know about copy constructors.

the following works

1
2
3
4
5
6
7
        fArray(const fArray& rhs){
		length = rhs.length;
		data = new mType[length];
		for(sli i =0;i<length;i++){
			data[i] = rhs.data[i];
		}
	}


I could only use the private variables/functions of rhs, I don''t know why, but it doesn't matter much anyway.
Topic archived. No new replies allowed.