Jump to content

  • Log In with Google      Sign In   
  • Create Account


#ActualL. Spiro

Posted 12 November 2012 - 06:01 PM

You have just returned a pointer to garbage from within readShaderFile().
buffer is a local variable and then you use it to create yet another temporary copy. It is destroyed on the next statement and with it the memory it has allocated. If that code ever worked it would only be luck.
You can’t return a pointer to its buffer. Either return the buffer itself (not recommended) or pass a buffer to be used to hold the return (and return a reference to that).
As in:

std::string & readShaderFile(const std::string & fileName, std::string & buffer)
{
	 // Load file into buffer.
	 // Return buffer.
	 return buffer;
}


Notice also that fileName is now a reference. You should never pass std::string or other structures by copy.
Also notice that buffer is passed by reference. Not only is this once again a good idea, it is 100% necessary for this to work properly.


L. Spiro

#3L. Spiro

Posted 12 November 2012 - 05:53 PM

You have just returned a pointer to garbage from within readShaderFile().
buffer is a local variable. It is destroyed when the function returns and with it the memory it has allocated. If that code ever worked it would only be luck.
You can’t return a point to its buffer. Either return the buffer itself (not recommended) or pass a buffer to be used to hold the return (and return a reference to that).
As in:

std::string & readShaderFile(const std::string &fileName, std::string &buffer)
{
	 // Load file into buffer.
	 // Return buffer.
	 return buffer;
}


Notice also that fileName is now a reference. You should never pass std::string or other structures by copy.
Also notice that buffer is passed by reference. Not only is this once again a good idea, it is 100% necessary for this to work properly.


L. Spiro

#2L. Spiro

Posted 12 November 2012 - 05:52 PM

You have just returned a point to garbage.
buffer is a local variable. It is destroyed when the function returns and with it the memory it has allocated. If that code ever worked it would only be luck.
You can’t return a point to its buffer. Either return the buffer itself (not recommended) or pass a buffer to be used to hold the return (and return a reference to that).
As in:

std::string & readShaderFile(const std::string &fileName, std::string &buffer)
{
	 // Load file into buffer.
	 // Return buffer.
	 return buffer;
}


Notice also that fileName is now a reference. You should never pass std::string or other structures by copy.
Also notice that buffer is passed by reference. Not only is this once again a good idea, it is 100% necessary for this to work properly.


L. Spiro

#1L. Spiro

Posted 12 November 2012 - 05:51 PM

You have just returned a point to garbage.
buffer is a local variable. It is destroyed when the function returns and with it the memory it has allocated. If that code ever worked it would only be luck.
You can’t return a point to its buffer. Either return the buffer itself (not recommended) or pass a buffer to be used to hold the return (and return a reference to that).
As in:

std::string & readShaderFile(const std::string &fileName, std::string &buffer)
{
	 // Load file into buffer.
	 // Return buffer.
	 return buffer;
}


Notice also that fileName is now a reference. You should never pass std::string or other structures by copy.


L. Spiro

PARTNERS