Archived

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

Memory Problem

This topic is 5019 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

Ive been working for hours on an error and im not sure what i am doing wrong. Im trying to write some funtions that will save a small array of triangles to a file The second function opens the file then calls the first function to fill a buffer with data. Then it returns the buffer and the the second function writes the buffer to a file. It works perfectly fine the only problem is in the second function i cannot free the buffer.
UCHAR   *CObjectFormat::Save ( __int64 *iSize )
{
	UCHAR *buffer;	
	int size = 0;

	// Find the size of the new file
	size += sizeof(FFileHeader);			// File header
	size += sizeof(long);				// numTriangles
	size += sizeof(Triangle) * File.NumTriangles;	// Triangle buffer

	// create the buffer
	buffer = (UCHAR *)malloc(size + 1);

	// Fill the buffer
	int cur_pos = 0;

	// Header
	memcpy(buffer, (UCHAR *)&File, sizeof(FFileHeader));

	// Num triangles	
	cur_pos = sizeof(FFileHeader);
	memcpy(buffer + cur_pos, (UCHAR *)&File.NumTriangles, sizeof(long);

	// Triangle array
	cur_pos += sizeof(long);
	memcpy(buffer + cur_pos, (UCHAR *)File.TriagleList, sizeof(Triangle) * File.NumTriangles); 

	// Set the size of the buffer and return it
	*iSize = size;
	return buffer;
}

long CFileObject::WriteData ( void )
{
	UCHAR *buffer = NULL;
	FILE *outFile = NULL;
	__int64 size = -1;

	if (pFileName == NULL)
		return E_FAIL;

	// Open the file for writing
	outFile = fopen(pFileName, "w");

	// fill the buffer
	buffer = pfileData->Save (&size);

	// write the data, then free buffer
	fwrite(buffer, sizeof(UCHAR), (size_t)size, outFile);
	fclose(outFile);

	free(buffer); //<- this is where the problem happens

        return S_OK;
}
 
when free(buffer) is called _CrtIsValidHeapPointer causes an assertion failure and in the output window it says HEAP[Launcher.exe]: Invalid Address specified to RtlValidateHeap( 088A0000, 08894F80 ) I did find this:
	 /*
         * If this ASSERT fails, a bad pointer has been passed in. It may be
         * totally bogus, or it may have been allocated from another heap.
         * The pointer MUST come from the ''local'' heap.
         */
        _ASSERTE(_CrtIsValidHeapPointer(pUserData));
 
this is the ASSERT that fails. I dont think the pointer is bad becuase when i debug it the pointer seems completly fine. Im not sure how to tell if its from the ''local'' heap though, so maybe that is the problem. If it is how can i make the pointer come from the ''local'' heap?

Share this post


Link to post
Share on other sites
long CFileObject::WriteData ( void ) is in a header that comes from another dll file but it uses templates so all the code is in the header.

UCHAR *CObjectFormat::Save ( __int64 *iSize ) is in a differnt dll file.

then both dlls are loaded into the launcher exe file

Share this post


Link to post
Share on other sites
oh duh now it makes sense. I just made a function inside the class that will call free for any pointer and it works perfectly fine now.

Thanks for the help.

Share this post


Link to post
Share on other sites