disposing data

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
Adek336

disposing data

Post 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
zloba

Re:disposing data

Post by zloba »

shouldn't that be

Code: Select all

fifo_t * keys;
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,

Code: Select all

fifo_t * keys=0;
so that if you try to access it before init, it crashes and you know there's a problem :)
pini

Re:disposing data

Post 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).
Adek336

Re:disposing data

Post 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
Tim

Re:disposing data

Post 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.
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:disposing data

Post 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 ?
Adek336

Re:disposing data

Post by Adek336 »

I have a string class and string::string() inits the char buffer as

Code: Select all

asciiz = new char[0x10];
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 :o

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
Tim

Re:disposing data

Post 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.
Adek336

Re:disposing data

Post by Adek336 »

I will try to do so, then.. in a few days I'll post the results
Post Reply