*** glibc detected *** error

I'm fairly new to C++ and i've just recently gotten an error I'm not very familiar with. I'm working on a project that uses fltk and my professor created a kind of simplified version of fltk for us to work with.

I've managed to create an object of a class that extends a window, and then within that class call another class multiple times that will create multiple buttons and attach them to the original window.

The problem comes in when I try to implement a callback function that will move the button that is clicked on the original window.

Here is where I've found/think that the problem is at.




1
2
3
4
5
 void Game_window::make_tile(){
	int xpoint = counter*100;
	new tile_creator(this, xpoint, tile_value);
	counter++;
}


and

1
2
3
4
5
6
7
8
9
10
11
12
13
void tile_creator::cb_click(Address, Address pw)
{  
    reference_to<tile_creator>(pw).move();    
}


void tile_creator::move()
{
	game->detach(tile);
	int xbound = game->clicks*100;
	Rectangle temp_rect(Point{xbound,600},90,90);
	game->attach(temp_rect);
}


and the header file for tile_creator is

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
#ifndef TILE_GUARD
#define	TILE_GUARD

#include "std_lib_facilities_4.h"
#include "GUI.h"
#include "Graph.h"
#include "Simple_window.h"

class Game_window;

class tile_creator{
	
	public:
	
	tile_creator(Game_window* a, int counter, string label);
	
	Button tile;
	string label;
	
	private:
		Game_window* game;
		static void cb_click(Address, Address pw);
		void move();
};

#endif 


and the header for Game_window is this:

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
#ifndef GAME_GUARD
#define	GAME_GUARD

#include "std_lib_facilities_4.h"
#include "GUI.h"
#include "Graph.h"
#include "Simple_window.h"

class tile_creator;

struct Game_window : Graph_lib::Window{
	
	Game_window(Point xy ,int w, int h, const string& title);
	int clicks;
	
	private:
		
		string tile_value;
		Button enter_diff;
		In_box diff;
		int difficulty;	
		
		int randint();
		int tile_val;
		
		void translate_value();
		void make_tile();
		
		void next();
		static void cb_next(Address, Address pw);
		
		int counter;
		
};

#endif 


The window is of type Game_window, and then the button I am trying to attach/have the callback function is a member of type Button in tile_creator

and the error is:

*** glibc detected *** ./a.out: double free or corruption (out): 0x00007ffd0103


with a long backtrace/memory map that I could provide if needed, but I feel that it'd just clutter the post.

I'm just trying to get a better idea of what exactly is going on with the code, and if I could be pointed in the right direction on where to continue with it.

Last edited on
The error is trying to tell you that a routine in the GNU C runtime library (AKA glibc) detected a double-free (i.e., you deleted memory more than once) or heap-memory corruption (i.e., you smashed some heap memory) at or around the address it gave you.

The first thing you should check is object ownership. Who owns who, and when? Your use of raw pointers makes this opaque; you should keep careful track of that. In modern C++ you can (and should) avoid most problems like this by using the appropriate smart pointer to explicitly enforce ownership semantics for you.

The line new tile_creator(this, xpoint, tile_value); appears to leak memory. Even if it does not due to code in the constructor, the statement is highly dubious.

You didn't specify when your code crashes, but I would suggest you check to make sure your callback's dynamic environment remains valid until it is deregistered.

There is not enough context here to diagnose. Where do you register the callback? What is reference_to<> and Address?
Last edited on
Topic archived. No new replies allowed.