If i allocate a void pointer like this, then i get an access violation while trying to free the pointer.
int Foo(void* ptr)
{
*((void**)ptr) = malloc(25);
((char*)ptr)[0] = 'A';
free(ptr); //crashes access violation
}
The same happens if i pass a void pointer to the function and tries to free it outside of the function
int main()
{
void* ptr;
Foo(&ptr);
printf("%s \n", (char*)&ptr); //works
free(ptr); //crashes access violation
retun 0;
}
any clues how to free this pointer?
You are not trying to free the same pointer, try
free(*((void**)ptr));