Click here to Skip to main content
15,889,335 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionPainfully frustrating sdk memory exception? [modified] Pin
fjdiewornncalwe13-Oct-10 1:47
professionalfjdiewornncalwe13-Oct-10 1:47 
AnswerRe: Bloody sdk memory exception? Pin
Sauro Viti13-Oct-10 2:33
professionalSauro Viti13-Oct-10 2:33 
GeneralRe: Bloody sdk memory exception? Pin
fjdiewornncalwe13-Oct-10 3:13
professionalfjdiewornncalwe13-Oct-10 3:13 
AnswerRe: Bloody sdk memory exception? Pin
Maximilien13-Oct-10 5:47
Maximilien13-Oct-10 5:47 
GeneralRe: Bloody sdk memory exception? Pin
fjdiewornncalwe13-Oct-10 5:54
professionalfjdiewornncalwe13-Oct-10 5:54 
AnswerRe: Bloody sdk memory exception? Pin
Code-o-mat13-Oct-10 5:59
Code-o-mat13-Oct-10 5:59 
GeneralRe: Bloody sdk memory exception? [modified] Pin
fjdiewornncalwe13-Oct-10 8:35
professionalfjdiewornncalwe13-Oct-10 8:35 
GeneralRe: Bloody sdk memory exception? [modified] Pin
Code-o-mat13-Oct-10 10:23
Code-o-mat13-Oct-10 10:23 
I didn't mean the code would be ugly, i meant the concept itself. Anyways, i can still try to give you the idea, maybe you can use it, maybe not, that is up to you to decide, so here's what i would do.

1. Try to determine where the memory leaks come from by trial and error (i mean, what 3rd party call, i supose you already know that, so you can skip this step)
2. Install hooks for the allocation/reallocation/deallocation methods listed here[^], see API hooking revealed[^] for hooking methods (since you are working within your own process -i supose- you don't need the DLL injection part). Depending on that SDK you might need to do the hooking for multiple modules (DLLs), again, this is up to you to determine. Since malloc/new come down to these memory management functions, this should suffice to "catch" all memory allocations.
3. In my hook functions, i would call the originals to perform the actual memory management, but i would add every allocated pointer to some array of my own, remove them when they are freed, replace them when reallocations are done. I would make this conditional based on some global flag (or such) so when it is unneeded (when no 3rd party code is executing) it doesn't take up resources uselessly.
4. Before the 3rd party call, i would turn the "allocation tracking" mentioned in point 3, let the 3rd party thing finish, turn off the tracking thing, and then analize the array i used for the pointers. If my assumptions are correct the array should contain pointers to the buffers which aren't freed up and then i could just free them up myself (be careful to use the correct methods for the deallocation, so if e.g. a pointer was allocated by HeapAlloc then use HeapFree, if VirtualAlloc then use VirtualFree).

What might go wrong is that if the 3rd party thing e.g. calls CRT's malloc, and that calls HeapAlloc, which in turn calls VirtualAlloc, and you hooked both HeapAlloc and VirtualAlloc, then you might "hit" the same pointer twice, another thing that can be -and i am afraid will be- a problem is that -as far as i know- CRT doesn't actually free up memory when you call free (or at least not always) but it somehow flags it internally for himself to be free and will reuse it later, sadly as far as i know you can't hook malloc/free.

This whole idea requires some experimenting i am afraid. In case you decide to try it, please do tell me how it went, i am curious. Good luck, if you should have any questions, feel free to hit me in the head with it. Smile | :)

[EDIT] If your third party SDK doesn't link statically to CRT (so uses the DLLs) then you might even hook malloc/free/realloc too. [/EDIT]
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
> "It doesn't work, fix it" does not qualify as a bug report. <
> Amazing what new features none of the programmers working on the project ever heard of you can learn about when reading what the marketing guys wrote about it. <

modified on Wednesday, October 13, 2010 4:47 PM

GeneralRe: Bloody sdk memory exception? Pin
fjdiewornncalwe14-Oct-10 2:30
professionalfjdiewornncalwe14-Oct-10 2:30 
JokeRe: Bloody sdk memory exception? Pin
Code-o-mat14-Oct-10 2:55
Code-o-mat14-Oct-10 2:55 
GeneralRe: Bloody sdk memory exception? Pin
Code-o-mat25-Oct-10 0:13
Code-o-mat25-Oct-10 0:13 
GeneralRe: Bloody sdk memory exception? Pin
fjdiewornncalwe25-Oct-10 4:26
professionalfjdiewornncalwe25-Oct-10 4:26 
QuestionRightJustify a widechar string Pin
T.RATHA KRISHNAN13-Oct-10 1:08
T.RATHA KRISHNAN13-Oct-10 1:08 
AnswerRe: RightJustify a widechar string Pin
Chris Losinger13-Oct-10 1:24
professionalChris Losinger13-Oct-10 1:24 
GeneralRe: RightJustify a widechar string Pin
T.RATHA KRISHNAN13-Oct-10 1:29
T.RATHA KRISHNAN13-Oct-10 1:29 
GeneralRe: RightJustify a widechar string Pin
Chris Losinger13-Oct-10 1:31
professionalChris Losinger13-Oct-10 1:31 
GeneralRe: RightJustify a widechar string Pin
CPallini13-Oct-10 1:38
mveCPallini13-Oct-10 1:38 
AnswerRe: RightJustify a widechar string Pin
Cedric Moonen13-Oct-10 1:49
Cedric Moonen13-Oct-10 1:49 
QuestionProxy authentication failed error code is 407. Same code works fine without proxy. Pin
amar_mhetre13-Oct-10 0:49
amar_mhetre13-Oct-10 0:49 
AnswerRe: Proxy authentication failed error code is 407. Same code works fine without proxy. Pin
Rahul Vaishnav13-Oct-10 1:56
Rahul Vaishnav13-Oct-10 1:56 
GeneralRe: Proxy authentication failed error code is 407. Same code works fine without proxy. Pin
amar_mhetre13-Oct-10 2:06
amar_mhetre13-Oct-10 2:06 
GeneralRe: Proxy authentication failed error code is 407. Same code works fine without proxy. Pin
Rahul Vaishnav13-Oct-10 2:45
Rahul Vaishnav13-Oct-10 2:45 
GeneralRe: Proxy authentication failed error code is 407. Same code works fine without proxy. Pin
amar_mhetre13-Oct-10 2:56
amar_mhetre13-Oct-10 2:56 
QuestionDebug assersion error Pin
MKC00213-Oct-10 0:33
MKC00213-Oct-10 0:33 
AnswerRe: Debug assersion error Pin
Niklas L13-Oct-10 0:35
Niklas L13-Oct-10 0:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.