I have a dynamically allocated memory to a pointer. Some part of code copy this pointer to many containers, but I am not aware of the exact containers. Is it possible to know (may be by registering callback or something) whenever a pointer is copied in any container (i.e. shallow copy)?
If anyone can suggest a way to achieve this through some code addition or through Valgrind.
Code
int *arr[1];
void func(int* p)
{
arr[0] = p;
}
void PrintArray()
{
cout<<arr[0]<<endl; //This is a stale pointer
}
int main()
{
int* p = new int(10);
func(p);
delete p;
PrintArray();
return 0;
}
In above code, 'p' is allocated memory in main function, and this pointer copied to array in 'func' function. Now in 'PrintArray' i am printing this array while its data is already deleted.
This is a simple case where in know the container is array. So i can handle it, but in my code base being it very large there can be many containers where i have such pointers, and accessing those giving me problems. How can i find those containers, other then again stumbling into codebase?
Aucun commentaire:
Enregistrer un commentaire