# Weird heap pointer violation

This topic is 5195 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Greetings, I'm writing a framework for my applications, and I've come accross an interesting problem. My project is set up in a .dll, with a .lib file to link it to the test project. There is this one class, CMeshNode, that resides in the .lib/.dll file, and crashes whenever I call delete on it. Here is the header file code:

class __declspec(dllexport) CMeshNode : public CSceneNode
{
protected:
CXFile *m_pFile;
float	m_fOldAlpha;
float   *m_pfAlphas;

public:
CMeshNode( const optional<ActorId> actorId,
CXFile *mesh,
RenderPass renderPass,
const Mat4x4 *t );

virtual ~CMeshNode() {}

virtual HRESULT onRestore(CSceneGraph *pScene );
virtual HRESULT onRender( CSceneGraph *pScene );
virtual HRESULT onInvalidate();

float	calcBoundingSphere();
};


Now, the interesting part, is that when I copy the class over to the test project, remove the __declspec(dllexport), and call delete on it, it works just fine. So what am I screwing up here? Is there some rule about dll's and classes that I'm breaking? If you have experienced this before and know how to fix it, I would appreciate the heads up.

##### Share on other sites
You can't allocate memory in the DLL and free it from in the EXE, or vice versa, because the memory is allocated from different heaps. This isn't a problem in Linux / Unix apparently, but it is under Windows.

Where are you calling new and delete from?

##### Share on other sites
It sounds like it's trying to delete something that doesn't belong to it.

Do you allocate any data with new in the client app, and and pass it to this class? Does it then get deleted with constructor?

Are there any stl::objects being passed in or out that might be getting resized?

Dlls use a separate heap to the client executable, so anything you allocate with new in the dll must also be deleted by the dll, and anything you allocate with new in the client exe must also be deleted in the client exe. Alternatively, you can get around this using the Win32 memory allocation functions instead of new

If it's an stl object that's causing the problem, see this thread.

##### Share on other sites
I found that it's the dll and the exe both having different heaps.
Is using that alternate memory allocation a good idea? Or should I just ditch the dll implemenation (I'd rather not).

##### Share on other sites
All you have to do is make sure that in your design if something is allocated from within the DLL and given to the exe, that you pass it back to some function in the DLL to have it deleted for you. It makes sense to do this anyway if you think about it.

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 16
• 11
• 23
• 42
• 75