Page 1 of 1
disposing data
Posted: Fri Jan 09, 2004 2:58 pm
by Adek336
I found a nasty habit of g++ which disposes all data I create.. for example:
Code: Select all
fifo_t keys;
int init_kbd()
{
keys = new fifo_t (0x100); //0x100 last keypresses
}
as I want to use the list later, it has been already disposed... I also found this on strings: each time i create a s = new string; it gets disposed after function end. This is nasty when I want to have a string list during all the execution... is this something well known or a misbehavour of g++?
Cheers,
Adrian
Re:disposing data
Posted: Fri Jan 09, 2004 3:51 pm
by zloba
shouldn't that be
because new returns a pointer
of course, you'll have to update any code using keys to use arrow ( -> ) instead of dot you've been using before
better yet,
so that if you try to access it before init, it crashes and you know there's a problem
Re:disposing data
Posted: Fri Jan 09, 2004 3:56 pm
by pini
First point : new is returnin a pointer, so make your lvalue check this.
Second point : is this for OSdeving ? because you can only use a redefinition of the new operator (no RTTI for a C++ kernel).
Re:disposing data
Posted: Sat Jan 10, 2004 6:04 am
by Adek336
actually, it was a typo. I did define it as *keys. And I have a working kmalloc() + kfree() implemenetation. I overloaded the new, delete operators. Now if I call new in a function, the object will *always* be disposed after the end of the routine. This is not comfortable for setting global tables, etc. I think there should be an option to turn it off; I'll check and tell you if I find one.
Cheers,
Adrian
Re:disposing data
Posted: Sat Jan 10, 2004 6:28 am
by Tim
No, this is not an option. It is broken. Returning from a function does not dispose of data allocated using new. It does destruct objects declared local to the function, but it doesn't touch objects allocated using new. Either there is a really big bug in gcc, and nobody's noticed, or there's a mistake in your code.
Re:disposing data
Posted: Sat Jan 10, 2004 7:27 am
by Pype.Clicker
what exactly makes you believe it is disposed ? have you tried disassembling kbd_init() function ?
could it be that kbd_init() is called prior a function that wipes the BSS or something alike ?
Re:disposing data
Posted: Sat Jan 10, 2004 7:34 am
by Adek336
I have a string class and string::string() inits the char buffer as
I have checked many times in various subfunctions that if now I save the pointer to the buffer somewhere and I want to read again, it may
1) report the string I put into it, that's when I do a string::asciiz = NULL before ending the first function
2) have garbage; as I checked further, the buffer was reckoned free space on the heap.
I suppose it's just g++ wanting my memory to be managed well and if I forget to dispose an object it does itself. This was a nasty bug when I allocated syms lists, each sym name was garbage
I think I'll just continue with the
Code: Select all
object_t *obj = new object_t;
.....
obj = NULL;
or use
Code: Select all
object_t *obj = (object_t*) kmalloc(sizeof(object_t));
.
Cheers,
Adrian
Re:disposing data
Posted: Sat Jan 10, 2004 8:44 am
by Tim
If you were writing Java or C# code I could understand the compiler wanting to clean up your allocations for you, but not C++. You have to try hard to get C++ to do garbage collection, so it's not going to do it behind your back.
It looks to me like some other part of your code is overwriting stuff it shouldn't be. I think you need to work out what is happening, or it will come back and bite you later. Start by reducing your code down until it behaves correctly, then start adding bits back until it stops working. Then you will know that the error lies in what you just added.
Bugs like this are a real pain, but better to get them fixed now while your kernel is still small, than later when there are far more possibilities to eliminate while debugging.
Re:disposing data
Posted: Sat Jan 10, 2004 7:46 pm
by Adek336
I will try to do so, then.. in a few days I'll post the results