[Plugin] Memory access plugin
#21

Yes.
pawn Код:
ptr = Pointer:NULL;
MEM::calloc() sets to zero, and MEM::malloc() leaves it as it was (what you actually can read after MEM::malloc() is data garbage left from previous accesses).
Reply
#22

Sorry for asking too much but let's say I have something like this:

pawn Код:
MEM::struct e_TEXT
{
    Message[100]
};
new Pointer:Texts[MAX_PLAYERS][MAX_TEXT_MESSAGES];
And my testing commands:
pawn Код:
CMD:on(playerid, params[])
{
    Texts[playerid][strval(params)] = MEM::calloc(e_TEXT);
    return 1;
}

CMD:off(playerid, params[])
{
    Texts[playerid][strval(params)] = Pointer:NULL;
    return 1;
}

CMD:show(playerid, params[])
{
    MEM_MACR_foreach(MAX_TEXT_MESSAGES, i)
    {
        if(Texts[playerid][i])
        {
            MEM::get_arr(Texts[playerid][i], Message, msg, 100);
            SCMEx(playerid, -1, "%s", msg);
        }
    }
    return 1;
}
When I try to set a specific index to a Pointer:NULL; it says it must be assigned to an array.
Reply
#23

CMD:on does allocate memory, and calling it again will cause leaking memory.
Using strval(params) without checks is not a good idea IMO.

CMD:off just sets the pointer to zero, which is bad, because the allocated memory is still allocated and you just lost the reference to it. See https://sampwiki.blast.hk/wiki/Memory_ac...in#Memory_leak
Use MEM:free to de-allocate it and set the pointer to NULL, so you won't cause leaking memory at all.

If you can't use NULL, because some other include may already defined it, you can use Pointer:0 or define MEM_NULL_EX before including memory.inc, so you can use Pointer:MEM_EX::NULL for the same result.
Reply
#24

I understand, it was just for testing purposes anyway.
So using calloc() in CMD: on every time will cause memory leak even after I set a pointer to null and free it?

Here's what I did using memory allocation: (not sure if it's written properly or has any memory leaking)

http://pastebin.com/ApQiGUNR

And when a player disconnects:
pawn Код:
MEM_MACR_foreach(MAX_TEXT_MESSAGES, i) // Text messages
{
    MEM::free(Texts[playerid][i]);
    Texts[playerid][i] = Pointer:0;
}
Connect
pawn Код:
MEM_MACR_foreach(MAX_TEXT_MESSAGES, i) // Text messages
{
     Texts[playerid][i] = MEM::calloc(e_TEXT);
}
Reply
#25

allocating at connection and freeing at disconnection is fine. Using this method you don't need further allocation and de-allocation at your command functions, and your memory is fine.
Reply
#26

Is there a way to set a float variable?
Reply
#27

Yes, for example:

Setting values:
pawn Код:
#define MEM_set_val_ex(%0,%1,%2)    (Float:(MEM_set_val(%0, %1, _:(%2))))
// ...

MEM::set_val_ex(ptr, _, 4.0);
// or...

new Float:p = 4.0;
MEM::set_val_ex(ptr, _, p);
Getting values
pawn Код:
#define MEM_get_float(%0)           (Float:(MEM_get_val(%0)))
// ...
printf("%f", MEM::get_float(ptr));
Setting values by using structures:
pawn Код:
#define MEM_set_val_ex(%0,%1,%2)    (Float:(MEM_set_val(%0, %1, _:(%2))))
MEM::struct Vect3D
{
    Float:Vect3D_X,
    Float:Vect3D_Y,
    Float:Vect3D_Z
}
// ...

MEM::set_val_ex(ptr, Vect3D_X, 2.0);
MEM::set_val_ex(ptr, Vect3D_Y, 3.0);
MEM::set_val_ex(ptr, Vect3D_Z, 4.0);
// or...

new Float:x = 2.0, Float:y = 3.0, Float:z = 4.0f;
// ...
MEM::set_val_ex(ptr, Vect3D_X, x);
MEM::set_val_ex(ptr, Vect3D_Y, y);
MEM::set_val_ex(ptr, Vect3D_Z, z);
// or...

new pos[Vect3D] = {2.0, 3.0, 4.0};
MEM::set_arr(ptr, _, pos);
Getting values by using structures:
pawn Код:
MEM::struct Vect3D
{
    Float:Vect3D_X,
    Float:Vect3D_Y,
    Float:Vect3D_Z
}
// ...

printf("{ %f, %f, %f }", MEM_get_val(ptr, Vect3D_X), MEM_get_val(ptr, Vect3D_Y), MEM_get_val(ptr, Vect3D_Z));
Reply
#28

Appreciated mate.
Reply
#29

You should add a CensOS version.

Anyway thank you for this plugin
Reply
#30

Files Not found.
Reply
#31

when I put this plugin
the server runs a bit and then shutdown
Reply
#32

I am sorry that the links have expired over the years. I have to relocate all my releases, create a build system, make some builds and update my threads over the next days. Please be patient.

Links are still working, kek.
Reply
#33

Quote:
Originally Posted by BigETI
View Post
I am sorry that the links have expired over the years. I have to relocate all my releases, create a build system, make some builds and update my threads over the next days. Please be patient.

Links are still working, kek.
I am guilty! I made some mistakes!
This plugin is great ++
Reply
#34

This plugin is obsolete. I recommend to switch to https://sampforum.blast.hk/showthread.php?tid=645166
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)