Prev: SetWindowsHookEx() on Windows 7
Next: Consuming ETW events from multiple applications running on different machines in same network
From: Jonathan de Boyne Pollard on 5 Apr 2010 19:45 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta content="text/html; charset=windows-1252" http-equiv="Content-Type"> </head> <body bgcolor="#ffffff" text="#000000"> <blockquote cite="mid:f7df83ef-0cba-4456-8643-b9c9201fb3c8(a)35g2000yqm.googlegroups.com" type="cite"> <p wrap="">I am facing a weird issue here.<br> </p> </blockquote> <p>A quick review indicates that this is little more than a simple variation on <a href="http://homepage.ntlworld.com./jonathan.deboynepollard/FGA/dll-design-mistakes-to-avoid.html">one of the commonplace DLL design mistakes to avoid</a>.� You're mixing and matching heaps.� You're getting heap corruption, manifesting itself as odd behaviour of your string class.� Just as the Frequently Given Answer told you that you would.<br> </p> </body> </html>
From: David Schwartz on 5 Apr 2010 23:04
On Apr 5, 4:45 pm, Jonathan de Boyne Pollard <J.deBoynePollard- newsgro...(a)NTLWorld.COM> wrote: > A quick review indicates that this is little more than a simple variation on > one of the commonplace DLL design mistakes to avoid. You're mixing and > matching heaps. You're getting heap corruption, manifesting itself as odd > behaviour of your string class. Just as the Frequently Given Answer told you > that you would. Bingo! If X allocates the memory, X should free the memory. So when an application calls into a DLL, one of three things should happen: 1) The application allocates the memory before calling the DLL. The application can free the memory at its leisure using a free function compatible with however it allocated the memory. 2) The application provides the DLL with hooks it should use to allocate any memory it needs to hand over to the application. The application then frees that memory (with a method compatible with the hooks it provided the DLL) at its leisure. 3) The DLL provides the application with a "free object" call. The DLL returns an object to the application that the application can free by calling the DLL's "free object" function. The DLL uses a a freeing method that is compatible with how it allocates the objects. But what the OP did is had the DLL allocate the memory however it wanted to and then had the application free the memory however it wanted to. There is no guarantee these two methods will be at all compatible. This is a big no-no. If X allocates the memory, X should free the memory. DS |