1

I've included some source files from a BSD-licensed library into my program. I didn't notice a function with this signature:

class SomeClass {
    ...
    private:
    static void initializeFunc() __attribute__ ((__constructor__));
    ....
};

From this other stackoverflow question it seems that __constructor__ is supposed to make initializeFunc called whenever the library gets loaded by the running binary. Of course in my case, the source code is compiled into the binary so it's not called as a library.

Now, someone else in my organization noticed from an oprofile that initializeFunc is being called thousands of times, even if SomeClass is never instantiated or called. I don't understand this behavior. Probably I should just remove that attribute and call initializeFunc directly from main. But why in my case isn't it being called exactly once? Can someone help me understand what is going on here?

Community
  • 1
  • 1
firebush
  • 5,180
  • 4
  • 34
  • 45
  • 3
    To help you we could use a bit more information. How trust worthy is that profile result? Is the used profiler able to deal with constructor functions? How is the binary compiled? Does it use dynamic loading at some place? – Daniel Jour Sep 21 '15 at 22:55
  • @DanielJour: your intuition about the inaccuracy of the profiler turned out to be correct. As mentioned in my post, my colleague is using oprofile. I added a print statement to the function and in fact notice that it's called only exactly once. I guess oprofile is confused about the \_\_constructor\_\_ function. Does this give you enough information to add an explanation for why the profiler might be confused? I'd be happy to accept an answer. – firebush Sep 25 '15 at 02:13
  • Did the numbers change from run to run? I'm currently looking through the code of oprofile, cannot run a test on my own atm (I'm on mobile) – Daniel Jour Sep 25 '15 at 06:11
  • Yes, they change from run to run. – firebush Sep 30 '15 at 13:52

0 Answers0