 |
Cheat Engine The Official Site of Cheat Engine
|
| View previous topic :: View next topic |
| Author |
Message |
zile Advanced Cheater
Reputation: 0
Joined: 11 Jul 2009 Posts: 75
|
Posted: Thu Aug 04, 2011 5:44 am Post subject: unsigned char* x = new unsigned char[] alternative |
|
|
As the title, i read somewhere that this line
| Code: | | unsigned char* x = new unsigned char[y]; |
is unsafe practice and shouldnt be used.
what are the alternatives to this? im using it to store packets and using them to send and etc now..
i want to change this because theres friggin alot of crashes in my app now at freec.h - at HeapFree's retval
how do i make unsigned char* x = packetbytes; ?
oh so the alternative is unsigned char x[y];
so i just use *x?
eg:
| Code: | void arrayCopy(unsigned char* src, int srcPos, unsigned char* dest, int destPos, int length){
for ( int i = 0; i < length; i++ )
dest[destPos+i] = src[srcPos+i];
} |
arrayCopy(*x... ) works?
the heapfree error is probably because of this :
unsigned char* x = new unsigned char[32000];
i use this to initialize and then write bytes to it and send as packet.. i think 32000 is too much for the heap
|
|
| Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8587 Location: 127.0.0.1
|
Posted: Thu Aug 04, 2011 2:39 pm Post subject: |
|
|
It's not that it's unsafe, it is that you are required to cleanup the memory afterward yourself since you are using the 'new' keyword. The general assumption that it's unsafe is due to that the application can exit unexpectedly at any time not giving you a chance to properly clean up memory.
You can use smart pointers to ensure the data is cleared properly when no references are still attached to the memory object.
Look into 'auto_ptr' or Boosts 'shared_ptr'.
http://ootips.org/yonat/4dev/smart-pointers.html
http://www.boost.org/doc/libs/1_47_0/libs/smart_ptr/shared_ptr.htm
As for the crash, are you over-stepping the buffer and writing too much to it? Are you also sure nothing is relying on the buffer when you delete it so that things are not attempting to read/write to invalid memory? Also are you sure you need 32000 bytes? Is your packet really that long? You should try to only use what is needed (or required) for the packet to work properly rather then making huge unneeded buffers.
_________________
- Retired. |
|
| Back to top |
|
 |
zile Advanced Cheater
Reputation: 0
Joined: 11 Jul 2009 Posts: 75
|
Posted: Thu Aug 04, 2011 8:03 pm Post subject: |
|
|
Heres an example on how i used it :
Stream.h
| Code: | class Stream {
private:
unsigned char* data;
int position;
int len;
public:
Stream(){
this->data = new unsigned char[MAX_LENGTH];
this->position = 0;
this->len = 0;
} |
| Code: | Stream* s = new Stream();
s->writeShort(S_REQ_WORLDLIST);
s->write(0);
writePacket(s);
|
writePacket
| Code: | void writePacket(Stream* data){
unsigned char* newData = new unsigned char[data->getSize()+4];
fCrypto->addPacketHeader(newData,data->getSize());
fCrypto->encrypt(data->getData(),data->getSize());
arrayCopy(data->getData(),0,newData,4,data->getSize());
data->setData(newData,data->getSize()+4);
send(cSocket, (const char*)data->getData(), data->getSize(), 0);
delete[] newData;
delete data;
} |
is there any way than doing something like new Stream(4)? // as in 4 is the size of the packet i want to send
edit : heres how i receive packet
| Code: | DWORD WINAPI receivePacket(LPVOID param){
InitializeCriticalSection(&decoderSection);
unsigned char packet[32000];
int size = 0;
do {
if ( (size = recv(cSocket, (char*)packet, sizeof(packet), 0)) > 0 ){
Stream* data = new Stream(packet,size);
OnPacket(data);
}
Sleep(1);
} while( size > 0 );
EndConnection();
return EXIT_FAILURE;
} |
| Code: | Stream(unsigned char* packet,int length){
len = length;
if (len > MAX_LENGTH || len <= 0)
return;
position = 0;
this->data = packet;
} |
OnPacket :
| Code: |
void OnPacket(Stream* Data){
Data->setPos(0);
//if cache not empty && not header, append to start of packet
if ( !fCrypto->checkHeader(Data->getData()) ){
Log("Incorrect header! " + Data->logPacket("Recv"));
delete Data;
EndConnection();
return;
}
while ( true ){
int totalLength = Data->getSize();
int realLength = fCrypto->getPacketLength(Data->getData());
if ( totalLength-4 < realLength ){
Log("Insufficient packet : not fixed");
/* incomplete packet -> store header2byte + remaining in cache */
break;
}
unsigned char* currentPacket = new unsigned char[realLength];
arrayCopy(Data->getData(),4,currentPacket,0,realLength);
fCrypto->decrypt(currentPacket,realLength);
Stream* newPacket = new Stream(currentPacket,realLength);
Log(newPacket->logPacket("Recv"));
handlePacket(newPacket);
if ( (totalLength-4-realLength) > 0 ){
Log("More than 1 packet found!");
unsigned char* nextPackets = new unsigned char[totalLength-4-realLength];
arrayCopy(Data->getData(),realLength+4,nextPackets,0,totalLength-4-realLength);
Data->setData(nextPackets,totalLength-realLength-4);
delete[] nextPackets;
} else {
delete[] currentPacket;
delete newPacket;
delete Data;
break;
}
} |
another q : if i use Data->getSize(); instead of totalLength above. theres error as well. it returns a different value before handlepacket and after it(handlepacket just reads the data and write the next packet to be sent). im guessing this is due to 32000 heap thing as well? its fixed after i put
| Code: | | int totalLength = Data->getSize(); |
and please do tell me if something there seems wrong/ mem leakable
|
|
| Back to top |
|
 |
Cheat Engine User Something epic
Reputation: 60
Joined: 22 Jun 2007 Posts: 2071
|
Posted: Fri Aug 05, 2011 5:30 am Post subject: Re: unsigned char* x = new unsigned char[] alternative |
|
|
| zile wrote: | As the title, i read somewhere that this line
| Code: | | unsigned char* x = new unsigned char[y]; |
is unsafe practice and shouldnt be used. | Not really, but frequent use of new and the removal of it can be very slow.
Also, for my next trick, I am going to C&P a thread from my forum regarding the memory leaks.
| knight666 wrote: | So, one of the things left to do for my awesome image loading library is getting rid of all the silly memory leaks. Apparently, the clever engineers at Microsoft hated memory leaks as much as I do, and they introduced a cool set of functions in Windows 95 (!). Here's how you use them for C++.
Initialization:
| Code: | #define _CRTDBG_MAP_ALLOC
#include <crtdbg.h> |
Now you're set. At the end of execution, check if memory leaks occurred:
| Code: | if (_CrtDumpMemoryLeaks())
{
printf("Abandon ship! I repeat, abandon ship!\n");
} |
However, if you do get memory leaks, they won't show up as file names! There's a very easy fix though:
| Code: | // please note that this stuff is extremely windows-specific
#if (MY_PLATFORM == MY_PLATFORM_WINDOWS)
#ifdef (_DEBUG)
#define _CRTDBG_MAP_ALLOC
#include <crtdbg.h>
#define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
#define new DEBUG_NEW
#endif
#endif |
You can even use this in libraries. As you long as you compile with "_DEBUG" defined, Runtime library (C/C++ -> Code Generation) is set to /MDd, and Enable C++ Exceptions is set to Yes (C/C++ -> Code Generation), you're good.
Sample output:
| Code: | Detected memory leaks!
Dumping objects ->
d:\projects\tinyimageloader2\src\tilimagepng.cpp(1651) : {108} normal block at 0x04110068, 65536 bytes long.
Data: < > 20 99 CD FF 20 99 CD FF 20 99 CD FF 20 99 CD FF
d:\projects\tinyimageloader2\src\tinyimageloader.cpp(248) : {98} normal block at 0x02142858, 1024 bytes long.
Data: <TinyImageLoader > 54 69 6E 79 49 6D 61 67 65 4C 6F 61 64 65 72 20
d:\projects\tinyimageloader2\src\tinyimageloader.cpp(332) : {97} normal block at 0x021427F0, 42 bytes long.
Data: <d:\Projects\Tiny> 64 3A 5C 50 72 6F 6A 65 63 74 73 5C 54 69 6E 79
d:\projects\tinyimageloader2\src\tinyimageloader.cpp(131) : {96} normal block at 0x02145E70, 260 bytes long.
Data: <d:\Projects\Tiny> 64 3A 5C 50 72 6F 6A 65 63 74 73 5C 54 69 6E 79
d:\projects\tinyimageloader2\src\tinyimageloader.cpp(119) : {95} normal block at 0x021423B0, 1024 bytes long.
Data: <TinyImageLoader > 54 69 6E 79 49 6D 61 67 65 4C 6F 61 64 65 72 20
d:\projects\tinyimageloader2\src\tinyimageloader.cpp(84) : {94} normal block at 0x02145E30, 4 bytes long.
Data: < > 0D 0A 00 00
d:\projects\tinyimageloader2\src\tinyimageloader.cpp(109) : {93} normal block at 0x02141F70, 1024 bytes long.
Data: < > 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
{87} normal block at 0x02145DE8, 12 bytes long.
Data: < > 80 02 00 00 E0 01 00 00 CD CD CD CD
Object dump complete. |
You can find a more detailed sample with your Visual Studio installation. <Visual Studio>\Samples\1033\AllVCLanguageSamples\C++\crt\crt_dbg1.
Hope this helps. ;) |
|
|
| Back to top |
|
 |
zile Advanced Cheater
Reputation: 0
Joined: 11 Jul 2009 Posts: 75
|
Posted: Sat Aug 06, 2011 2:57 pm Post subject: |
|
|
I think i would like to ignore the mem leaks first. i just want to fix the crash problem.
how do i do somethign like this? or is it not possible
because i can do it if i use new char[Length] lol
|
|
| Back to top |
|
 |
sponge I'm a spammer
Reputation: 1
Joined: 07 Nov 2006 Posts: 6009
|
Posted: Sat Aug 06, 2011 4:52 pm Post subject: |
|
|
You can't. The size of a local variable must be known at compile time.
_________________
|
|
| Back to top |
|
 |
Dark Byte Site Admin
Reputation: 471
Joined: 09 May 2003 Posts: 25857 Location: The netherlands
|
Posted: Sat Aug 06, 2011 5:37 pm Post subject: |
|
|
Or try a different compiler (the gnu compiler does support that)
_________________
Do not ask me about online cheats. I don't know any and wont help finding them.
Like my help? Join me on Patreon so i can keep helping |
|
| Back to top |
|
 |
zile Advanced Cheater
Reputation: 0
Joined: 11 Jul 2009 Posts: 75
|
Posted: Sat Aug 06, 2011 9:20 pm Post subject: |
|
|
Alright, thanks to everyone who helped, its fixed now, just had to change the one doing
| Code: | | new unsigned char[32000] |
and it stopped crashing
|
|
| Back to top |
|
 |
Slugsnack Grandmaster Cheater Supreme
Reputation: 71
Joined: 24 Jan 2007 Posts: 1857
|
Posted: Mon Aug 08, 2011 1:56 pm Post subject: |
|
|
| lmao great fix
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You can download files in this forum
|
|