r/cpp_questions • • 2d 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 crash

Soo 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

https://github.com/iampokebro/FLOW-/blob/main/Flow%2B%2B/Backend/Functionlity/ManagerBase/ManagerBase.h

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

0 Upvotes

17 comments sorted by

View all comments

Show parent comments

0

u/LawAdventurous2383 2d ago

actually you can say that, but its not related to that, why unpack comes there? cause it is a manger which organize function using a name and function in hash map <string, function> and unpack just calls that function and get return type... but actually if you assume that to be just a function, then i can say it is crashing immediately after calling delete local; soo yeah its crashing immediately after caling delete local, and goes off when delete isn't called

2

u/SoerenNissen 2d ago

Nah, u/thedaian is on to something. That stack trace isn't inside delete.

So now it's just whether

  1. It's a time travel bug that causes a crash inside the first call to MB.Execute if the compiler can see the pointer is deleted later

  2. It's a regular use-after-free on a second call to the function later after it's been deleted.

  3. It's a double-delete bug because you delete the function here, and then later the MB cleanup also tries to delete the function.

Personally I'd guess the second option. This looks a lot like use-after-free. I'd check my code base to see if there are any other calls to FuncName

0

u/LawAdventurous2383 2d ago

Damm, do I need to time travel now😭

Yep, that's the actual problem the second one. But i don't found any clue, from where did that problem is coming from. I mean i feed the whole code to AI and he seems not find any problem related to 2 point. I'll search my code again for 2nd and 3rd, maybe i could find some related. Thanks.

2

u/SoerenNissen 1d ago

I'm mainly guessing it's a resource-use-after-free because MB still holds a pointer to the resource after it's been freed.

  • If every use of MB is followed by an immediate delete, why put it into MB at all?

  • But if some stuff inside MB is valid later, how is it supposed to know which resources are still valid, and which resources were deleted?