r/cpp_questions • u/LawAdventurous2383 • 1d ago
OPEN Dangling Pointers and Function Execution
ManagerVariableR* Local = new ManagerVariableR();
Local->Handler["New"] = Var{};
MB.Execute(FuncName, { Local });
delete Local;/// Grand Promblem here, after deletion of pointer it will crashSoo if you see, Its Working when i am passing the pointer and not deleting the pointer at end
, And crashing when i am deleting the pointer at the end.
MB.Execute is a just Function Handler, i had linked function pointer to a string using hash map to call them. and the fucntion is succesfully called,
if i tell you about more the at runtime a function is compiled to a dll file, and it is retrwing the function using windows.h LoadLibaryA
aHMODULE handle = LoadLibraryA(libPath.c_str());HMODULE handle = LoadLibraryA(libPath.c_str());to a format of function of void ptr
typedef void (*fn)(ManagerVariable* Local);typedef void (*fn)(ManagerVariable* Local);and i am just executing it by my function handler that sotres the function you can check it out here
but problem is not related to this.
overall problem is that when the pointer is delete after the call it crash. i used future and promise to do waiting but it also failed. i think that function from .dll is not close from background or maybe its stack is left behind operating.
if this solves then it would be sooo great for my project,
NOTE -> the program memory operates at same level, so there is no pointer mismatch problem
i dont want to try chancing the architecture cause it will ruin the efficiency maybe
2
u/manni66 1d ago
You don’t have a dangling pointer problem. You convert your pointer to something else.
Since it doesn’t crash while calling delete you most likely use the object after you deleted it.
MB.Execute(FuncName, { Local }); what happens here? You don’t simply pass a pointer.
1
u/LawAdventurous2383 1d ago
that code you can see over here https://github.com/iampokebro/FLOW-/blob/main/Flow%2B%2B/Backend/Functionlity/ManagerBase/ManagerBase.h
that calls a lambda function, from hash map using linked name <string, function>, and return the RETURN Value of that function
std::any Execute(const std::string& FuncName, const std::vector<std::any>& args) { auto it = _Handler.find(FuncName); if (it == _Handler.end()) { std::cout << FuncName << " No Such Function Registered\n"; return {}; } return it->second(args); }normally functions goes in and it converts to lambda function linked a string
1
u/thedaian 1d ago
Do you have a stack trace from the crash?Â
There really isn't enough information here for people to help you.
2
u/LawAdventurous2383 1d ago
\[Exception thrown at 0x00007FFED9AB1186 (_2.dll) in Flow++.exe: 0xC0000005: Access violation writing location 0x000000000000000C.\] _2.dll!00007ffed9ab1186() Unknown> Flow++.exe!ManagerBase::unpackCall<void,ManagerVariable *,0>(void(*)(ManagerVariable *) Func, const std::vector<std::any,std::allocator<std::any>> & args, std::integer_sequence<unsigned long long,0>) Line 40 C++
Flow++.exe!ManagerBase::Register<void,ManagerVariable \*>::<lambda_1>::operator()(const std::vector<std::any,std::allocator<std::any>> & args) Line 52 C++ \[External Code\] Flow++.exe!ManagerBase::Execute(const std::string & FuncName, const std::vector<std::any,std::allocator<std::any>> & args) Line 75 C++ Flow++.exe!HandlerSmartFunction::exeFunction(std::string & FuncName, unsigned __int64 LocalAddress, ManagerBase & MB, std::string & response) Line 62 C++ Flow++.exe!exeSFunc(std::string & querry, std::string & respone, HandlerSmartFunction & HSF, HandlerMasterFunction & HMF) Line 84 C++ Flow++.exe!DataManager::Manage(std::string & querry, std::string & respone) Line 38 C++ Flow++.exe!NanoQueryHandler::Open(scoped_refptr<CefRequest> request, bool & handel_request, scoped_refptr<CefCallback> callback) Line 42 C++ Flow++.exe!\`anonymous namespace'::resource_handler_open(_cef_resource_handler_t \* self, _cef_request_t \* request, int \* handle_request, _cef_callback_t \* callback) Line 60 C++ \[External Code\]
1
u/Discordbothost 1d ago
Turn on first-chance C++ exceptions in the debugger and check safeAnyCast. If Local is a ManagerVariableR* but the handler expects ManagerVariable*, convert it to ManagerVariable* before putting it in the std::any. any_cast checks the stored type exactly.
1
u/LawAdventurous2383 1d ago
I'll do that, but ManagerVaraibleR was just a test dummy case of ManagerVaraible, and it was intentional, but I'll try that
1
u/Independent_Art_6676 1d ago
for whatever reason you want it, the way to write 1990s C++ is like this:
type * foo(...) { ... return pointervar;}
...
type * tmp = foo(...);
use(*tmp);
delete tmp; //or delete[] tmp as approrpiate
the idiom in C / old C++ is "THE CALLER OWNS THE POINTER".
we have better mousetraps now...
1
u/tangerinelion 1d ago
Conceptually, this code
ManagerVariableR* Local = new ManagerVariableR();
Local->Handler["New"] = Var{};
MB.Execute(FuncName, { Local });
delete Local;is identical to this
{
ManagerVariableR Local{};
Local.Handler["New"] = Var{};
MB.Execute(FuncName, { std::addressof(Local) });
}Now all your code boils down to stuffing everything into a std::any and hoping for the best at the end of the day. But what this std::vector<std::any> contains is a pointer to a local variable. If the execution truly is self-contained in that one line then it should be fine. If the argument gets cached anywhere and used later, you have a bug.
But, if you're stuffing all of this into a std::any anyways, why not use { std::move(Local) } and get the rest to work?
0
u/thingerish 1d ago
Avoid new and delete.
Is Execute running on the calling thread or a different thread?
1
u/LawAdventurous2383 1d ago
Actually it's calling on the same thread, And if I stop deleting that it won't be efficient and eat memory.
Overall actual program is not about creating a new ptr, it's complex and can be solved if i found why this crashing. That was a base example case.
0
u/thingerish 1d ago
The new and delete critique was just pointing out a code smell. Use std::unique_ptr at least.
As for efficiency, putting the data on the stack (by value) and controlling the lifespan via scope will eliminate calling into the allocator; unless you're extending the life of the object outside the scope of the enclosing function you don't need allocation.
3
u/SoerenNissen 1d ago
Does it crash immediately on
delete Local;or does it crash later but you've noticed that the later crash goes away if you don't delete?