PLEASE, i realy need help.. don't panic from the sharedPtr implementation
I'm having a problem with my project in c++.
In my project, I'm creating a company which holds all the information about its employees (artist, programmer,manager), projects ,...
for holding all the employees I use a vector of SharedPtr(use own Implementation)
and it seems like the vector is doing some problem:
Unhandled exception at 0x00CC8CA9 in projComp.exe: 0xC0000005: Access violation reading location 0x00000044.
and pointing me to this break point in vector-
bool _Inside(const value_type *_Ptr) const
{ // test if _Ptr points inside vector
return (_Ptr < this->_Mylast && this->_Myfirst <= _Ptr);
}
this is my SharedPtr implementation:
#ifndef _SHARED_PTR_H_
#define _SHARED_PTR_H_
#include "RCObject.h"
template<class T>
class SharedPtr {
public:
SharedPtr(T* realPtr = 0);
SharedPtr(const SharedPtr& rhs);
~SharedPtr();
SharedPtr& operator=(const SharedPtr& rhs);
const T* operator->() const;
T* operator->();
const T& operator*() const;
T& operator*();
template<class newType> // template function for
operator SharedPtr<newType>() // implicit conversion ops.
{
return SharedPtr<newType>(counter->pointee);
}
private:
struct CountHolder : public RCObject {
~CountHolder() { delete pointee; }
void addReference() {
++refCount;
}
void removeReference() {
if (--refCount == 0)delete this;
}
T *pointee;
};
CountHolder *counter;
void init();
void makeCopy();
};
template <class T>
void SharedPtr<T>::init() {
if (counter->isShareable() == false) {
T *oldValue = counter->pointee;
counter = new CountHolder;
counter->pointee = new T(*oldValue);
}
counter->addReference();
}
template <class T>
SharedPtr<T>::SharedPtr(T* realPtr) : counter(new CountHolder) {
counter->pointee = realPtr;
init();
}
template <class T>
SharedPtr<T>::SharedPtr(const SharedPtr& rhs) : counter(rhs.counter) {
init();
}
template <class T>
SharedPtr<T>::~SharedPtr() {
counter->removeReference();
}
template <class T>
SharedPtr<T>& SharedPtr<T>::operator=(const SharedPtr& rhs) {
if (counter != rhs.counter) {
counter->removeReference();
counter = rhs.counter;
init();
}
return *this;
}
template <class T>
void SharedPtr<T>::makeCopy() {
if (counter->isShared()) {
T *oldValue = counter->pointee;
counter->removeReference();
counter = new CountHolder;
counter->pointee = new T(*oldValue);
counter->addReference();
}
}
template <class T>
const T* SharedPtr<T>::operator->() const {
return counter->pointee;
}
template <class T>
T* SharedPtr<T>::operator->() {
makeCopy();
return counter->pointee;
}
template <class T>
const T& SharedPtr<T>::operator*() const {
return *(counter->pointee);
}
template <class T>
T& SharedPtr<T>::operator*() {
makeCopy();
return *(counter->pointee);
}
#endif
and this is RCObject:
#ifndef _RCOBJ_H_
#define _RCOBJ_H_
#include <iostream>
using namespace std;
class RCObject {
//private:
protected:
int refCount;
bool shareable;
protected:
RCObject();
RCObject(const RCObject&);
virtual ~RCObject() = 0;
RCObject& operator=(const RCObject&);
public:
virtual void addReference() = 0;
virtual void removeReference() = 0;
void markUnshareable();
bool isShareable() const;
bool isShared() const;
};
#endif
and its implementation:
#include <iostream>
#include "RCObject.h"
using namespace std;
RCObject::RCObject() : refCount(0), shareable(true) {
}
RCObject::RCObject(const RCObject&) : refCount(0), shareable(true) {
}
RCObject& RCObject::operator=(const RCObject&) {
return *this;
}
RCObject::~RCObject() {
}
void RCObject::addReference() {
++refCount;
}
void RCObject::removeReference() {
if (--refCount == 0) delete this;
}
void RCObject::markUnshareable() {
shareable = false;
}
bool RCObject::isShareable() const {
return shareable;
}
bool RCObject::isShared() const {
return refCount > 1;
}
the problem is with this code line:
void Company::addArtist(vector<string> empAfield, string idCmp, string currPrj, double currPrjHrs, double ttlHrs, double exp, bool isEmp, double hrsDay, string name, string lastName, string idPrsn, long phoneNum) {
_employeeList.push_back(SharedPtr<Artist>(new Artist(empAfield, idCmp, currPrj, currPrjHrs, ttlHrs, exp, isEmp, hrsDay, name, lastName, idPrsn, phoneNum)));
}
sorry for the long code, I struggle thinking what can made this problem. maybe the vector deleting the SharedPtr after its already freed? It's not supposed to be, but i run out of idea.
thanks for everyone who tries to redeem my misery ;)
The error location is 0x00000044, which usually means that this
was NULL in the method call, while it is trying to access a member variable (at location this+0x44
in this case).
Check that you are not calling addArtist()
on a NULL Company*
pointer.
User contributions licensed under CC BY-SA 3.0