Non-aligned pointer being freed

Nevermind, I figured out why I was getting the error and how to fix it. Its' because I was trying to delete things that aren't at the beginning of contiguous memory assignments


I've been getting this very weird error that I can't figure out. It's coming about when I'm trying to deallocate memory. For background, here are the full headers for the classes in question:

1
2
3
4
5
6
7
8
9
10
class dish {

public:
	~dish(void);
	std::vector<table*>& getTables(void) {return tablePtrVec;};
	
private:
	std::vector<table*> tablePtrVec;
	
};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class table {
	
public:
	table(patron* fp, restaurant* rp, dish* dp);
	~table(void);
	std::vector<patron*>& getPatrons(void) {return patronPtrVec;};
	table* getTablePtr(void) {return tablePtr;};
	dish* getDishPtr(void) {return dishPtr;};
	void seatPatron(patron* np) {patronPtrVec.push_back(np);};
	restaurant* getRestPtr(void) {return restPtr;};
	
private:
	std::vector<patron*> patronPtrVec;	
	table* tablePtr; //points to tables of tables, for higher level hierarchies
	dish* dishPtr; //points at dish if table is top of hierarchy
	restaurant* restPtr; //points at restaurant table is in (each level has a new restaurant)	
};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class patron {
	
public:
	~patron(void); 
	int getData(void) {return data;};
	table* getTablePtr(void) {return tablePtr;};
	void reseat(table* nt);
	void initialize(std::string gn, int l, int el, int gl);
	
private:
	int data;
	table* tablePtr;
	std::string geneName;
	int lane;
	int geneLength;
};


As you can see, there is a lot of pointing cross-talk. Patrons point at the table they are sitting at, tables point at all the patrons sitting at the table, and at the dish that is being eaten at the table, and the dishes point at all the tables eating the dishes.

My issue is deallocating the memory, which I am trying to do by going "top down", i.e. deleting the dishes, which deletes the tables, which deletes the patrons. Below are the relevant destructors (the data class has nothing useful in it besides storing an array of the restaurants and a vector of the dishes, and the restaurant destructor is empty):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
data::~data(void) {
	delete[] restaurants;
	for (std::vector<dish*>::iterator it = allDishes.begin(); it != allDishes.end(); it++)
		delete *it;
}

dish::~dish(void) {
	std::cout << "I'm in the dish destructor" << std::endl;
	for (std::vector<table*>::iterator it = tablePtrVec.begin(); it != tablePtrVec.end(); it++)
		delete *it;
}

table::~table(void) {
	std::cout << "I'm in the table destructor" << std::endl;
	for (std::vector<patron*>::iterator it = patronPtrVec.begin(); it != patronPtrVec.end(); it++) {
		std::cout << "The table thinks this patron is at " << *it << std::endl;
		delete *it;
	}
}

patron::~patron(void) {
	std::cout << "I'm in the patron destructor" << std::endl;
	std::cout << "This patron is at location " << this << std::endl;
}


All the couts help me to see that something weird is going on when I run this code. I essentially do a lame initialization of every customer in a given restaurant to be sitting at the same table and every table to eat the same dish, which I am quite positive works, and I get this kind of console output:

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
I'm in the dish destructor
I'm in the table destructor
the table thinks this patron is at 0x1001d4
I'm in the patron destructor
This patron is at location 0x1001d4
Untitled(30485) malloc: *** error for object 0x1001d4: Non-aligned pointer being freed
*** set a breakpoint in malloc_error_break to debug
the table thinks this patron is at 0x1001d4
I'm in the patron destructor
This patron is at location 0x1001d4
Untitled(30485) malloc: *** error for object 0x1001d4: Non-aligned pointer being freed
*** set a breakpoint in malloc_error_break to debug
the table thinks this patron is at 0x1001d4
I'm in the patron destructor
This patron is at location 0x1001d4
Untitled(30485) malloc: *** error for object 0x1001d4: Non-aligned pointer being freed
*** set a breakpoint in malloc_error_break to debug
the table thinks this patron is at 0x1001e8
I'm in the patron destructor
This patron is at location 0x1001e8
Untitled(30485) malloc: *** error for object 0x1001e8: Non-aligned pointer being freed
*** set a breakpoint in malloc_error_break to debug
the table thinks this patron is at 0x1002b0
I'm in the patron destructor
This patron is at location 0x1002b0
the table thinks this patron is at 0x100378
I'm in the patron destructor
This patron is at location 0x100378


What is going on? The weirdest thing is that it seems to try to go back and do it again or something, as you can see it's trying to free the patron that's at 0x1001d4 a few times or something? Thanks in advance, this board has been super helpful to me already.
Last edited on
I found one issue, which is that I wasn't actually filling my vector of patron pointers properly. Now I don't have the repetition, but I still get the non-aligned pointer being freed error.
Topic archived. No new replies allowed.