I am trying to make a basic any style class in C++, called object. It compiles successfully, but before anything happens, I get the error: Unhandled exception at 0x008128C1 in object.exe: 0xC0000005: Access violation reading location 0xCCCCCCCC.:
#include <typeinfo>
struct object
{
public:
template < typename T > struct Data
{
public:
Data(T value) : val_(&value){}
Data() : val_(nullptr){}
T* value()
{
return val_;
}
~Data()
{
delete &val_;
}
template < typename Tn > void Swap(Data<Tn>* D)
{
if (std::is_destructible<T>())
{
val_->~T();
}
Tn n_val = (Tn)val_;
std::swap<Tn>(&n_val, D->value());
}
private:
T* val_;
};
struct Inner : Data<void*>
{
template < typename T > void Cast()
{
Swap<T>(new Data<T>((T)NULL));
}
template < typename T > void Cast(const T& value)
{
Swap<T>(new Data<T>(value));
}
};
private:
Inner* Held;
public:
template < typename T > object(const T& value)
{
Held->Cast<T>(value);
}
template < typename T > void operator=(const T& value)
{
Held->Cast<T>(value);
}
template < typename T > void cast()
{
Held->Cast<T>();
}
template < typename T > void cast(const T& value)
{
Held->Cast<T>(value);
}
~object(){ delete Held; }
const void* operator()() const
{
return *Held->value();
}
};
And then in my test file
#include <iostream>
int main()
{
object MyObject = 5;
std::cout << MyObject();
}
Notice that you are doing delete Held; even though you never usednew. You never actually assign anything to Held, so it is uninitialized when you attempt to do Held->Cast<T>(value);. You're going to have to allocate a Held object in some way before you can do that.
You also have a problem your Data struct. Its constructor takes a copy of its argument, and then you store a pointer to that copy. That copy is local to the constructor though and will be destroyed when the constructor ends. The val_ pointer is left pointing at the destroyed object. Not only that, but you then do delete &val_; which attempts to deallocate the object that had automatic storage duration.
You really shouldn't be using new and delete as much as you are, and you would avoid many of the problems you're having.
Your object class is expecting data that has been allocated on the heap. This can be seen since you are accessing that data via a pointer in your object class, swapping its value, and deleting that pointer.
In your main function you are building an object with a literal value of 5. This value has not been allocated on the heap because no allocation was called for (no call to new exists). 5 may have been allocated on the stack, but as a literal it might also be stored in program ROM by the compiler, or any other location where it is dangerous to access the memory address.
The moment your code attempts to modify the data at the address of the literal value 5, you have committed an access violation because you are accessing memory that you are not allowed by your program to modify directly.
To solve this you probably want your object class to allocate a copy of the data being passed to it on the heap that it can then take ownership of and modify and delete at will.
Related
The following code is abstracted from the book << Hands-On Design Patterns with C++ >> by Fedor G. Pikus published by Packt.
Some confusions have been bugging me for weeks.
(1) How the char array mem_ is initialized?
(2) Is allocate used to allocate memory? How?
(3) Why does mem_ == p ? How was the memory delocated?
// 02_scoped_ptr.C
// Version 01 with deletion policy.
#include <cstdlib>
#include <cassert>
#include <iostream>
template <typename T, typename DeletionPolicy>
class SmartPtr {
public:
explicit SmartPtr(T* p = nullptr,
const DeletionPolicy& deletion_policy = DeletionPolicy() )
: p_(p), deletion_policy_(deletion_policy) {}
SmartPtr(const SmartPtr&) = delete;
SmartPtr& operator=(const SmartPtr&) = delete;
~SmartPtr() { deletion_policy_(p_); }
void release() { p_ = NULL; }
T* operator->() { return p_; }
const T* operator->() const { return p_; }
T& operator*() { return *p_; }
const T& operator*() const { return *p_; }
private:
T* p_;
DeletionPolicy deletion_policy_;
};
class SmallHeap {
public:
SmallHeap() {}
SmallHeap(const SmallHeap &) = delete;
SmallHeap &operator=(const SmallHeap &) = delete;
~SmallHeap() {}
void * allocate(size_t s) {
assert(s <= size_);
return mem_; // ------------------ is allocate used to allocate memory? how?
}
void deallocate(void *p) {
assert(mem_ == p); // ------------------- why does mem_ == p ? How was the memory delocated?
}
private:
static constexpr size_t size_ = 1024;
char mem_[size_]; // ------------------- how mem_ is initialized?
};
void * operator new(size_t s, SmallHeap *h)
{
return h->allocate(s);
}
template<typename T>
struct DeleteSmallHeap {
explicit DeleteSmallHeap(SmallHeap &heap)
: heap_(heap) {}
void operator()(T *p) const {
p->~T();
heap_.deallocate(p);
}
private:
SmallHeap &heap_;
};
int main() {
SmallHeap a_sh_obj;
SmartPtr<int, DeleteSmallHeap<int>> sh_sp{new(&a_sh_obj) int(42), DeleteSmallHeap<int>(a_sh_obj)};
std::cout << *sh_sp << std::endl;
}
------------------ Update 1 : how is char related to memory? --------------------
Thanks for the helpful explanations, and I need some time to them, especially the custom allocator.
But one thing that is really strange to me is that:
we are talking about memory stuff, but why do we need a char array here?
This code demonstrates a custom allocator which has a static fixed size of size (1024). There is no allocation, but it can be used as an allocator on a STL container on the assumption that you will never need more than 1024 bytes.
If you do need more, boom.
char mem_[size_];
This line initializes the static size and allocate() simply returns that without any call to new.
For the deallocation it uses a simple assert to ensure that the memory that is to be 'deleted' is the same than the one that was 'created'.
All these practises are practically non existant. If you do need a vector of a static size, use a std::array. If you need a vector of an unknown size, use the reserve() vector function to preallocate. If your vector's size is unknown but expected to be small, it's okay to leave it as it is for, in Windows (and I assume in other OSes), it eventually calls HeapAlloc and HeapFree which, for small allocations, are probably cheap, especially if the vector is within a limited scope.
If you need some flexible combination of stack/heap vector, you can use https://github.com/thelink2012/SmallVector.
How the char array mem_ is initialized?
mem_ is not initialized as in filled with values until the use of the custom new operator in new(&a_sh_obj) int(42). This only initializes a small portion of the memory though. Space is allocated on the stack however when you create the local SmallHeap a_sh_obj; variable in main.
Is allocate used to allocate memory? How?
Yes, it is used. The expression new(&a_sh_obj) int(42) uses
void * operator new(size_t s, SmallHeap *h)
{
return h->allocate(s);
}
which gets sizeof(int) passed as first parameter and &a_sh_obj as second parameter.
Why does mem_ == p? How was the memory delocated?
On destruction of sh_sp the DeleteSmallHeap<int> object is used to get rid of the object. The assert is just verification that the memory "freed" is actually the one expected. This doesn't actually deallocate anything, since the memory is still owned by a_sh_obj. It's leaving the main function that in fact releases the memory during when cleaning up a_sh_obj.
I have two classes,
template<class Type>
class SafePtr {
public:
SafePtr() {}
~SafePtr() {}
void Lock(Type* data, void* key)
{
if (!pKey)
{
pKey = key;
pData = data;
}
}
Type* Unlock(void* key) const
{
if (key == pKey)
return pData;
}
Type* operator->()
{
return pData;
}
private:
Type* pData = nullptr;
void* pKey = nullptr;
};
template<class Type>
class SafePtrArray {
public:
SafePtrArray() {}
~SafePtrArray() {}
template<class... Args>
SafePtr<Type> CreatePtr(Args&&... args)
{
Type* data = new Type(args...);
ptrs.insert(ptrs.end(), data);
SafePtr<Type> ptr;
ptr.Lock(data, this);
return ptr;
}
Type* UnlockPtr(const SafePtr<int>& ptr)
{
return ptr.Unlock(this);
}
void Destroy(const SafePtr<int>& ptr)
{
Type* pointer = ptr.Unlock(this);
for (auto itr = ptrs.begin(); itr != ptrs.end(); itr++)
{
if ((*itr) == pointer)
{
delete pointer;
ptrs.erase(itr);
}
}
}
private:
std::vector<Type*> ptrs;
};
The goal is to protect a pointer so that the user can access its members but not get to manipulate its actual pointer (mainly delete it prematurely). And also I need to store all the pointers in an array so that when the parent object destroys, I can automatically destroy all the allocated pointers
For this I use two classes, SafePtr and SafePtrArray. SafePtrArray creates and stores the pointers and wraps them in the SafePtr and returns it to the user. SafePtr is just a wrapper and should not let the user get access to the underlying pointer but will allow them to access its members.
It works fine at first but soon I found this error,
int main()
{
SafePtrArray<int> ptr;
auto pInt = ptr.CreatePtr();
int* i = pInt.operator->(); // Users can get access to the underlying pointer using this.
ptr.Destroy(pInt);
}
Is there a way to prevent users from getting access to the underlying type and prevent them from manipulating the pointer while having the privilege to access its members?
I still think you try to solve a problem that has more to do with possible flaws in the design of the API/of the code, the documentation, or with the lack of C++ knowledge of the one using it, with a "solution" that has more cons than pros.
If a C++ programmer does not know what ownership is or does not respect it and blindly deletes objects or frees the memory of pointers, then there will be much bigger concerns. You likely will move the problem just to a different part of the code.
Having that said, the closest you can do to not expose the pointer right now is something like this:
(The code is just a proof of concept, so things like call might need to be improved)
#include <iostream>
#include <string>
struct Test {
void foo(int x, int y, std::string str) {
std::cout << x << " " << y << " " << str << std::endl;
}
double test = 0.5;
};
template <typename T>
struct Ptr {
template <auto M, typename... Args>
auto call(Args... args) {
return (obj.*M)(std::forward<Args>(args)...);
}
template <auto M>
auto get() {
return (obj.*M);
}
protected:
T obj;
};
int main() {
Ptr<Test> p;
p.call<&Test::foo>(1, 2, "hello");
std::cout << p.get<&Test::test>() << std::endl;
return 0;
}
But I still don't think that this is a good approach.
And a user can still mess around with the code and do something bad like:
int main() {
Ptr<Test> p;
delete &p;
return 0;
}
Or this, which for sure is undefined behavior, but that does not really matter as deleting a not owned object will also result in undefined behavior at some point:
template<typename T>
struct Ptr {
protected:
T *obj;
}
template<typename T>
struct Ptr2 {
public:
T *obj;
};
int main()
{
Ptr<Test> p;
Ptr2<Test> *p2 = reinterpret_cast<Ptr2<Test>*>(&p);
std::cout << p2->obj << std::endl;
}
So there is no protection again such things.
Besides the shown code, there is a proposal for reflection that is feature complete now, which would allow getting information about the members of a type, but this was not added to c++20, and one for metaclasses which is also not in the standard yet.
With these two proposals, you might be able to implement something better useable. But my concerns about the benefits of this remain.
Is there a way to prevent users from getting access to the underlying type and prevent them from manipulating the pointer while having the privilege to access its members?
Under certain conditions, no, this is not possible. If the underlying Type is a standard layout class then providing access to the first non-static non-bitfield data member breaks your goal. (Caveat: providing access to just the value of the member is a different story.) The address of that member can be converted to a pointer to the underlying object via reinterpret_cast, which allows calling delete on that pointer. (Well, "allows" in the sense that the call is syntactically valid. Not much else matters for "allows" since we are headed into undefined behavior anyway.)
For classes that are not standard layout, there are probably compiler-specific (non-portable) methods to achieve the same effect (converting the address of a data member into a pointer to the underlying object). There is no reason for a compiler to try actively to thwart such things.
If a programmer is determined to invoke undefined behavior, there is little you can do to stop it.
How would i use Write Process Memory with a std::vector
This works if i use std::vector
Im not sure if this any class is returning right info when using .Data()
class any
{
private:
struct base {
virtual ~base() {}
virtual base* clone() const = 0;
};
template <typename T>
struct data : base {
data(T const& value) : value_(value) {}
base* clone() const { return new data<T>(*this); }
T value_;
};
base* ptr_;
public:
template <typename T> any(T const& value) : ptr_(new data<T>(value)) {}
any(any const& other) : ptr_(other.ptr_->clone()) {}
any& operator= (any const& other) {
any(other).swap(*this);
return *this;
}
~any() { delete this->ptr_; }
void swap(any& other) { std::swap(this->ptr_, other.ptr_); }
template <typename T>
T& get() {
return dynamic_cast<data<T>&>(*this->ptr_).value_;
}
};
template<typename T>
size_t vectorsizeof(const typename std::vector<T>& vec)
{
return sizeof(T) * vec.size();
}
std::vector<any> args{100, 1.1f};
WriteProcessMemory(hProc, pMemory, args.data(), vectorsizeof(args), nullptr)
There has to be a easy way to use std::any in a vector with Write Process Memory.
No, there does not.
Ignoring what std::any does with the value it stores for the moment, std::any stores its value (as if) indirectly. This means that it is rather like a vector<T>; it stores a pointer to the object which it allocated on the heap (except the T is hidden from the type and it only stores one of them). So copying the bits of the any itself will not (necessarily) make the T it stores visible; you are (may be) copying a pointer, not what it points to.
Furthermore, any can't be delivered across processes like this, for many reasons. Even if you could get access to the byte-range of the object being stored by an any, you wouldn't be able to use that to reconstruct the any on the other side. And even if you somehow could, you'd somehow have to transmit the type_index that represents the type stored in the any. And that isn't cross-process compatible; a type index value for a particular type in one process can be different from the index for the same type in the other process.
What you want simply isn't going to work. You'll have to use something else, and that something else is going to have to have some knowledge of the type of data it's transmitting.
I have a void pointer of which I can set the value just fine (at least I think I did it right). But when I try to get the value of what is stored there, all I get nothing back. Doesn't matter if the void* points to a string or int or anything else.
What am I missing here?
class Vertex2{
public:
int _id;
void *_data;
template<typename T>
T getData() {
T *value = (T*)_data;
return *value;
}
template <typename T>
void setData(T data) {
_data = &data;
}
};
void setData(T data) receives data by value.
Setting a pointer to data therefore is only valid for the lifetime of that function call.
After that, the pointer dangles, and dereference behaviour is undefined.
template <typename T>
void setData(T data) {
_data = &data;
}
let's check what's going on here. You store a pointer to a local variable (method argument actually). Right after you leave the method the local var is destroyed and its memory is free to be reused. Now your void* points to the same memory address but the memory can contain anything.
Try it like this:
// the entire class should be templated and you should not cast the data to void
template<typename T>
class Vertex2
{
public:
int _id;
// data is no longer void
T m_data;
// now returning a const pointer to your data and protect it against manipulation from outside
getData() const {
return m_data;
}
// was setting the address of a temporary, that will not work. Now it takes a copy and moves that to the member.
void setData(T data) {
m_data = std::move(data);
}
};
I have added comments in the code.
As to your code
template <typename T>
void setData(T data) {
_data = &data;
}
Do not do that. You store the address to the temporary copy of data. This will go wrong!
void *_data;
Do not store the data as void, template the class like this:
template<typename T>
class Vertex2
{
T m_data;
.
.
.
There's nothing stored there.
You set the pointer to point to a function argument, that then went out of scope.
You can cast as much as you like, but that object has gone!
This design won't work unless you dynamically allocate.
Consider a std::variant or something instead.
hi i am delevop a multicontainner using templates, but i am getting a segmentation fault from the child class destructor, here is the code:
#include <algorithm>
#include <map>
#include <iostream>
class BaseType{
public:
virtual ~BaseType(){}
virtual BaseType * clone() const =0;
};
template<typename T>
class DataType : public BaseType
{
public:
DataType(const T & aValueData = T()):mValue(aValueData) {
// new DataType<T>(*this)
}
~DataType(){
}
BaseType * clone() const
{
return new DataType<T>(*this);
}
T mValue;
};
class MValueData
{
public:
template<typename T>
MValueData(T const & aAnyValue = T()):mTypeData(0),isDelete(false)
{
std::cout<<"Object Address before create object: "<<mTypeData<<std::endl;
mTypeData=new DataType<T>(aAnyValue);
std::cout<<"Object Address after create object"<<mTypeData<<std::endl;
}
~MValueData(){
std::cout<<"Object Address "<<mTypeData<<std::endl;
delete mTypeData;
mTypeData=0;
}
MValueData()
{
mTypeData=0;
}
template<typename T>
MValueData(const MValueData & aCopy)
{
mTypeData= new DataType<T>();
*mTypeData=aCopy.mTypeData;
}
template<typename T>
const MValueData & operator=(const MValueData & aCopy)
{
mTypeData= new DataType<T>();
*mTypeData=aCopy.mTypeData;
//MValueData(aCopia).swap(*this);
}
void swap(MValueData& other) {
std::swap(this->mTypeData, other.mTypeData);
}
template <typename T>
T& get()
{
return dynamic_cast<DataType<T>&>(*this->mTypeData).mValue;
}
bool operator <(const MValueData &rhs) const {
return (mTypeData<rhs.mTypeData);
}
template<typename T>
void setValue(T const & anyValue=T())
{
mTypeData= new DataType<T>(anyValue);
}
BaseType *mTypeData;
private:
bool isDelete;
};
int main()
{
MValueData aAnyType_1(0.22);
aAnyType_1.get<double>();
MValueData aAnyType_2(false);
std::map<MValueData , MValueData&> mMapa;
mMapa.insert(std::pair<MValueData , MValueData&>(aAnyType_1,aAnyType_2));
// mMapa.find(aAnyType_1);
return 0;
}
I am using GDB to determinate the bug but i cannot see proper way to fix, the segmentacion stop when i comment this line:
~MValueData(){
// if(mTypeData) delete mTypeData;
}
Only then it run propperly, but it seems that i am creating a memory leak.
Updated:std::map create copys of the object that i insert into, the object is destroyed twice, one when exit the main function and another when std::map is destroying it self,
any hint?
thx in advance!
This segmentation fault might appear to be in the destructor, but it is a problem in your copy constructor. Lets take a simple example, I have a class which stores a pointer. Then I copy this pointer value like you are doing: I will have two pointers to the same memory location. Now I delete one of these objects, thus deleting the value at the pointer. The second object will have a pointer to invalid memory, and when this tries to delete the memory you get a segmentation fault.
How to fix this:
Well there are a few ways actually. Firstly, you need to decide whether you want deep copying of the pointers or not. If you do, write a deep copy of the memory the pointer points too. If not I reocmmend using shared_ptr to avoid these sorts of problems.
Your copy constructor is broken. You are not cloning the object, but only the pointer. Once an object is copied, the two copies will try to delete the same real object in memory in the destructor and that will cause a crash. You need to figure out who should own the object and whether you want it cloned or shared among the different instances of the MValueData object. Then act accordingly to fix the issue.
Your copy constructor is not correct.
It only copies the pointer not the object.
The two objects copied with the copy constructor will try to delete the same object in their destructor.