Sign in to follow this  
gamechampionx

C++ "glint double free or corruption"

Recommended Posts

gamechampionx    140
I'm getting back into c++ after not using it for a while and I'm running into a strange memory-related error. Code: main.cpp
#include <iostream>
#include "localdatabase.h"
#include "localdatabase.cpp"
#include "externalconnectionslist.h"
#include "externalconnectionslist.cpp"
using namespace std;

int main()
{	
	LocalDatabaseFactory* f = new LocalDatabaseFactoryImpl();
	LocalDatabase* ldb = f->getInstance();
	cout << ldb << endl;
	delete ldb;
	cout << f << endl;
	delete f;

	ExternalConnectionsListFactory* fact = new ExternalConnectionsListFactoryImpl();
	ExternalConnectionsList* l = fact->getInstance();
	cout << l << endl;
	delete l;
	cout << fact << endl;
	delete fact;
	
	return 0;
}

localdatabase.h:
#ifndef LocalDatabaseHeader
#define LocalDatabaseHeader

class LocalDatabaseEntry
{	
};

class LocalDatabase
{
};

class LocalDatabaseFactory
{
public:
	virtual LocalDatabase* getInstance() = 0;
};
#endif

localdatabase.cpp
#include "localdatabase.h"
#include <iostream>
using namespace std;

class LocalDatabaseImpl : public LocalDatabase
{
};

class LocalDatabaseFactoryImpl : public LocalDatabaseFactory
{
public:
	LocalDatabase* getInstance();
	void barf();
private:
	LocalDatabase* instance;
};

LocalDatabase* LocalDatabaseFactoryImpl::getInstance()
{
	if(instance == 0)
		instance = new LocalDatabaseImpl();
	return instance;
}

externalconnectionslist.h:
#ifndef EXTERNAL_CONNECTIONS
#define EXTERNAL_CONNECTIONS

class ExternalConnection
{
public:
	char* getUserName();
};

class ExternalConnectionsList
{
public:
	virtual ExternalConnection* getNameServer() = 0;
	virtual int getNumPeers() = 0;
	virtual ExternalConnection* getPeer(int index) = 0;
};

class ExternalConnectionsListFactory
{
public:
	virtual ExternalConnectionsList* getInstance() = 0;
};

#endif

externalconnectionlist.cpp
#include "externalconnectionslist.h"

char* ExternalConnection::getUserName()
{
	return 0;
}

class ExternalConnectionsListImpl : public ExternalConnectionsList
{
public:
	ExternalConnection* getNameServer();
	int getNumPeers();
	ExternalConnection* getPeer(int index);
};

ExternalConnection* ExternalConnectionsListImpl::getNameServer()
{
	return 0;
}

int ExternalConnectionsListImpl::getNumPeers()
{
	return 0;
}

ExternalConnection* ExternalConnectionsListImpl::getPeer(int index)
{
	return 0;
}

class ExternalConnectionsListFactoryImpl : public ExternalConnectionsListFactory
{
public:
	ExternalConnectionsList* getInstance();
private:
	ExternalConnectionsList* instance;
};

ExternalConnectionsList* ExternalConnectionsListFactoryImpl::getInstance()
{
	if(instance == 0)
		instance = new ExternalConnectionsListImpl();
	return instance;
}

This code isn't pretty or particularly well-designed, I'm just testing out some classes as showing. I keep getting the following error: 0x804a018 0x804a008 0x804a018 *** glibc detected *** ./a.out: double free or corruption (fasttop): 0x0804a018 I don't get how I could be double-deleting a pointer as I've already deleted ldb before even declaring l. This completely boggles me. I'm probably doing something really horrible with pointers without realizing it, but this message isn't really helping me much. Is there a way to get more info about these errors? My pointer appears to be on the heap which is where it should be as far as I can understand.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this