Jump to content


Malloc returns 0


  • You cannot reply to this topic
12 replies to this topic

#1 Dom_152

    Member

  • Members
  • PipPip
  • 67 posts

Posted 04 March 2007 - 12:14 PM

		

//GetVertexData

//Returns the vertex data

virtual void *GetVertexData()

{	

	T *vertexData = (T *)malloc(m_numVertices * sizeof(T));


	for(int i = 0; i < m_numVertices; i++)

	{

		vertexData[i] = m_vertexData.at(i);

	}


	return (void *)vertexData;

}	


As far as I can see there is nothing wrong with this code. It simply takes data from a std::vector and puts it in vertexData. But for some reason malloc is returning 0 so no memory is allocated for the pointer. I've done some debugging and m_numVertices is definatly non-null as is sizeof(T) (This is a templated function BTW). In fact the values are something like m_numVertices = 12 and sizeof(T) = 24. Any ideas why it would return 0?

#2 Nils Pipenbrinck

    Senior Member

  • Members
  • PipPipPipPip
  • 597 posts

Posted 04 March 2007 - 01:36 PM

either you really don't have any memory on the heap anymore (unlikely, but...)

Or you've messed up the heap somehow. Candidates for such mess ups are overwrites (e.g. allocating 1000 bytes but writing 1008), underwrites (writing to an array with negative offsets). Also double frees and double deletes can cause such problems.

If you're working with visual c++ you can enable the run time library heap-checking feature.

Include crtdbg.h

and at the very start of your program add the line:

  _CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_CHECK_ALWAYS_DF | _CRTDBG_LEAK_CHECK_DF);

Also: Is there a reason why you use malloc instead of new? You should not mix malloc and new in a c++ program. Chances are that you allocate something using malloc and call delete on it (or vice versa) That's undefined behaviour.
My music: http://myspace.com/planetarchh <-- my music

My stuff: torus.untergrund.net <-- some diy electronic stuff and more.

#3 Dom_152

    Member

  • Members
  • PipPip
  • 67 posts

Posted 04 March 2007 - 02:26 PM

I use malloc because I need to allocate enough memory for multiple instances of the object T. I thought the new keyword only allocated enough room for one?

#4 monjardin

    Senior Member

  • Members
  • PipPipPipPip
  • 1033 posts

Posted 04 March 2007 - 03:27 PM

Replace
T *vertexData = (T *)malloc(m_numVertices * sizeof(T));
with
T *vertexData = new T[m_numVertices];

When freeing the data remember to use the delete[] operator. If you are not familiar with these concepts then try this link.

EDIT: That link doesn't seem to address some important differences between malloc/free and new/delete. For example, new calls a class constructor and delete calls a class destructor. Neither malloc nor free does so.
monjardin's JwN Meter (1,2,3,4,5,6):
|----|----|----|----|----|----|----|----|----|----|
*

#5 Rubicon

    Member

  • Members
  • PipPip
  • 90 posts

Posted 04 March 2007 - 03:58 PM

Nils Pipenbrinck said:

Also: Is there a reason why you use malloc instead of new? You should not mix malloc and new in a c++ program. Chances are that you allocate something using malloc and call delete on it (or vice versa) That's undefined behaviour.
Technically true, but almost everyone overides one to do the other anyway :) If you do do that, you can put all sorts of debugging data around the free ram you return. I can fill memory before and after the returned window by a defined amount as an aid to catching this kind of crap. When you fill 16K either side of every 2 byte malloc, your odds of catching the scribbler go way up :)
Regards, Paul Johnson
www.rubicondev.com
My Free 3D Tower Defence Game

#6 Dom_152

    Member

  • Members
  • PipPip
  • 67 posts

Posted 04 March 2007 - 04:00 PM

OK tried it and it still doesn't work. When debugging I get this exception:

Unhandled exception at 0x7c81eb33 in Test 1.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x0012f4a4.

Just after the new T[m_numVertices] line is executed. What does this mean?

#7 Rubicon

    Member

  • Members
  • PipPip
  • 90 posts

Posted 04 March 2007 - 06:18 PM

It means you didn't catch the bad_alloc exception! If you turn off exception handling in the project's settings, it'll probably say something else.

I still think this is caused by a prior corruption of the heap. You'll only get previous errors reported the _next_ time you do something (at best), such as your alloc above.

A common one is writing a stack array with a negative index. If I were you, I'd go give the trial verson of boundschecker a whirl, see if that catches it. I used to recommend that program to everyone until they started charging a trillion bucks for it.
Regards, Paul Johnson
www.rubicondev.com
My Free 3D Tower Defence Game

#8 Valadas

    New Member

  • Members
  • Pip
  • 8 posts

Posted 04 March 2007 - 06:20 PM

You mind posting the constructor/destructor code of the T class. Maybe there's something wrong with the way the object is being constructed/destructed.

Another thing you should avoid is (void*) in C++. If you're trying to return some template data shouldn't it be <T>?? (just a though, i'm not familiar with C++ templates)

#9 Dom_152

    Member

  • Members
  • PipPip
  • 67 posts

Posted 04 March 2007 - 06:46 PM

It returns a void pointer because Direct3D wants the vertex data in the form of a void pointer so I thought it would make sense to convert it in the function there. The T class constructor/destructor is just blank. Nothing happens in it.

//Vertex with a diffuse colour
class Vertex_Diffuse
{
public:
	Vertex_Diffuse() { };
	~Vertex_Diffuse() { };

	float X, Y, Z;
	unsigned long diffuse;
	float NX, NY, NZ;
}; //Class Vertex_Diffuse


#10 Valadas

    New Member

  • Members
  • Pip
  • 8 posts

Posted 04 March 2007 - 07:09 PM

I have to agree with Rubicon. I has to be something else corrupting your heap.
Do you use malloc or new anywhere else in your code?

#11 Dom_152

    Member

  • Members
  • PipPip
  • 67 posts

Posted 04 March 2007 - 07:17 PM

I do use Malloc just before the draw calls. Trying to do pretty much what I'm doing with the vertex data only with index data instead:

unsigned long *indexData = (unsigned long *)malloc(rData->GetIndexData(i).size());
for(int y = 0; y < rData->GetIndexData(i).size(); y++)
{
	indexData[y] = rData->GetIndexData(i).at(y);
}

EDIT: Fixed it. Changed that malloc call into a new.

#12 Dom_152

    Member

  • Members
  • PipPip
  • 67 posts

Posted 04 March 2007 - 08:40 PM

Ahh now I've got to work out why it's not displaying my cube :(

#13 Valadas

    New Member

  • Members
  • Pip
  • 8 posts

Posted 05 March 2007 - 01:44 AM

lol! That's a new thread altogether gook luck!





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users