The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register. September 19, 2019, 02:23:51 PM

Login with username, password and session length


Pages: [1]
  Print  
Author Topic: Copy Constructor funnies :)  (Read 4256 times)
m0nkeymafia
Expert
****
Offline Offline

Posts: 240


Check it!


View Profile
« on: January 24, 2008, 12:21:04 PM »

Another little tale of woes from work.

We store a list of classes, by value, in a stl list.  We copy it in by value into the list.
Been using the code for ages, job done, easy stuff.

We have some custom built data class templates inside the classes [verrrrry nice]
Anyway I upped the data limit in some of them and the program started crashing

After some digging it turns out we were hitting the stack limit [its a good thing to check for]
So we went through and put everything on the heap.
Now we start to get some very odd random crashes.

Turns out we hadnt implemented the copy constructor on the class, so when it was on teh stack, the compiler just copied it all easily, when we started using pointers to the memory on the heap, the compiler only copied the pointer over, so we got errors!

It was an hour or so out my day chasing ghosts, but a nice lesson anyway.
So if you ever get random crashes, think about memorrryyyy
Logged

I am Tyler Durden
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #1 on: January 24, 2008, 12:41:39 PM »

I love and hate pointers. That is such a classic type of bug it's not funny.

Glad you squashed it, random stuff is monstrously frustrating.
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
thedarkness
Lifer
*****
Offline Offline

Posts: 585



View Profile
« Reply #2 on: January 24, 2008, 08:03:22 PM »


After some digging it turns out we were hitting the stack limit [its a good thing to check for]


Dude, very interested. What was the stack limit and how would you test for that? Is the stack limit machine specific or is it fairly standard?

Cheers,
td
Logged

"I want to be the guy my dog thinks I am."
 - Unknown
thedarkness
Lifer
*****
Offline Offline

Posts: 585



View Profile
« Reply #3 on: January 25, 2008, 12:23:03 AM »


Turns out we hadnt implemented the copy constructor on the class, so when it was on teh stack, the compiler just copied it all easily, when we started using pointers to the memory on the heap, the compiler only copied the pointer over, so we got errors!


Watch out for the same sort of problems with the assignment operator as well, the default assignment operator will just do a bitwise copy of the pointers so in the case of "a = b;" internal pointers in "b" will now point to the same location in memory as the equivalent pointer in "a" and the memory "b"s pointer was pointing to is now lost for good (memory leak). The copy constructor won't leak but the assignment operator will. I guess you had the issue of trying to delete memory that had already been deleted as well huh? Prolly not what you want. Scott Meyers "Effective C++" has a great section on this, I have the second edition and there is now a third edition so I hope I'm still up to date lmao...... It's a great book and the more I read it the more  I understand (understood almost nothing the first time i read it).

What was the stack limit and how would you test for that? Is the stack limit machine specific or is it fairly standard?


To answer my own question "ulimit -s" will tell you the stack size 10MB in my case, seems to be fairly standard for l8 model linux.

Still don't know how to test for it though.... monk?

Cheers,
td

 
Logged

"I want to be the guy my dog thinks I am."
 - Unknown
m0nkeymafia
Expert
****
Offline Offline

Posts: 240


Check it!


View Profile
« Reply #4 on: January 25, 2008, 02:03:20 AM »

Hah yeah it's easy to forget stuf like that, even when you've solved it for an almost identical problem.

On PC the stack is about 1mb ish.

Its a hard one to test for because VS2005 [my IDE... may be different in others] throws an exception when you hit the stack limit, but doesnt catch it so you don't know you've hit it... you need to go into the options and actually tell it to catch all exceptions! Odd, and annoying, but ce la vie!!
Logged

I am Tyler Durden
thedarkness
Lifer
*****
Offline Offline

Posts: 585



View Profile
« Reply #5 on: January 25, 2008, 02:24:44 AM »

Cool, understood. I do a lot of stuff in VS as well, and lots in kdevelop, etc. on Linux.

I know I can look at the stack usage under linux through /proc/xxx/status or "top -H -p xxx" where xxx = pid.

Windows I'm not sure, don't grok Windows at that level as much (not OS you see :-) )

Cheers,
td
Logged

"I want to be the guy my dog thinks I am."
 - Unknown
Pages: [1]
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!