• Advertisement

Archived

This topic is now archived and is closed to further replies.

cannot delete array of pointers...

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, i''ve encountered a strange little thing. I pass a pointer by reference to a function in a DLL. The function creates a dynamic array from the pointer by using new. That works fine, the data in the array is filled like it should. But, when I try to delete the array from the main program, delete throws a runtime error. :/ any clues? Function from my DLL:
void DDraw::Get_Enumeration_Result(Enum_Result *&result, int &resultCount)
{
	int	idx = 0, sz = (int)deviceEnum.size();

	result = new Enum_Result[sz];
	resultCount = sz;

	for(enumIterator = deviceEnum.begin(); enumIterator != deviceEnum.end(); ++enumIterator)
	{
		Enum_Result		tmp = (Enum_Result)*enumIterator;

		result[idx].enumWidth = tmp.enumWidth;
		result[idx].enumHeight = tmp.enumHeight;
		result[idx].enumDepth = tmp.enumDepth;
		idx++;
	}
}
 
Code in my main program:
	Enum_Result	*res = NULL;
	int			count;

	gfx->Get_Enumeration_Result(res, count);
	for(int i = 0; i < count; i++)
	{
		char txt[100];
		sprintf(txt, "%dx%dx%d", res.enumWidth, res[i].enumHeight, res[i].enumDepth);
		MessageBox(hWnd, txt, "Enum", MB_OK);
	}
	delete[] res;
 

Share this post


Link to post
Share on other sites
Advertisement
Unfortunately, you can't free memory allocated in the dll from the main program and vice versa. I think that is because they have separate heaps, but I might be wrong there. Anyway, you'll have to include a Free-function in your dll which you call from the main program instead of deleting it.

Free(res);

or something similar. Or you could just make all your objects derive from a base object which has a Free-method, so that you just have to do res->Free();. Another option would be to have the object also do reference counting, which might be even better. These latter options sound better to me, as you don't really have to track where something was allocated, but of course it would only work with objects of your own.

Hope this helps.

[edited by - Wuntvor on May 7, 2004 10:42:02 AM]

Share this post


Link to post
Share on other sites

  • Advertisement